如何在Symfony2中使用Swiftmailer将PDF附加到电子邮件

7
我将使用symfony2中的swiftmailer发送邮件,但我想将指定的PDF文件作为附件添加到电子邮件中。我该如何做?
以下是我的当前代码:
$message = \Swift_Message::newInstance()
    ->setSubject('Hello Email')
    ->setFrom('send@example.com')
    ->setTo('recipient@example.com')
    ->setBody(
        $this->renderView(
            'HelloBundle:Hello:email.txt.twig',
            array('name' => $name)
        )
    )
;
$this->get('mailer')->send($message);
4个回答

11

使用Swift Mailer将文档附加到电子邮件有几种选项。

来自Symfony文档:

    $message = Swift_Message::newInstance()
      ->setFrom('from@example.com')
      ->setTo('to@example.com')
      ->setSubject('Subject')
      ->setBody('Body')
      ->attach(Swift_Attachment::fromPath('/path/to/a/file.zip'))
    ;

$this->getMailer()->send($message);

我必须在setBody()之前写attach()。 - Jorgeeadan

6
如果您想从缓冲区上传文件,可以这样做:
$attach=getPdfFunction(); //binary
Swift_Attachment::newInstance($attach, 'document.pdf','application/pdf');

5
您可以使用以下代码添加附件:

您可以使用以下代码添加附件:

$message->attach(\Swift_Attachment::fromPath($attach));

2
以下代码应该可以实现:
$attachment = \Swift_Attachment::fromPath('subpath/to/attachment');
$message->attach($attachment);

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