Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/244543-php-mail-using-bcc-and-html/
Share on other sites

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.

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?

 

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

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.

 

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

 

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";

?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.