Php 通过SwiftMailer为text/html和text/plain使用附件

Php 通过SwiftMailer为text/html和text/plain使用附件,php,email,swiftmailer,Php,Email,Swiftmailer,我习惯了 发送电子邮件,我希望有相同的附件: *HTML版本(text/HTML)作为内联图像(cid) *文本版本(Text/plain)作为附件 我正在Ubuntu上用Mozilla Thunderbird 45.3.0测试电子邮件 我一直在玩弄->setBody,->addPart,->embed和->attach方法, 但我总是破坏其中一个版本(即,获取纯文本的电子邮件,或者将邮件视为HTML或文本) 我的测试代码如下(当然带有有效的SMTP地址和文件路径): 函数swiftMail()

我习惯了 发送电子邮件,我希望有相同的附件: *HTML版本(
text/HTML
)作为内联图像(cid) *文本版本(
Text/plain
)作为附件

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

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

我的测试代码如下(当然带有有效的SMTP地址和文件路径):

函数swiftMail(){
需要_once'/vendor/swiftmailer/swiftmailer/lib/swift_required.php';
//创建传输
$transport=Swift\u SmtpTransport::newInstance('smtp.mail.com',25);
//使用创建的传输创建邮件程序
$mailer=Swift\u mailer::newInstance($transport);
//创建消息
$message=Swift\u message::newInstance()
//给信息一个主题
->setSubject('SwiftMailer test/'.uniqid('',true))
//使用关联数组设置发件人地址
->setFrom(数组('first)。name@mail.com“=>”我“)
//使用关联数组将设置为地址
->设置为(数组('first)。name@mail.com“=>”我“);
//将图像作为附件附加
$message->attach(Swift_Attachment::fromPath('./path/to/file.png')->setFilename('cool_image.png');
//将电子邮件正文设置为纯文本
$message->setBody('我的神奇身体在纯文本中','文本/纯文本');
//将电子邮件的HTML部分添加到邮件中
$message->addPart(
'' .
'' .
' ' .
' ' .
“这是一张图片
”。 'embed(Swift\u Image::fromPath('./path/to/file.png')->setFilename('inline\u cool\u Image.png')。'alt=“inline Image”/>
'。 “消息的其余部分”。 ' ' . '', 'text/html'//将内容类型标记为html ); //发送消息 如果(!$mailer->send($message,$failures)) { 回应“失败”:; 打印(失败); } }
下面的代码生成了两个附件(可以很好),并且只有纯文本版本可用(不好)


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

尝试将
->setDisposition('inline')
添加到您的Swift\u图像中

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

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

根据您的示例,它将是:

函数swiftMail(){
需要_once'/vendor/swiftmailer/swiftmailer/lib/swift_required.php';
//创建传输
$transport=Swift\u SmtpTransport::newInstance('smtp.mail.com',25);
//使用创建的传输创建邮件程序
$mailer=Swift\u mailer::newInstance($transport);
//创建消息
$message=Swift\u message::newInstance()
//给信息一个主题
->setSubject('SwiftMailer test/'.uniqid('',true))
//使用关联数组设置发件人地址
->setFrom(数组('first)。name@mail.com“=>”我“)
//使用关联数组将设置为地址
->设置为(数组('first)。name@mail.com“=>”我“);
//将图像作为附件附加
$message->attach(Swift_Attachment::fromPath('./path/to/file.png')->setFilename('cool_image.png');
//将电子邮件正文设置为纯文本
$message->addPart(‘我惊人的身体在纯文本中’、‘text/plain’);
//将电子邮件的HTML部分添加到邮件中
$message->addPart(
'' .
'' .
' ' .
' ' .
“这是一张图片
”。 'embed(Swift\u Image::fromPath('./path/to/file.png')->setFilename('inline\u cool\u Image.png')。'alt=“inline Image”/>
'。 “消息的其余部分”。 ' ' . '', 'text/html'//将内容类型标记为html ); //发送消息 如果(!$mailer->send($message,$failures)) { 回应“失败”:; 打印(失败); } }

使用SwiftMailer 5.4.4版、macOS上的Thunderbird 45.5.0和Gmail web界面进行测试。

感谢@jay bryan cañas已经尝试过了,无论查看选项如何,仍然只显示普通/txt电子邮件。感谢@Boschman的努力,但同样的问题还是发生了。当您解决问题时,您的HTML消息中是否也有嵌入/内联图像?当我删除
$message->embed([…])
部分时,一切都按预期工作。我刚刚测试了您的示例,它对我使用addPart()有效。我使用了SwiftMailer 5.4.4版。我用你的完整示例更新了我的答案。那么,你确认你在plain/txt消息中得到了“我惊人的纯文本身体”,而不是“这是一个图像/n内联图像/n消息的其余部分”吗?刚刚使用最新的SwitMail进行了检查,仍然得到HTML内容的转换,而不是替代文本部分。当我使用示例代码时,我的邮件客户端向我显示了正文的HTML版本(“这是一幅图像…”)。当我使用你的示例代码(与setBody一起)时,我的邮件客户端会向我显示正文的纯文本版本(“我惊人的纯文本正文”)。我想在HTML或普通电子邮件中有不同的内容。HTML的文本转换不符合我的需要。例如,假设我想使用标记内容:*在HTML内容中,pandoc用于生成电子邮件*在文本内容中,原始标记文件与Thunderbird一起使用,当我在原始HTML/纯文本之间切换时,我希望看到不同的内容。这是预期的工作,因为我不包括嵌入式图像。
$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);
    }
}