多部分电子邮件在eM Client中显示不正确

时间:2015-01-19 16:00:59

标签: php email multipart

我有一个多部分的电子邮件脚本,它接收一个POSTed电子邮件地址,并以HTML和/或纯文本格式向他们发送简单的电子邮件。它在Gmail和Outlook中正确显示,但不在eM中显示(并且甚至无法通过Communigate服务器)。代码:

<?php
$email_address = addslashes($_POST['email_address']);

if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    header("Location: ./?error=invalid-email");
    exit();
}

$subject_line = "This is a test multi-part email";

$boundary = uniqid();

$headers  = "MIME-Version:1.0\r\n";
$headers .= "From: Maggie Multipart <web@ukipme.com>\r\n";
$headers .= "To: " . $email_address . "\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\r\n";
$message .= "Hello,\nThis is a test email, the text/plain version.\n\nRegards\nMaggie Multipart";
$message .= "\r\n\r\n--" . $boundary . "\r\n";

$message .= "Content-Type: text/html;charset=utf-8\r\n\r\n";
$message .= "<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>";
$message .= "\r\n\r\n--" . $boundary . "--";

mail("", $subject_line, $message, $headers);

header("Location: ./?success=email-sent");
exit();

// var_dump($_POST);
?>

eM中收到的消息如下:

  

Content-Type:text / plain; charset = utf-8

     

您好,

     

这是一封测试电子邮件,即text / plain版本。

     

此致

     

Maggie Multipart

但是,eM设置为接收HTML电子邮件(并且经常这样做)。有人可以帮我解决这个问题吗?我错过了任何标题吗?

1 个答案:

答案 0 :(得分:0)

我对创建电子邮件的一般建议:不要自己做(有一些字符串连接函数/运算符)。我选择的武器是swiftmailer,但网上还有其他可行的库。

<?php
require_once('autoload.php'); // swiftmailer was installed via Composer

$message = Swift_Message::newInstance('This is a test multi-part email')
    ->setBody(
        "Hello,\nThis is a test email, the text/plain version.\n\nRegards\nMaggie Multipart",
        'text/plain',
        'utf-8'
    )
    ->addPart(
        "<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>",
        'text/html',
        'utf-8'
    )
    ->setFrom(array('...@...' => '...'))
    ->setTo(array('...@...' => '...'));

$transport = Swift_SmtpTransport::newInstance('MSERV', 25, 'tls')
  ->setUsername('...')
  ->setPassword('...');

$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);