Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with sending a multipart/alternative email with PHP

Tags:

php

email

Here's the script that's builds/sends the email:

$boundary = md5(date('U'));

$to = $email;
$subject = "My Subject";

$headers = "From: [email protected]" . "\r\n".
           "X-Mailer: PHP/".phpversion() ."\r\n".
           "MIME-Version: 1.0" . "\r\n".
           "Content-Type: multipart/alternative; boundary=--$boundary". "\r\n".
           "Content-Transfer-Encoding: 7bit". "\r\n";

$text = "You really ought remember the birthdays";     
$html = '<html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </html>
    ';

$message = "Multipart Message coming up" . "\r\n\r\n".
       "--".$boundary.
       "Content-Type: text/plain; charset=\"iso-8859-1\"" .
       "Content-Transfer-Encoding: 7bit".
       $text. 
       "--".$boundary. 
       "Content-Type: text/html; charset=\"iso-8859-1\"". 
       "Content-Transfer-Encoding: 7bit". 
       $html.
       "--".$boundary."--";



mail("[email protected]", $subject, $message, $headers);

It sends the message just fine, and my recipient receives it, but they get the whole thing in text/plain instead of in multipart/alternative. Viewing the source of the received message gives this (lots of cruft removed):

Delivered-To: [email protected]
Received: by 10.90.100.4 with SMTP id x4cs111413agb;
    Wed, 25 Mar 2009 16:39:32 -0700 (PDT)
Received: by 10.100.153.6 with SMTP id a6mr85081ane.123.1238024372342;
    Wed, 25 Mar 2009 16:39:32 -0700 (PDT)
Return-Path: <[email protected]>
--- snip ---
Date: Wed, 25 Mar 2009 17:37:36 -0600 (MDT)
Message-Id: <[email protected]>
To: [email protected]
Subject: My Subject
From: [email protected]
X-Mailer: PHP/4.3.9
MIME-Version: 1.0
Content-Type: text/plain;
boundary="--66131caf569f63b24f43d529d8973560"
Content-Transfer-Encoding: 7bit
X-OriginalArrivalTime: 25 Mar 2009 23:38:30.0531 (UTC) FILETIME=[CDC4E530:01C9ADA2]
X-TM-AS-Product-Ver: SMEX-8.0.0.1181-5.600.1016-16540.005
X-TM-AS-Result: No--4.921300-8.000000-31
X-TM-AS-User-Approved-Sender: No
X-TM-AS-User-Blocked-Sender: No


Multipart Message coming up

--66131caf569f63b24f43d529d8973560
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

You really ought remember the birthdays

--66131caf569f63b24f43d529d8973560
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </html>


--66131caf569f63b24f43d529d8973560--

It looks like the content-type header is getting changed along the way from multipart/alternative to text/plain. I'm no sysadmin, so if this is a sendmail issue I'm in way over my head. Any suggestions?

like image 513
saturdayplace Avatar asked Mar 26 '09 16:03

saturdayplace


2 Answers

The line

"Content-Type: multipart/alternative; boundary=--$boundary". "\r\n".

should be

"Content-Type: multipart/alternative; boundary=$boundary". "\r\n".

You don't include the dashes in the header.

like image 86
Greg Avatar answered Oct 21 '22 11:10

Greg


To get this to work properly for me, I also needed to add a "\r\n" at the end of each header line, two of them ("\r\n\r\n") at the end of the lines before the text and html bodies, and one at the end of each body. This results in something like this:

$message = "Multipart Message coming up" . "\r\n\r\n".
       "--".$boundary."\r\n".
       "Content-Type: text/plain; charset=\"iso-8859-1\""."\r\n".
       "Content-Transfer-Encoding: 7bit"."\r\n".
       $text."\r\n". 
       "--".$boundary."\r\n". 
       "Content-Type: text/html; charset=\"iso-8859-1\""."\r\n". 
       "Content-Transfer-Encoding: 7bit"."\r\n".
       $html."\r\n".
       "--".$boundary."--";
like image 26
Sam Critchley Avatar answered Oct 21 '22 11:10

Sam Critchley