How to connect PDF to email using Swiftmailer in Symfony2

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

Here is my current code:

$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); 
+6
source share
4 answers

You have several options for attaching a document to email using quick mail.

From the symfony doc:

  $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); 
+9
source

You can add your attachment using the following line:

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

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

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

The following code should do this:

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

Source: https://habr.com/ru/post/973305/


All Articles