cactuscake Posted October 20, 2009 Share Posted October 20, 2009 A bit confused about this... Below is a copy of some code on my website, currently works without a hitch, however, when I try to add a bcc address to the headers it either sends the entire message in text/plain or it messes up the 'from' field or it just doesn't work at all (depending on the syntax I've tried using). Can anyone suggest a way of slipping a bcc header in without screwing anything up? <?php $name = $_POST['sendername']; $to = $_POST['recipient']; $from = $_POST['senderemail']; $subject = basename( $name)." has sent you a jCard!"; $message = $_POST['text']; $bcc = $_POST['bcc']; $finalimage = $_POST['finalimage']; $headers = "From: ". $name. " <". $from .">\n"; // Read the file to be attached ('rb' = read binary) $file = fopen($finalimage,'rb'); $data = fread($file,filesize($finalimage)); fclose($file); // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-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" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $name . " sent you this jCard:\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"jcard.jpg\"\n" . "Content-Disposition: attachment;\n" . " filename=\"jcard.jpg\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; // Send the message $ok = @mail($to, $subject, $message, $headers); if ($ok) { <snip> Quote Link to comment https://forums.phpfreaks.com/topic/178380-solved-bcc-in-multipart-email-causing-grief/ Share on other sites More sharing options...
mikesta707 Posted October 20, 2009 Share Posted October 20, 2009 taken straight from the PHP manual $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n"; $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n"; $headers .= 'Cc: birthdayarchive@example.com' . "\r\n"; $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";//this is probably what you want to pay attention to is that what you put? it it all different? How did you add it in there? Quote Link to comment https://forums.phpfreaks.com/topic/178380-solved-bcc-in-multipart-email-causing-grief/#findComment-940701 Share on other sites More sharing options...
cactuscake Posted October 20, 2009 Author Share Posted October 20, 2009 Tried this: $headers = "From: ". $name. " <". $from .">\r\n"; $headers .= "Bcc: ". $bcc ."\r\n"; But it sends the email as plain text While I'm here - what is the difference between \r\n and just \n? Quote Link to comment https://forums.phpfreaks.com/topic/178380-solved-bcc-in-multipart-email-causing-grief/#findComment-940712 Share on other sites More sharing options...
mikesta707 Posted October 20, 2009 Share Posted October 20, 2009 well firstly, your headers should start like this $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; the \r\n is there to satisfy the RFC 2822 internet mail standard. \n does not satisfy the RFC2822 standard, so its not valid Quote Link to comment https://forums.phpfreaks.com/topic/178380-solved-bcc-in-multipart-email-causing-grief/#findComment-940714 Share on other sites More sharing options...
cactuscake Posted October 21, 2009 Author Share Posted October 21, 2009 Couldn't get my original code to behave with the MIME header coming first, for some reason it was sending the mail as text/plain if I changed pretty much anything from that original layout. In the end I borrowed a completely new code snippet and it seems to work much better. Posting it here in case anyone else comes across the same problem in the future. I've tailored it for an image attachment that I want to be able to show inline in the body of the html email, you need to change the content-disposition if you don't want it to behave that way. <?php $name = $_POST['sendername']; $to = $_POST['recipient']; $from = $_POST['senderemail']; $subject = basename( $name)." sent you a card!"; $message = $_POST['text']; $bcc = $_POST['bcc']; $finalimage = $_POST['finalimage']; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: ".$name." <".$from.">\r\nReply-To: ".$name." <".$from.">\r\nBcc: ".$bcc.""; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the attachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents($finalimage))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Plain text email content --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <p>html email content</p> <img src="cid:<?php echo $random_hash; ?>.image.jpg" /> <p>more html content below image</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: image/jpeg; name="image.jpg" Content-Transfer-Encoding: base64 Content-ID: <<?php echo $random_hash; ?>.image.jpg> Content-Disposition: inline; filename="/image.jpg" <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully return mail sent message. Otherwise return fail message. echo $mail_sent ? "Mail sent" : "Mail failed"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/178380-solved-bcc-in-multipart-email-causing-grief/#findComment-941100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.