使用SwiftMailer为text/html和text/plain都添加附件

3
我正在使用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图像源并作为普通附件在纯文本电子邮件中使用?
2个回答

6

我遇到了完全相同的问题,并且通过使用addPart()而不是setBody()来添加纯文本部分进行修复。

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

Based on your example, it would be:

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);
    }
}

Tested with SwiftMailer version 5.4.4, Thunderbird 45.5.0 on macOS and the Gmail web interface.


感谢您的努力,@Boschman,但问题仍然存在。当您解决了您的问题时,您的HTML消息中是否也有嵌入/内联图像?当我删除$message->embed([...])部分时,一切都按预期工作。 - ponsfrilus
我刚刚测试了你的示例,使用addPart()方法可以正常工作。我使用的是SwiftMailer版本5.4.4。我已经更新了我的答案并添加了完整的示例代码。 - Boschman
你确认你在plain/txt消息中收到的是“My amazing body in plain text”而不是“Here is an image /n Inline image /n Rest of message”吗?我刚用最新的switfmailer检查了一下,仍然得到了HTML内容的转换,而不是替代文本部分。 - ponsfrilus
当我使用我的示例代码时,我的邮件客户端会显示正文的HTML版本(“这里有一张图片...”)。当我使用您的示例代码(使用setBody)时,我的邮件客户端会显示正文的纯文本版本(“我的惊人正文”)。 - Boschman
好的,也许我没有解释清楚我想做什么。我希望在HTML或纯文本电子邮件中有不同的内容。HTML的文本转换不符合我的需求。例如,假设我想使用Markdown内容:
  • 在HTML内容中,使用pandoc生成电子邮件
  • 在TEXT内容中,使用原始的Markdown文件 对于Thunderbird,当我在原始HTML /纯文本之间切换时,我希望看到不同的内容。只要我不包含嵌入式图像,这正常工作。
- ponsfrilus

0

尝试在Swift_Image中添加->setDisposition('inline')

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

感谢@jay-bryan-cañas已经尝试过了,但无论查看选项如何,仍然只显示纯文本电子邮件。 - ponsfrilus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接