7

I 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 above the email is being sent ok, file is attached but when I try to open it I get the error message that file is corrupted or badly encoded.

My question is: how to send pdf generated by TCPDF as Swiftmailer attachment without saving the file to server and deleting it after sending the email. Here is the link to the TCPDF output method documentation, maybe somebody can see something I have missed.

matino
  • 17,199
  • 8
  • 49
  • 58

4 Answers4

8

I am using something like this and it is working. For the PDF content I am using one of the simplest examples on 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);
[...]

Maybe this answer comes a little late since I am using swiftmailer v4_3_0 and TCPDF v6_0_002. But just in case is worth to someone.

4

I have had no problems attaching TCPDFs on the fly.

I call a function which eventually returns the 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 returned value from above.

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

Jon
  • 41
  • 1
  • That's how I tried to do it but after I opend pdf in email I get the error message that file is corrupted or badly encoded... – matino Nov 23 '11 at 09:37
1

You can use outputmode 'E' to get base64String.

$base64PdfString = $pdf->Output('', 'E');

Beware: Maybe you have to cut the first 5-6 lines, because of

Content-Type: application/pdf; name=""
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=""

Base64StringStartsHere....

cut

$base64PdfArray = explode("\r\n", $base64PdfString);
$base64 = '';
for($i = 5; $i < count($base64PdfArray); $i++) {
    $base64 .= $base64PdfArray[$i];
}

Now you have the Email as base64String.
Before sending you have to decode it.

$mail->attach(new \Swift_Attachment(base64_decode($base64), 'Pdf.pdf', 'application/pdf'));
Gollm
  • 120
  • 5
0

Did you try this?

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

I'm doing this with another mail backend in PHP, and this does work. I guess the mail backend takes care of encoding the attachment, so there is no need to encode it as base64 manually.

sij_a
  • 633
  • 5
  • 10
Markus
  • 5,667
  • 4
  • 48
  • 64
  • Tried this already, tried again and still doesn't work (same error msg) – matino Nov 09 '11 at 12:11
  • If you save the file to the filesystem, can you open it? If yes, then it is a swiftmailer-fault. Then you at least know where to look further. – Markus Nov 09 '11 at 12:53
  • If I save the file and attach it with $message->attach(Swift_Attachment::fromPath($path)); then everything's ok – matino Nov 09 '11 at 13:04
  • sorry, then i cannot help you. I know nothing about Swiftmailer, I'm using http://pear.php.net/Mail_Mime, and it can successfully add an attachment with `$mime->addAttachment($file, 'application/pdf', 'name.pdf')` generated by `$file = $pdf->Output('', 'S')` ... no big help four you, though. – Markus Nov 09 '11 at 15:54