FPDF is a class that provides a useful way to deal with PDF documents using dynamic content. Sometimes, according to a special circumstance, also would be valuable to send directly the PDF as attachment e.g. send an invoice automatically after processing a payment.

In this example we use a html message and FPDF / mail() techniques.

<?php
// download fpdf class (http://fpdf.org)
require("/fpd/fpdf.php");

// fpdf object
$pdf = new FPDF();

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");

// email stuff (change data below)
$to = "target@domain.com"; 
$from = "me@domain.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "example.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));


// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

// send message
mail($to, $subject, "", $headers);

?>

I tested the code snippet using my yahoo email account in the $from variable. It sent out an email to my receipient but a copy of the email was not saved in my Sent folder.

Is it possible to save a copy of the email in my Sent folder? If yes, what changes to the snippet must be made?

Thanks.

If you want a copy in your Sent folder you'll need to set up the SMTP authentication with your Yahoo account, only sending messages through Yahoo interface or smtp server will make you able to track sent messages.

The default mail() function lacks some features like SMTP server configuration passing a parameter through this function, to do this, I recommend to use a more functional and advanced class like PHPMailer or Swift Mailer, both are easy to configure and use. On the snippet code, only would be necessary to replace the mail() function call.

So how exactly do you initiate this script? I've edited line 14 and 15 of the file and put it on my server. When I access the file from a browser nothing happens. How does it work exactly?

I'd like to create an HTML form with a bunch of fields and upon clicking submit, have this script generate a PDF with the form data and then email the PDF as an attachment.

Would I set up the form to call this script in the form tag to initiate it? And then how do I modify the script to write the field data?

Sorry, I guess I'm just not understanding how it actually works.

hi, thanks for posting this.

it works great, sends the email and attachment.

when i try to open the pdf attachment, adobe reader says it couldn't open it because it is not a supported file type or the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).

thoughts?

thanks

hi, thanks for posting this.

it works great, sends the email and attachment.

when i try to open the pdf attachment, adobe reader says it couldn't open it because it is not a supported file type or the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).

thoughts?

thanks

<?php
require("/fpd/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
$to = "target@domain.com"; 
$from = "me@domain.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "example.pdf";
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; 
boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; 
name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
mail($to, $subject, "", $headers);
mail($to, $subject, "", $headers);
?>

Hello please your help!!! The code above doesn't work for me. I got these messages in the mail function. What can i do?

Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. in /home/content/f/t/p/ftpaction/html/paginas/send_application.php on line 41

Thanks a lot.

Hi,

I used this script & found following error:


Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable to relay for smartnile@gmail.com in C:\xampp\htdocs\xampp\Tej Consultancy\pdfprac.php on line 80

pls help me???

FPDF is a class that provides a useful way to deal with PDF documents using dynamic content. Sometimes, according to a special circumstance, also would be valuable to send directly the PDF as attachment e.g. send an invoice automatically after processing a payment.

In this example we use a html message and FPDF / mail() techniques.

hii
hey i want to add 20 images in one pdf .and then send it via email as a attachment using php.
can anybody pl help me?

<?php
require("/fpd/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
$to = "target@domain.com"; 
$from = "me@domain.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "example.pdf";
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; 
boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; 
name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
mail($to, $subject, "", $headers);
mail($to, $subject, "", $headers);
?>

hii all.
hey i want to add 20 images in one pdf .and then send it via email as a attachment using php.
can anybody pl help me?

hii
hey i want to add 20 images in one pdf .and then send it via email as a attachment using php.
can anybody pl help me?

Where the code says "generate a simple PDF", below AddPage() method, use the following code:

$pdf->Image('logo-1.gif',10,8,25)

to add an image. Where the first and second number sets position and the last parameter is for the size.

Hi,

I get the mail with an attachement-sign. But when I open the mail, the sign disappears an there is no attachement.

It loocks like the pdf would not be complete.

Joseph

This looks very useful, and I'd love to play around with it. However, although the script says to download the class from fpdf.org, when you get there the only downloads offered are manuals in various languages and ports to any language EXCEPT php.

I use the FPDF class and some extensions to it, and find it very useful. FPDI allows you to import existing PDF files to use as templates and you can add data via standard FPDF functions.

I need to confirm that is really worked?

The original code posted looks like it should work, although is a very basic example and would require a few minor adjustments.

However, although the script says to download the class from fpdf.org, when you get there the only downloads offered are manuals in various languages and ports to any language EXCEPT php

The download page is not very clear, but if you look just below the translated version, you will see links to the various versions. "v1.7" is the latest, and downloads a zip containing the required PHP class files. Here is a direct link to the download (Sorry Mods, if this is agains the rules feel free to remove this link)

this cose send attach but I'didn't send a text messagge.why?

The original code doesn't include and message content.

mail($to, $subject, "", $headers);

To fix this simply add it to this line.

mail($to, $subject, $message, $headers);

---

If anyone has any more questions I'll do my best to help out. If you have trouble with the original code in this post, let me know and I will post some sample code of my own that I know is working.

cheers
/H

Edited 4 Years Ago by |-|x

Thanks for the help. This is great.

However, when I received the email, the attachment wouldn't open because Reader said it was corrupt or not decoded correctly. To make it work I modified everything after line 31 to this:

// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;

//NOTICE I changed $headers to $body!!

$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol; //had one more .$eol

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol; //had one more .$eol

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message NOTICE I replaced "" with $body
mail($to, $subject, $body, $headers);

Eveything is working fine. Credit goes to a post I found here: http://stackoverflow.com/questions/4353271/email-pdf-attachment-with-php-using-fpdf

Thank's for this script.. It sends mail and attached pdf file, but there's problem in getting data from database like name, address and other basic info. I want these data to include into attached file. But I dont know how to do that.. please help.. Any help is highly appreciated.

Thank's
planetjane414 ^_^

Thank you so much powerpotatoe!! I nearly gave up, because i had the same problem and couldn't find the solution. Now it works great! :)

I keep getting the following error from my server. ALERT - mail() - double newline in headers, possible injection, mail dropped

I tried using the above code and it works great with pdf attachment. But my problem is our webserver runs on linux. It does not return any errors but no email is sent either. I tried using PHPMailer the email is sent but no attachment at this time. How can I generate the attachment on the fly and automatically attached it? mail function sadly works for windows only :( Thanks

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.