【问题标题】:Multi-part emails displaying incorrectly in eM Client多部分电子邮件在 eM 客户端中显示不正确
【发布时间】:2015-03-17 16:47:45
【问题描述】:

我有一个包含多个部分的电子邮件脚本,它接受一个已发布的电子邮件地址,并以 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中收到的消息如下:

内容类型:text/plain;charset=utf-8

你好,

这是一封测试邮件,文本/纯文本版本。

问候

玛吉多部分

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

【问题讨论】:

    标签: php email multipart


    【解决方案1】:

    我对创建电子邮件的一般建议:不要自己做(无论如何都要使用一些字符串连接函数/运算符)。我选择的武器是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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      相关资源
      最近更新 更多