generate a PDF and send as attachment

martin5211 0 Tallied Votes 22K Views Share

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); ?>
Member Avatar for noycasin
noycasin0 Newbie Poster

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.

Member Avatar for martin5211
martin521137 Posting Whiz in Training

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.

Member Avatar for ludachris
ludachris0 Newbie Poster

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.

Member Avatar for rahsab
rahsab0 Newbie Poster

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

Member Avatar for campl
campl0 Newbie Poster

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

Member Avatar for amamajid
amamajid0 Newbie Poster
<?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); ?>
Member Avatar for jimmy2010
jimmy20100 Newbie Poster

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.

Member Avatar for smartnile
smartnile0 Newbie Poster

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???

Member Avatar for cjforu
cjforu0 Newbie Poster

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?

Member Avatar for cjforu
cjforu0 Newbie Poster
<?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?

Member Avatar for martin5211
martin521137 Posting Whiz in Training

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.

Member Avatar for Jsail
Jsail

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

Member Avatar for sakk
sakk0 Newbie Poster

Thankyou sooo very much...You saved my day...... :))))))))))))

Member Avatar for imranjavedpk
imranjavedpk0 Newbie Poster

I need to confirm that is really worked?

Member Avatar for jpdeleon
jpdeleon0 Newbie Poster

that's cool

Member Avatar for fiorentinese
fiorentinese0 Newbie Poster

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

Member Avatar for tiggsy
tiggsy4 Junior Poster

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.

Member Avatar for |-|x
|-|x126 Junior Poster in Training

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

Member Avatar for powerpotatoe
powerpotatoe0 Newbie Poster

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

Member Avatar for planetjane414
planetjane4140 Newbie Poster

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 ^_^

Member Avatar for justinos
justinos0 Newbie Poster

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! :)

Member Avatar for mrk5105
mrk51050 Newbie Poster

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

pritaeas2,211 ¯\_(ツ)_/¯ ModeratorFeatured Poster

Your mail server doesn't like double newlines. So change the header code by removing the doubles.

Member Avatar for dsupladita
dsupladita0 Newbie Poster

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

Member Avatar for sangamesh_modi
sangamesh_modi0 Newbie Poster

hey dude i want to attach new pdf using this code can u help me?

Member Avatar for mmddev
mmddev0 Newbie Poster

Any clues on how to add a CC or BCC to the code. I have tried adding it as $header .= "BCC: ".$replyto."\r\n";
With negative results..but the primary email does deliver.

Dani4,558 The Queen of DaniWeb AdministratorFeatured PosterPremium Member

Are you doing $header .= or $headers .=?

Try adding right after the Content-Type line on line 36 of the code.

Also, instead of \r\n use $eol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

close