dtwd74916 2015-01-19 16:00
浏览 105

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

I have a multi-part email script, that takes a POSTed email address and sends a simple email to them in HTML and/or Plain Text. It displays correctly in Gmail and Outlook, but not eM (and doesn't even get through to a Communigate server). The code:

<?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
";
$headers .= "From: Maggie Multipart <web@ukipme.com>
";
$headers .= "To: " . $email_address . "
";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "
";

$message  = "This is a MIME encoded message.";
$message .= "

--" . $boundary . "
";

$message .= "Content-Type: text/plain;charset=utf-8

";
$message .= "Hello,
This is a test email, the text/plain version.

Regards
Maggie Multipart";
$message .= "

--" . $boundary . "
";

$message .= "Content-Type: text/html;charset=utf-8

";
$message .= "<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>";
$message .= "

--" . $boundary . "--";

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

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

// var_dump($_POST);
?>

The message is received in eM as follows:

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

Hello,

This is a test email, the text/plain version.

Regards

Maggie Multipart

However, eM is set up to receive HTML emails (and does so frequently). Can someone please help me fix this problem? Am I missing any headers?

  • 写回答

1条回答 默认 最新

  • doumei1926 2015-01-19 16:29
    关注

    My general advice for creating emails: Don't do it yourself (with some string concat functions/operators anyway). My weapon of choice is swiftmailer but there are other feasible libraries on the web, too.

    <?php
    require_once('autoload.php'); // swiftmailer was installed via Composer
    
    $message = Swift_Message::newInstance('This is a test multi-part email')
        ->setBody(
            "Hello,
    This is a test email, the text/plain version.
    
    Regards
    Maggie 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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计