1

I have this script that runs via a cron job once a month to send out awstats reports to clients. We've had to modify it recently because of tightening restrictions on our outbound mail server. Unfortunately some recipients are receiving the report as raw html code in the body of the message. Can anyone tell from the script where I went wrong?

    ########################## 
## Call to get awstats 
########################## 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL,"http://www.$your_domain:2082/awstats.pl" 
        ."?month=$report_month&year=$report_year&output=main&config=$your_domain" 
        ."&lang=en&framename=mainright"); 
curl_setopt($curl, CURLOPT_USERPWD, "$user:$password"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
//$attachment = curl_exec($curl); 
$attachment = "<br /><br /><div align=\"center\"><a href=\"$logo_link\"><img src=\"$logo_path\" border=\"0\" /></a>\n"; 
$attachment .= "<br /><font color=\"#FF0000\"><b>AwStats Report for $your_domain ($report_monthname $report_year)</b></font></div>\n";
$attachment .= "<base href=\"$awstats_img_path\">\n"; 
$attachment .= curl_exec($curl); 
curl_close($curl); 

########################## 
## Call to send email 
########################## 

$subject = "AwStats Report for $your_domain ($report_monthname $report_year)";
$header    = "From: " . $email_from . " <" . $email_from . ">\n";
$header .= "Reply-To: " . $email_from . "\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n";
$uid = md5(uniqid(time()));
$message = "--" . $uid . "\n";
$message .= "Content-type:text/html; charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $attachment . "\n\n";
mail($email_to, $subject, $message, $header);

I've obviously left out the variable declarations here. But they are all in the code. I actually receive a cc of the message and it displays fine in Apple Mail on the desktop.

Thanks, CJ

CJ Catalano
  • 97
  • 1
  • 1
  • 10

1 Answers1

1

You are missing the ending MIME boundary in the email which will cause some clients not to display it correctly.

Add this as the last line before mail();

$message .= "--{$uid}--\n"; // terminate mime boundary

EDIT: After your comment I noticed another issue.

$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n";
$uid = md5(uniqid(time()));

needs to be changed to:

$uid = md5(uniqid(time()));
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n";

The boundary was originally empty which might explain why some mail clients didn't show it properly.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • Hmmn, I added the closing as you suggested and reran the cron. So now I'm getting the raw html in Mac Mail. But when I checked in the client's Horde account in webmail the message shows up as "This message part contains HTML data, but inline HTML display is disabled. View HTML data in new window." Clicking on the view link does appear to display the message properly. So now the tables are turned. Not sure how it will appear in Outlook. FWIW in my Mac Mail program in the header I see this: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="" – CJ Catalano Mar 04 '16 at 04:44
  • Looks like the additional edit did the trick. I just tested with 2 clients and they both were able to read the message. It also came through correctly in Mac Mail again. I'll try with a few more clients and if all is well I'll mark the solution as correct. Thank you for your help drew010. – CJ Catalano Mar 04 '16 at 17:05