How to attach PDF to email using Swiftmailer in Symfony2

14,892

Solution 1

If you want to upload a file from buffer, you can do this:

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

Solution 2

You can add your attachement using this line:

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

Solution 3

The following code should do it:

$attachment = \Swift_Attachment::fromPath('subpath/to/attachment');
$message->attach($attachment);
Share:
14,892
Krishna Ghodke
Author by

Krishna Ghodke

I'm a Java/PHP developer from Pune(India) with more than 7 years of experience. StackOverflow Enthusiast! I also have some experience in Android & AngularJS

Updated on June 25, 2022

Comments

  • Krishna Ghodke
    Krishna Ghodke almost 2 years

    I am sending an email using swiftmailer in symfony2, but I would like to add a specified PDF file as a file attachment to the email. How would I do that?

    Here is my current code:

    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('[email protected]')
        ->setTo('[email protected]')
        ->setBody(
            $this->renderView(
                'HelloBundle:Hello:email.txt.twig',
                array('name' => $name)
            )
        )
    ;
    $this->get('mailer')->send($message);