【问题标题】:Use an attachment for both text/html and text/plain with SwiftMailer使用 SwiftMailer 为 text/html 和 text/plain 使用附件
【发布时间】:2017-03-05 22:56:24
【问题描述】:

我正在使用swiftmailer 发送电子邮件,我希望有相同的附件: * HTML 版本 (text/html) 作为内联图像 (cid) * 文字版(text/plain)作为附件

我正在 Ubuntu 上使用 Mozilla Thunderbird 45.3.0 测试电子邮件。

我一直在玩->setBody->addPart->embed->attach 方法, 但我总是破坏其中一个版本(即以纯文本形式获取电子邮件,我将消息视为 HTML 或文本)。

我的测试代码看起来像(当然有有效的 SMTP 地址和文件路径):

function swiftMail() {
    require_once './vendor/swiftmailer/swiftmailer/lib/swift_required.php';
    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('smtp.mail.com', 25);
    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    // Create the message
    $message = Swift_Message::newInstance()
        // Give the message a subject
        ->setSubject('SwiftMailer test // ' . uniqid('', true))
        // Set the From address with an associative array
        ->setFrom(array('first.name@mail.com' => 'Me'))
        // Set the To addresses with an associative array
        ->setTo(array('first.name@mail.com' => 'Me'));

    // attach the image as attachment
    $message->attach(Swift_Attachment::fromPath('./path/to/file.png')->setFilename('cool_image.png'));

    // set the email's body as plain text
    $message->setBody('My amazing body in plain text', 'text/plain');

    // add the email's HTML part to the message
    $message->addPart(
        '<!doctype html>' .
        '<html>' .
        ' <head><meta charset="UTF-8"></head>' .
        ' <body>' .
        '  Here is an image <br />'.
        '  <img src="' . $message->embed(Swift_Image::fromPath('./path/to/file.png')->setFilename('inline_cool_image.png')) . '" alt="Inline Image" /><br />' .
        '  Rest of message' .
        ' </body>' .
        '</html>',
        'text/html' // Mark the content-type as HTML
    );

    // send the message
    if (!$mailer->send($message, $failures))
    {
        echo "Failures:";
        print_r($failures);
    }
}

以下代码生成两个附件(可能没问题),只有纯文本版本可用(不太好)。

有没有办法让附件用作内联 HTML 图像源和纯文本电子邮件中的标准附件?

【问题讨论】:

    标签: php email swiftmailer


    【解决方案1】:

    尝试将-&gt;setDisposition('inline') 添加到您的 Swift_Image 中

    Swift_Image::fromPath('./path/to/file.png')->setFilename('inline_cool_image.png')->setDisposition('inline')
    

    【讨论】:

    • 感谢@jay-bryan-cañas 已经尝试过了,但无论查看选项如何,仍然只显示纯文本/txt 电子邮件。
    【解决方案2】:

    我遇到了完全相同的问题,并且能够通过使用 addPart() 而不是 setBody() 添加纯文本部分来解决它。

    $message->addPart('My amazing body in plain text', 'text/plain');
    

    根据您的示例,它将是:

    function swiftMail() {
        require_once './vendor/swiftmailer/swiftmailer/lib/swift_required.php';
        //Create the Transport
        $transport = Swift_SmtpTransport::newInstance('smtp.mail.com', 25);
        //Create the Mailer using your created Transport
        $mailer = Swift_Mailer::newInstance($transport);
        // Create the message
        $message = Swift_Message::newInstance()
            // Give the message a subject
            ->setSubject('SwiftMailer test // ' . uniqid('', true))
            // Set the From address with an associative array
            ->setFrom(array('first.name@mail.com' => 'Me'))
            // Set the To addresses with an associative array
            ->setTo(array('first.name@mail.com' => 'Me'));
    
        // attach the image as attachment
        $message->attach(Swift_Attachment::fromPath('./path/to/file.png')->setFilename('cool_image.png'));
    
        // set the email's body as plain text
        $message->addPart('My amazing body in plain text', 'text/plain');
    
        // add the email's HTML part to the message
        $message->addPart(
            '<!doctype html>' .
            '<html>' .
            ' <head><meta charset="UTF-8"></head>' .
            ' <body>' .
            '  Here is an image <br />'.
            '  <img src="' . $message->embed(Swift_Image::fromPath('./path/to/file.png')->setFilename('inline_cool_image.png')) . '" alt="Inline Image" /><br />' .
            '  Rest of message' .
            ' </body>' .
            '</html>',
            'text/html' // Mark the content-type as HTML
        );
    
        // send the message
        if (!$mailer->send($message, $failures))
        {
            echo "Failures:";
            print_r($failures);
        }
    }
    

    在 macOS 和 Gmail 网络界面上使用 SwiftMailer 版本 5.4.4、Thunderbird 45.5.0 进行测试。

    【讨论】:

    • 感谢@Boschman 的努力,但仍然会发生同样的问题。当您解决问题时,您的 HTML 消息中是否还有嵌入/内联图像?当我删除 $message-&gt;embed([...]) 部分时,一切都按预期工作。
    • 我刚刚测试了您的示例,它适用于我的 addPart()。我使用了 SwiftMailer 5.4.4 版。我用您的完整示例更新了我的答案。
    • 那么您是否确认您在纯文本/txt 消息中得到“我的纯文本中的惊人身体”,而不是“这是一个图像/n 内联图像/n 其余消息”?刚刚检查了最新的 switfmailer,仍然得到 HTML 内容的转换,而不是替代文本部分。
    • 当我使用我的示例代码时,我的邮件客户端向我显示了正文的 html 版本(“这是一张图片……”)。当我使用您的示例代码(使用 setBody)时,我的邮件客户端会向我显示正文的纯文本版本(“我的纯文本惊人正文”)。
    • 好吧,也许我误解了我想要做什么。我想在 HTML 或 PLAIN 电子邮件中有不同的内容。 HTML 的文本转换不符合我的需要。例如,假设我要使用markdown内容: * HTML内容中,pandoc用于生成电子邮件 * TEXT内容中,原始markdown文件使用Thunderbird,当我在Original HTML/Plain Text之间切换时,我想看看不同的内容。就我不包含嵌入图像而言,这是按预期工作的。
    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多