Page 1 of 1

PHP mail cc bcc

Posted: Sat Jul 16, 2011 9:57 pm
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/>';

?>

Re: PHP mail

Posted: Sun Jul 17, 2011 2:11 am
by social_experiment

Code: Select all

<?php
/*
$headers .= "Cc: " . $emailcc ."\n";
$headers .= "Bcc: " . $emailbcc ."\n";
*/
?>
Try the script with the above code in comments

Re: PHP mail

Posted: Sun Jul 17, 2011 9:24 am
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?

Re: PHP mail

Posted: Sun Jul 17, 2011 11:12 am
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.

Re: PHP mail

Posted: Mon Jul 18, 2011 8:12 pm
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.