tags:

views:

46

answers:

2

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('&nbsp;',' ', $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

+1  A: 

Perhaps showing the contents of each component variable while looping could yield some insights?

Seems like a NULL may be showing up in the $body composition somehow -- first instinct anyway.

KMW
Thank you for the response. As mentioned I have tried dumping the content variable and found it contained expected html. Additionally I have tried replacing $html_text and $plain_text with a hard coded "oh hai" and found this did not produce different results.
jerrygarciuh
A: 

I freely admit I have no idea why this succeeds where the foreach() failed but the kludgy solution was to eliminate the foreach() and to assign the six possible addresses like so:

// #5 //
if ($to = $_SESSION['recipients'][4]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/alternative;\n" .
    "     boundary=" . $mime_boundary_header);
    echo "Email sent to " . htmlentities($to) . ".<br />";
}

It does the job here with no other code alterations and as Larry Wall said "Makeshifts last longest."

Peace,

JG

jerrygarciuh