php - 使用 TinyMCE 格式化并使用 SwiftMailer 发送的电子邮件中不会出现文本格式

标签 php tinymce swiftmailer email-client

我使用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/

相关文章:

html - 如何配置 TinyMCE 以允许 anchor (a) 标记内的 block 级元素?

php - Swiftmailer:按用户 ID 进行 dbReplacements?

php - Laravel Mail::send() 在一个循环中

php - 为什么在 paypal 集成中金额为零?

php - 如何在不破坏环境的情况下在 homestead 中添加一个新的 laravel 站点?

php - 试图使用 php 查找年份中每个日期的平均值?

php - Laravel 在不使用 foreach 循环的情况下发送单独的多封邮件

php - Twitter 搜索 API - since_id 和 max_id 生成更多结果

javascript - 将非按钮控件添加到 TinyMCE 编辑器 4.x

javascript - TinyMCE - 未捕获的异常 : module [5] returned undefined when using local javascript file