我使用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电子邮件,但我也想要一个纯文本版本。上面的代码在我能看到的各种方式看起来都没问题,所以不确定为什么我会得到电子邮件标题的奇怪输出 - 可能是字符编码问题?
非常感谢任何帮助或方向!
谢谢, 迈克尔
答案 0 :(得分:0)
我使用的是DEV版本 - Swift-5.3.1-DEV(因此可能不稳定)。
降级到最新的稳定版本Swift-5.3.0后,我现在可以看到这个问题已经消失。
我没有使用Composer来安装最新的稳定版,而是发现自己使用了SwiftMailer的GitHub帐户中的ZIP。此版本是DEV版本,因此请确保您获得最新的稳定版本:https://github.com/swiftmailer/swiftmailer/releases
希望这有帮助。