【问题标题】:How to attach PDF to email using Swiftmailer in Symfony2如何在 Symfony2 中使用 Swiftmailer 将 PDF 附加到电子邮件
【发布时间】:2014-09-26 15:08:24
【问题描述】:

我在 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);

【问题讨论】:

    标签: php email symfony swiftmailer


    【解决方案1】:

    您有多种选择可以使用 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()
    【解决方案2】:

    如果你想从缓冲区上传文件,你可以这样做:

    $attach=getPdfFunction(); //binary
    Swift_Attachment::newInstance($attach, 'document.pdf','application/pdf');
    

    【讨论】:

      【解决方案3】:

      您可以使用此行添加附件:

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

      【讨论】:

        【解决方案4】:

        下面的代码应该做到这一点:

        $attachment = \Swift_Attachment::fromPath('subpath/to/attachment');
        $message->attach($attachment);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-13
          • 2012-05-23
          • 1970-01-01
          • 1970-01-01
          • 2014-07-01
          • 1970-01-01
          • 2016-01-18
          相关资源
          最近更新 更多