PHP mail cc bcc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP mail cc bcc

Post by cjkeane »

I've resolved my issues with regards to sending to cc and bcc. thanks

Code: Select all

<?php
       $from = $Sender;
		$to = $EmailTo;
		$subject = "RE: " . utf8_decode($EmailSubject);
		$returnpath = explode("<", $from);
		$returnpath = str_replace(">", "", $returnpath['1']);
		$body .= utf8_decode($ActionTextField);
									
		// Headers
		$priority = "3";
		$headers = "From: " . $from ."\n";
		$headers .= "Cc: " . $emailcc ."\n";
		$headers .= "Bcc: " . $emailbcc ."\n";
		$headers .= "Reply-To: $emailfrom\n";
		$headers .= "X-Sender: <$emailfrom>\n";
		$headers .= "X-Mailer: PHP v" . phpversion() . "\n";
		$headers .= "X-Priority: $priority\n"; //1 UrgentMessage, 3 Normal
		
		// create a boundary string. It must be unique
		$semi_rand = md5(time());
		$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

		// Add the headers for a file attachment
		$headers .= "MIME-Version: 1.0\n" .
		            "Content-Type: multipart/mixed;\n" .
		            " boundary=\"{$mime_boundary}\"";

		 // Add a multipart boundary above the plain message
		 // $message ="This is a multi-part message in MIME format.\n\n";
		  	 
		 $message ="--{$mime_boundary}\n";
		 //$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
		 $message.="Content-Type: text/plain; charset=\"unicode utf-8\"\n";
		 $message.="Content-Transfer-Encoding: 7bit\n\n";
		 $message .= str_replace("\n", "", $body)."\n";
		$message .= "--PHP-alt-".$mime_boundary."--\n";

		$fullfilepath = 'upload/' . $attachments;
		// Read the file to be attached ('rb' = read binary)
		$file = fopen($fullfilepath,'rb');
		$data = fread($file,filesize($fullfilepath));
		 
		// Base64 encode the file data and identify the filetype using the returnMIMETYPE function
		$data = chunk_split(base64_encode($data));
		$mime = returnMIMEType($attachments); 
		
		// Add file attachment to the message
		$message .= "--{$mime_boundary}\n" .
		              "Content-Type: {$mime};\n" .
		              " name=\"{$attachments}\"\n" .
		              "Content-Disposition: attachment;\n" .
		              //" filename=\"{$fileatt_name}\"\n" .
		              "Content-Transfer-Encoding: base64\n\n" .
		              $data . "\n\n" .
		              "--{$mime_boundary}--\n";
		
		//Sending part
		if (!mail($to,$subject,$message,$headers,$returnpath)) {
			echo '<div style="color:red;" align="right">Reply failed to be sent...</div><br/>';
	} else {
                echo '<div style="color:red;" align="right">Message sent...</div><br/>';

?>
Last edited by cjkeane on Thu Aug 18, 2011 9:59 am, edited 4 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP mail

Post by social_experiment »

Code: Select all

<?php
/*
$headers .= "Cc: " . $emailcc ."\n";
$headers .= "Bcc: " . $emailbcc ."\n";
*/
?>
Try the script with the above code in comments
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP mail

Post by cjkeane »

what if I do want the cc and bcc addresses included? using /* and */ will effectively not use the addresses specified on the form. are there any other suggestions?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP mail

Post by social_experiment »

cjkeane wrote:what if I do want the cc and bcc addresses included? using /* and */ will effectively not use the addresses specified on the form
Valid point.

Edit.

Look at server settings, tested the script and it only sends to the '$to' email address.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: PHP mail

Post by beetree »

Someone may need to correct me, but doesn't PHPs mail() use sendmail to send the email? If so, you may want to look into the configuration of sendmail. Go to your shell and type "mail -h" and make sure that it works sending from the command line.
Post Reply