How to send a PDF created by TCPDF as a Swiftmailer attachment

I have already tried several solutions, the closest (for me) should look like this:

$file = $pdf->Output('', 'E'); $message->attach(Swift_Attachment::newInstance($file, 'name.pdf', 'application/pdf')); 

$pdf is an instance of TCPDF and $message is an instance of Swift_Message . Using the above, the message is sent normally, the file is connected, but when I try to open it, I get an error message that the file is damaged or badly encoded.

My question is: how to send the PDF created by TCPDF as an Swiftmailer attachment without saving the file on the server and deleting it after sending the email . Here is a link to the documentation of the TCPDF output method, maybe someone can see something that I missed.

+7
source share
3 answers

I use something like this and it works. For PDF content, I use one of the simplest examples in the PDF library.

 [...] $pdf_as_string = $pdf->Output('', 'S'); // $pdf is a TCPDF instance [...] $transport = Swift_MailTransport::newInstance(); // using php mail function $message->setTo(array( " client@customdomain.com " => "Main Email", " client@publicdomain.com " => "Secondary Email" )); $message->setSubject("This email is sent using Swift Mailer"); $message->setBody("You're our best client ever."); $message->setFrom(" developers@mydomain.com ", "Developers United"); $attachment = Swift_Attachment::newInstance($pdf_as_string, 'my-file.pdf', 'application/pdf'); $message->attach($attachment); [...] 

Perhaps this answer comes a little later, since I use swiftmailer v4_3_0 and TCPDF v6_0_002. But just in case, it's worth someone.

+8
source

I had no problems connecting TCPDF on the fly.

I call a function that eventually returns a PDF using the output type 'S':

return $pdf->Output('TE_Invoice.pdf', 'S');

I attach the file using:

 $message->attach(Swift_Attachment::newInstance() ->setFilename('TE_Invoice.pdf') ->setContentType('application/pdf') ->setBody($val['file'])); 

Where $val['file'] is the return value from above.

I am using TCPDF version: 5.9.134 and Swift Mailer version: 4.1.3

+4
source

Have you tried this?

 $file = $pdf->Output('', 'S'); 

I am doing this with another mail server in PHP and it really works. I assume that the mail backend takes care of encoding the attachment, so there is no need to encode it as base64 manually.

0
source

All Articles