【问题标题】:How to create a swiftmailer message and attach it to another message如何创建 swiftmailer 消息并将其附加到另一条消息
【发布时间】:2017-12-05 08:25:17
【问题描述】:

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

经理现在也愿意接收发送的电子邮件副本,我们可以通过将电子邮件副本添加为电子邮件报告的附件来做到这一点。

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

【问题讨论】:

    标签: php symfony email swiftmailer


    【解决方案1】:

    您可以像这样将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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多