如何创建 swiftmailer 消息并将其附加到另一条消息

How to create a swiftmailer message and attach it to another message

我们在发送电子邮件的功能之一中使用了 SwiftMailer。 最后,有一个总报告发送给部门经理,以便跟踪并了解功能使用情况。

经理现在也愿意收到已发出的电子邮件副本,我们可以通过添加一封电子邮件副本作为电子邮件报告的附件来实现。

关于我们如何创建 swiftmailer 消息而不发送它,仅将其用作新 swiftmailer 电子邮件的附件的任何想法。

您可以像这样将 Swift_Message 添加为附件:

$attachment = new \Swift_Attachment($messageAttachment, 'some-email.txt', 'text/plain');

但是,您的电子邮件现在将作为包含所有邮件详细信息的 .txt 文件附加。我不确定这是否是预期的行为!?

完整示例:

$messageAttachment = (new \Swift_Message('Attached Email'))
    ->setFrom('yourmail@gmail.com')->setTo('yourmail@gmail.com')
    ->setBody("Attached Email Body", 'text/plain');
$attachment = new \Swift_Attachment($messageAttachment, 'some-email.txt', 'text/plain');

$message = (new \Swift_Message('Real Email'))
    ->setFrom('yourmail@gmail.com')->setTo('yourmail@gmail.com')
    ->setBody("Real Email Body", 'text/plain')
    ->attach($attachment);
$mailer->send($message);

附件some-email.txt的内容如下所示:

Message-ID: <568d128d97e530d1389cb83b154d64eb@swift.generated>
Date: Tue, 05 Dec 2017 17:00:05 +0100
Subject: Attached Email
From: yourmail@gmail.com
To: yourmail@gmail.com
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Attached Email Body