Hi guys,
I have a client that wanted their sales staff to be able to populate highly designed 2 column html templates and send emails using these. 6 recipients max per email.
Solution worked very well until they decided they want to send to each of the recipients separately. No CC, no BCC.
I figured where I had simply imploded the array of addresses before I would loop instead but this produced emails with empty content. Subject line was fine but the content was zero bytes.
Interestingly if I insert a die() and dump the $html_text to screen I see what I expect to have as content.
Initially I set the content, subject etc outside the loop but seeing empty content I thought perhaps I should set the vars inside the loop though this made no sense to me. It changed nothing.
Here is the looping code:
if (count($_SESSION['recipients']) > 0) {
foreach ($_SESSION['recipients'] as $to) {
$template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile'];
ob_start();
include($template);
$html = ob_get_contents();
ob_end_clean();
if (strlen($html) == 0) {
echo "The template at $template did not load.";
exit;
}
$bullets = unslash($_SESSION['bullets']);
$bullets = preg_replace('/<li>/', '* ', $bullets);
$bullets = strip_tags($bullets);
$TextMessage = strip_tags(unslash($_SESSION['message'])) . "\n\n" . $bullets;
$notice_text = "This is a multi-part message in MIME format.";
$plain_text = str_replace(' ',' ', $TextMessage);
$html_text = $html;
$email = $_SESSION['user']->email;
$name = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name;
$from = "$name <$email>";
$subject = unslash($_SESSION['subject']);
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$body = "$notice_text
--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$plain_text
--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$html_text
--$mime_boundary--";
if (@mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=" . $mime_boundary_header)) {
$out .= " Email sent to " . htmlentities($to) . ".<br />";
} else {
$out .= " Email to htmlentities($to) NOT sent successfully!";
}
} // foreach
Any advice much appreciated!
2010.08.06 - EDIT: I have tried replacing $html_text and $plain_text with hard coded text and found the same results: Empty mail sent. Also tried moving the $mime_boundary and $mime_boundary_header generation outside the loop with no success.
However if I add die('<pre>'.$body.'</pre>');
immediately after the empty mail is sent I see everything I would expect in the HTML and the text and the boundaries... Here is an example:
This is a multi-part message in MIME format.
--==MULTIPART_BOUNDARY_94e43eed6cf2826fc5787c860814066e
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
My plain text here
* Bullet point 1
* Bullet point 2
--==MULTIPART_BOUNDARY_94e43eed6cf2826fc5787c860814066e
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
// RENDERED HTML CONTENTS SHOW UP HERE
JG