gpt4 book ai didi

php - 使用 SwiftMailer 的 text/html 和 text/plain 附件

转载 作者:可可西里 更新时间:2023-11-01 00:56:44 25 4
gpt4 key购买 nike

我正在使用 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 图像源和纯文本电子邮件中的标准附件?

最佳答案

我遇到了完全相同的问题,并且能够通过使用 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 进行了测试。

关于php - 使用 SwiftMailer 的 text/html 和 text/plain 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40213760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com