TheBrandon Posted August 11, 2011 Share Posted August 11, 2011 Hello all, I need to send out mass HTML emails to a list of people in a BCC list. Right now I have everything working except the BCC part. It seems to do nothing. Here is my code: $email_array = getEmailAddresses($dbHost, $dbUser, $dbPass, $dbDatabase); $email_array = implode(", ", $email_array); $subject = 'Subject'; $headers = "From: ".$to."\n"; $headers .='Content-type: text/html; charset=iso-8859-1 '. "\r\n"; $headers .= 'BCC: '.$email_array.'' . "\r\n"; mail($to, $subject, " <html> <head> <title>Title</title> <style> table {background-color:white;border:1px solid black;padding:5px;} table tr {padding:10px;border:1px solid black;background-color:white;} table tr td {padding:5px;border:1px solid black;} table tr.odd {background-color:#e2e1df;} </style> </head> <body> <table> <tr> <td><p>Please see below for a new announcement.</p></td> </tr> <tr> <td>".$announcement_content."</td> </tr> </table> </body> " , $headers); } Can anyone help me get this to work? Right now if you run that code, it is putting the list of BCC emails in the body of the email; not even in the headers. Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/ Share on other sites More sharing options...
gizmola Posted August 11, 2011 Share Posted August 11, 2011 2 things: -the bcc header is 'Bcc' -this is not how to do an html email, from a standards point of view. You should start with a text only version, and multi part mime encode the html version. There are plenty of articles and tutorials on how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256105 Share on other sites More sharing options...
TheBrandon Posted August 11, 2011 Author Share Posted August 11, 2011 Anything you recommend specifically? If I change it to Bcc it still spits out in the body. Is there any way to fix it without re-writing the entire email portion? I'll be happy to follow any tutorials you recommend for sure but is it possible to make this work this way, or does it have to be rewritten to do the mime thing? Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256111 Share on other sites More sharing options...
gizmola Posted August 11, 2011 Share Posted August 11, 2011 What OS are you running this code on? What is the email server involved? One other thing i just noticed, is that your From header just has a \n, --- the headers all need the \r\n. Here's one article that lays out the right structure. Bottom line, is that email is a 7bit tech, and html doesn't fit into that. If you want to code something that doesn't work right for many email agents because you want to scrape by, it's a lot harder to say definitively what the end result will be or diagnose problems. It's not like doing it the standard compliant way is that difficult. Here's one article, and there are many more that can be found with a quick search: for things like php email, php html email, php multipart etc. http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256114 Share on other sites More sharing options...
TheBrandon Posted August 11, 2011 Author Share Posted August 11, 2011 What OS are you running this code on? What is the email server involved? One other thing i just noticed, is that your From header just has a \n, --- the headers all need the \r\n. Here's one article that lays out the right structure. Bottom line, is that email is a 7bit tech, and html doesn't fit into that. If you want to code something that doesn't work right for many email agents because you want to scrape by, it's a lot harder to say definitively what the end result will be or diagnose problems. It's not like doing it the standard compliant way is that difficult. Here's one article, and there are many more that can be found with a quick search: for things like php email, php html email, php multipart etc. http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php OSX. Yeah I noticed the \r\n thing earlier and tried this, but it didn't work: $subject = 'New Announcement From Rhino Shield'; $headers = "From: ".$to." \r\n"; $headers .='Content-type: text/html; charset=iso-8859-1 '. "\r\n"; $headers .= 'BCC: '.$email_array.'' . "\r\n"; I'll take a look at the article when I get home. Thank you for recommending it. I don't intend to try and make poor code to scrape by; it's more of an intellectual curiosity side of things as to why simply adding BCC breaks it. Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256124 Share on other sites More sharing options...
gizmola Posted August 11, 2011 Share Posted August 11, 2011 Even in your latest example, you're still screwing up the Bcc header Little details matter. I think you would also benefit from a better understanding of the difference between using single and double quotes for php strings. Start with this: $subject = 'New Announcement From Rhino Shield'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: $to \r\n"; $headers .= "Bcc: $email_array \r\n"; mail($to, $subject, ' Title <br /> table {background-color:white;border:1px solid black;padding:5px;}<br /> table tr {padding:10px;border:1px solid black;background-color:white;}<br /> table tr td {padding:5px;border:1px solid black;}<br /> table tr.odd {background-color:#e2e1df;}<br /> </pre> <table> Please see below for a new announcement. ".$announcement_content." </table> <br><br><br>', $headers Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256159 Share on other sites More sharing options...
TheBrandon Posted August 12, 2011 Author Share Posted August 12, 2011 Okay so I tried using your BCC header on my code and it didn't do anything differently, so now I'm trying that tutorial you posted. I'm using this code, but again, the bcc does nothing. I'd appreciate your help. <?php //define the receiver of the email $to = 'brandon@revivemediaservices.com'; //define the subject of the email $subject = 'Test HTML email'; //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: webmaster@example.com\r\nReply-To: webmaster@example.com"; //add bcc header $header .= "\r\nBcc:brandon@revivemediaservices.com, brandon@revivemediaservices.com, michele@revivemediaservices.com"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit I'm the announcement variable too! Yay! --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <html> <head> <title>New Announcement</title> <style> table {background-color:white;border:1px solid black;padding:5px;} table tr {padding:10px;border:1px solid black;background-color:white;} table tr td {padding:5px;border:1px solid black;} table tr.odd {background-color:#e2e1df;} </style> </head> <body> <table> <tr> <td><p>Please see below for a new announcement</p></td> </tr> <tr> <td>I'll be an announcement variable one day!</td> </tr> </table> </body> --PHP-alt-<?php echo $random_hash; ?>-- <? //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 print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256443 Share on other sites More sharing options...
gizmola Posted August 12, 2011 Share Posted August 12, 2011 You have a typo in your code --- you set $headers, then append to $header. Probably your first variable should be $header = "From....." Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256565 Share on other sites More sharing options...
gizmola Posted August 12, 2011 Share Posted August 12, 2011 Well what I mean is that you need to use either a variable named $header or $headers, and use that throughout the script. Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256566 Share on other sites More sharing options...
gizmola Posted August 12, 2011 Share Posted August 12, 2011 This is 100% debugged and verified to work. I changed email addresses in my test to send to a number of email accounts. I also cleaned things up and eliminated some spurious spaces and things that broke the multipart in gmail. Webmail readers are notoriously picky about these things and the version I started with did not parse correctly in gmail. An extra space after the part separators is enough to throw things off. The main thing I noted is that you have a bad habit of being inconsistent -- for example, you put /r/n in front of some headers and after others. That sort of practice is going to lead to a hard to debug error, and of course you missed little things like having no Quote Link to comment https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/#findComment-1256597 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.