我使用SwiftMailer在我的 PHP 脚本中发送电子邮件,我使用 TinyMCE作为文本编辑器来编写和格式化消息正文。问题是,当我发送消息时,它在所有电子邮件客户端(gmail、yahoo 和 hotmail)中都没有任何格式显示,甚至链接也没有显示为链接,它们显示为普通文本,但呈蓝色。那么问题出在哪里呢?
这是我用来发送电子邮件的代码:
<?php
require_once 'path/to/SwiftMailer/lib/swift_required.php';
$transport = Swift_MailTransport::newInstance();
# Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
# Create the message
$msg = Swift_Message::newInstance();
# Give the message a subject
$msg->setSubject($_POST['subject']);
# Set the From address with an associative array
$msg->setFrom(array($_POST['sender_email'] => $_POST['sender_name']));
# Give it a body
$msg->setBody($_POST['message'], 'text/html');
$failedRecipients = array();
$numSent = 0;
$to = array(
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cfee9efe5fce5e9e2f8d3bdccebe1ede5e0a2efe3e1" rel="noreferrer noopener nofollow">[email protected]</a>',
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0829593998099959e84afc2b08991989f9fde939f9d" rel="noreferrer noopener nofollow">[email protected]</a>' => 'Recipient 2',
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671502040e170e0209133854270f08130a060e0b4904080a" rel="noreferrer noopener nofollow">[email protected]</a>',
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91e3f4f2f8e1f8f4ffe5cea5d1f6fcf0f8fdbff2fefc" rel="noreferrer noopener nofollow">[email protected]</a>' => 'Recipient 4',
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f98b9c9a9089909c978da6ccb98098919696d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>'
);
foreach ($to as $address => $name) {
if (is_int($address)) {
$msg->setTo($name);
} else {
$msg->setTo(array($address => $name));
}
$numSent += $mailer->send($msg, $failedRecipients);
}
echo $numSent > 0 ? 'SUCCESS' : 'FAILURE';
?>
请注意,$_POST['message']
包含我使用 TinyMCE 编写和格式化的消息正文。
最佳答案
我尝试了很多方法,直到弄清楚,问题是格式化消息正文中的特殊字符被转义(每个特殊字符前面添加了反斜杠)我不知道为什么,我不知道他们中的哪一个(TinyMCE 或 SwiftMailer)执行了此操作。因此,我需要做的就是在通过 SwiftMailer 发送消息之前从消息正文中去掉斜杠。我只需要更改这一行:
$msg->setBody($_POST['message'], 'text/html');
成为:
$msg->setBody(stripslashes($_POST['message']), 'text/html');
关于php - 使用 TinyMCE 格式化并使用 SwiftMailer 发送的电子邮件中不会出现文本格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12350742/