0

I am trying to create a PDF stream using FPDF library and to send the pdf over e-mail using Swift Mailer. Below is my code. The mail is sent successfully and even pdf is also attached but the pdf is of zero bytes size and so could not be opened. When I download the pdf using $pdf->Output('receipt.pdf', 'D') it is successful and the content of PDF is also present. Can somebody help me to identify whether I am doing something wrong? Or do I need to set some additional fields in the message so that it will send the PDF correctly. I searched all the posts related to FPDF and swiftmailer but could not identify issue in my code.

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Text(80, 30, "Dummy text");
$pdfstream = $pdf->Output('receipt.pdf', 'S');
$attachment = Swift_Attachment::newInstance($pdfstream, 'name.pdf', 'application/pdf');

$message = Swift_Message::newInstance('Dummy subject')
    ->setFrom(array("abc@xyz.com" => 'XYZ'))
    ->setTo('pqr@abc.com')
    ->setBody('Dummy body text');

$message->attach($attachment);

$transport = Swift_MailTransport::newInstance();

$mailer = Swift_Mailer::newInstance($transport);

$result = $mailer->send($message);

EDIT 1: Below is working perfectly fine for me. Please note that I have used two external libraries SwiftMailer and FPDF so you need to do required things like imports etc. to make them work.

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Text(80, 30, "Dummy text");
$data=$pdf->Output('receipt.pdf', 'S');

$message = Swift_Message::newInstance('Subject')
    ->setFrom(array("Geoff" => 'abc@xyz.com'))
    ->setTo('xyz@abc.com')
    ->setBody('This is body text', 'text/html');    
if(!empty($data))
{
  $attachment = Swift_Attachment::newInstance($data, 'pdf_name.pdf', 'application/pdf');
  $message->attach($attachment);
}
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
Mandar Joshi
  • 73
  • 2
  • 8

1 Answers1

1

Try this:

$pdfstream = $pdf->Output('receipt.pdf', 'S');

$emailStream = 'Content-Type: application/pdf;'."\r\n";
$emailStream.= ' name="name.pdf"'."\r\n";
$emailStream.= 'Content-Transfer-Encoding: base64'."\r\n";
$emailStream.= 'Content-Disposition: attachment;'."\r\n";
$emailStream.= ' filename="name.pdf"'."\r\n\r\n";
$emailStream.= chunk_split(base64_encode($pdfstream), 76, "\r\n");

$attachment = Swift_Attachment::newInstance($emailStream, 'name.pdf', 'application/pdf');
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Thanks MonkeyZeus. Actually the encoding is done internally by SwiftMailer so it is not needed explicitly. But while trying this out I realised that I was using wrong variables in my code thats why it was generating blank pdf. Anyway thanks for your suggestion. – Mandar Joshi Apr 30 '14 at 17:31
  • @Mandar What exactly were the wrong variables. I want to do something similar and tried your code, but it still has the bug. Could you possibly supply a corrected version? – Geoff May 13 '14 at 11:00
  • @Geoff edited my question with answer. Let me know what specific problem you are facing. – Mandar Joshi May 13 '14 at 14:37
  • @Mandar I get the following: `Notice: Trying to get property of non-object on line 11` and `Fatal error: Uncaught exception 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.'`. Line 11 is the line `->setFrom` – Geoff May 13 '14 at 18:58
  • @MonkeyZeus I put in your code and when I opened the pdf I got a message to say that the pdf was not properly encoded. – Geoff May 14 '14 at 10:10
  • 3
    @Geoff I have no idea what your code looks like. You should create a new question on StackOverflow and post a code sample if you would like assistance from this website. – MonkeyZeus May 14 '14 at 12:11
  • Change $email->from to the name with which you want to send the email. For example Geoff. Edited in the code. – Mandar Joshi May 20 '14 at 11:25
  • @Mandar When I did that I got `Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [Geoff] does not comply with RFC 2822, 3.6.2.' in C:\xampp\htdocs\ssangyong\lib\classes\Swift\Mime\Headers\MailboxHeader.php on line 352` – Geoff May 21 '14 at 20:02
  • 1
    @MonkeyZeus I have taken your advice and created another question at [link](http://stackoverflow.com/questions/23792608/blank-pdf-attachment-sent-using-fpdf-and-swiftmailer) – Geoff May 21 '14 at 20:06