2

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 = "to@test.com";
    $subject = "test subject";
    $boundary = uniqid('np');               
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: from-address@test.com\r\n";
    $headers .= "Reply-To: reply-address@test.com\r\n";
    $headers .= "Return-Path: return-path@test.com\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?

Austin
  • 1,619
  • 7
  • 25
  • 51
  • I would recommend using a library rather than trying to roll this one yourself. Email gets exceedingly complex quickly. Try something like https://github.com/PHPMailer/PHPMailer. Trust me, it'll make your life a lot easier. – Matt Rink Sep 08 '17 at 12:19
  • @matt I tried PHPMailer but no matter what I do I can't get it to work on my server. It's hosted with GoDaddy. Even GoDaddy said to just use the built-in mail function. – Austin Sep 08 '17 at 12:25
  • 1
    Try integrating with https://www.mailgun.com/ then. You make an API call and they build and send the email for you. – Matt Rink Sep 08 '17 at 12:31

2 Answers2

1

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);

}
spice
  • 1,442
  • 19
  • 35
0

Hi try the following code,

    $to = "to@test.com";
    $subject = "test subject";
    $plainTextMessage = "Hi all";
    $HTMLmessage = "<b>Hi all</b>";
     //$boundary = uniqid('np');               
    $boundary = md5(uniqid(time()));   
    $headers .= "From: from-address@test.com\r\n";
    $headers .= "Reply-To: reply-address@test.com\r\n";
    $headers .= "Return-Path: return-path@test.com\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

KMS
  • 566
  • 4
  • 16
  • I have verified it's not a boundary issue which is the only suggestion you provided here. – Austin Sep 08 '17 at 12:27
  • yes. Today only i worked on this. This code is working fine for me. Any error r u getting? – KMS Sep 08 '17 at 12:28