how to send multipart/alternative email with PHP correctly

1.8k views Asked by At

I'm trying to use the built-in PHP mail function to send multipart messages that contain a html and a plain text version of my message. I've been playing around with different encoding types but, I keep running into problems. Originally I set Content-Transfer-Encoding to Binary but, that resulted in exclamation points being placed every 78 characters. I also tried base64 but I believe that base64 is overkill for what I am doing.

All I'm doing is sending basic HTML, no encoded images, files, or attachments. I'd prefer an encoding method that would still allow the source code to be human readable.

I heard that Quoted-Printable is what I'm looking for but, when I attempted to send messages using that encoding type the result ending up looking really weird. I noticed a bunch of " symbols sprinkled throughout the message source code.

Here is the code I'm using:

    $to = "[email protected]";
    $subject = "test subject";
    $boundary = uniqid('np');               
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: [email protected]\r\n";
    $headers .= "Reply-To: [email protected]\r\n";
    $headers .= "Return-Path: [email protected]\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
    $message = "This is a MIME encoded message.";

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $plainTextMessage;

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/html; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $HTMLmessage;

    $message .= "\r\n\r\n--" . $boundary . "--";

    $ok = mail($to,$subject,$message,$headers);

What the heck am I doing wrong here?

2

There are 2 answers

2
KMS On

Hi try the following code,

    $to = "[email protected]";
    $subject = "test subject";
    $plainTextMessage = "Hi all";
    $HTMLmessage = "<b>Hi all</b>";
     //$boundary = uniqid('np');               
    $boundary = md5(uniqid(time()));   
    $headers .= "From: [email protected]\r\n";
    $headers .= "Reply-To: [email protected]\r\n";
    $headers .= "Return-Path: [email protected]\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
    $message = "This is a MIME encoded message.";

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $plainTextMessage;

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/html; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $HTMLmessage;

    $message .= "\r\n\r\n--" . $boundary . "--";

    $ok = mail($to,$subject,$message,$headers);

May it will help

0
spice On

No need for a third party library or an external mail forwarding host. This is what I use and it works like a charm. It also fixes some potential security holes by forcing headers using the $from address that can otherwise reveal your system user :

private function sendMail($to, $from, $fromName, $subject, $text, $html)
{

    $boundary = uniqid('np');

    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: ".$fromName." <".$from.">" . "\r\n"; 
    $headers .= "X-Sender: ".$fromName." <".$from.">\n";
    $headers .= "Return-Path: <".$from.">\n";
    $headers .= "To: ".$to."\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

    // Content body
    $message = "This is a MIME encoded message.";
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

    // Plain text body
    $message .= $text;
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

    // Html body
    $message .= $html;
    $message .= "\r\n\r\n--" . $boundary . "--";

    // Send mail
    mail('', $subject, $message, $headers, '-f ' . $from);

}