Swift Mailer - 获取奇数标头输出


Swift Mailer - Getting odd header output

我正在使用Swift Mailer,但得到奇怪的输出,如下所示:

--_=_swift_v4_1421068500_80a78fd29e619e918b1c16e227289934500e81e7_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Website EnquiryName - nameEmail - example@example.comTelephone - te=
lephoneCompany - companyMessage - message
--_=_swift_v4_1421068500_80a78fd29e619e918b1c16e227289934500e81e7_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<h2>Website Enquiry</h2><p><strong>Name</strong> name</p><p><strong=
>Email</strong> example@example.com</p><p><strong>Telephone</strong> teleph=
one</p><p><strong>Company</strong> company</p><p><strong>Message</strong><b=
r />message</p>
--_=_swift_v4_1421068500_80a78fd29e619e918b1c16e227289934500e81e7_=_--

我像这样发送电子邮件:

<?php
    if ( $_SERVER['REQUEST_METHOD'] != 'POST' ) {
        die();
    }
    require_once '../swiftmailer/lib/swift_required.php';
    $name       = utf8_encode(htmlentities($_POST['name'], ENT_QUOTES, "UTF-8"));
    $email      = utf8_encode(htmlentities($_POST['email'], ENT_QUOTES, "UTF-8"));
    $telephone  = utf8_encode(htmlentities($_POST['telephone'], ENT_QUOTES, "UTF-8"));
    $company    = utf8_encode(htmlentities($_POST['company'], ENT_QUOTES, "UTF-8"));
    $message    = utf8_encode(htmlentities($_POST['message'], ENT_QUOTES, "UTF-8"));
    $to = "example@example.com"; // Removed for Stack Overflow demo.
    $subject = "Website Enquiry";
    $htmlMessage = "<h2>Website Enquiry</h2>";
    $htmlMessage .= "<p><strong>Name:</strong> "    . $name . "</p>";
    $htmlMessage .= "<p><strong>Email:</strong> " . $email . "</p>";
    $htmlMessage .= "<p><strong>Telephone:</strong> " . $telephone . "</p>";
    $htmlMessage .= "<p><strong>Company:</strong> " . $company . "</p>";
    $htmlMessage .= "<p><strong>Message:</strong><br />" . nl2br($message) . "</p>";
    $plainMessage = "Website Enquiry";
    $plainMessage .= "Name: " . $name . "";
    $plainMessage .= "Email: " . $email . "";
    $plainMessage .= "Telephone: " . $telephone . "";
    $plainMessage .= "Company: " . $company . "";
    $plainMessage .= "Message: " . $message . "";
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom(array($email => $name))
        ->setTo(array($to => $subject))
        ->setContentType("text/html; charset=UTF-8")
        ->setBody($plainMessage, 'text/plain')
        ->addPart($htmlMessage, 'text/html');
    $result = $mailer->send($message);
?>

最好是我想发送HTML电子邮件,但我也想要纯文本版本。上面的代码在我能看到的各个方面似乎都没问题,所以不确定为什么我得到电子邮件标题的奇怪输出 - 可能是字符编码问题?

任何帮助或指导非常感谢!

谢谢迈克尔

我使用的是DEV版本 - Swift-5.3.1-DEV(因此可能不稳定)。

降级到最新的稳定版本 Swift-5.3.0 后,我现在可以看到这个问题消失了。

我没有使用 Composer 来安装最新的稳定版,而是发现自己使用的是 SwiftMailer GitHub 帐户中的 ZIP。此版本是 DEV 版本,因此请确保您获得的是最新的稳定版本:https://github.com/swiftmailer/swiftmailer/releases

希望这有帮助。