I have some problems with sending a multipart E-Mail through the WordPress function wp_mail, which is basically just a wrapper for PHP Mailer.
In the OS X Mail.app, the Mail displays fine. In Thunderbird, nothing is displayed, while Google Mail displays an attachment called ‘noname’ with the size of 1 KB.
My guess is that it has something to do with the encoding, since I noticed that the boundary is not displayed in the E-Mail’s header at all, while it appears in the header from other E-Mails I have received. Also, there seems to be some kind of Content-Transfer-Encoding mismatch?
These are the important bits of the header:
To: email@address.com
Subject: Testsubject
X-PHP-Originating-Script: 10029:class-phpmailer.php
Date: Mon, 24 Mar 2014 15:52:59 +0000
From: WordPress <wordpress@example.com>
Message-ID: <ab7dd25d45c2d5be71df1e592bb0ab96@www.example.com>
X-Priority: 3
X-Mailer: PHPMailer 5.2.4 (http://code.google.com/a/apache-extras.org/p/phpmailer/)
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: multipart/alternative; charset=UTF-8
--1234567890
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Text Plain
--1234567890
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
<b>HTML E-Mail</b>
--1234567890--
In other HTML E-Mail that get correctly displayed, I can clearly read the boundary from the header. Also, the Content-Transfer-Encoding is different.
Content-Type: multipart/alternative; boundary="--==_mimepart_532c53995fed2_5f733fb4b5966cd0158292"; charset="UTF-8"
Content-Transfer-Encoding: 7bit
In my PHP script, the mail is compiled like that:
$to = 'email@address.com';
$subject = 'Testsubject';
$headers = 'Content-Type: multipart/alternative; charset=UTF-8; boundary="1234567890"' . "rnrn";
$message = '--1234567890' . "rn";
$message .= 'Content-Type: text/plain; charset=UTF-8' . "rn";
$message .= 'Content-Transfer-Encoding: 7bit' . "rnrn";
$message .= 'Text Plain' . "rnrn";
$message .= '--1234567890' . "rn";
$message .= 'Content-Type: text/html; charset=UTF-8' . "rn";
$message .= 'Content-Transfer-Encoding: 7bit' . "rnrn";
$message .= '<b>HTML E-Mail</b>' . "rnrn";
$message .= '--1234567890--';
wp_mail( $to, $subject, $message, $headers );
Do you see anything that strikes you here? Why is the E-Mail body not displayed at all in Thunderbird/Google Mail?
At the moment I am thinking that maybe the server is misconfigured or something like that, but the fact that Mail.app displays the mail correctly does not fit into that.
Add “rn” after each header line in the $headers var.
Some mail systems have an issue with “r” – it breaks the message on some mail servers for some reason; if you continue to have issues, try omitting the “r” ‘s.
You can also pass
$headers
as an array andwp_mail
will handle it for you.