1,105,281 Community Members

generate a PDF and send as attachment

Member Avatar
Reputation Points: 37 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 0 [?]
 
0
 

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
noycasin
Newbie Poster
1 post since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 37 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 0 [?]
 
0
 

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
ludachris
Newbie Poster
2 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
rahsab
Newbie Poster
1 post since Aug 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
campl
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
amamajid
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
<?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
jimmy2010
Newbie Poster
1 post since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
smartnile
Newbie Poster
1 post since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
cjforu
Newbie Poster
2 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
cjforu
Newbie Poster
2 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
<?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
martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 37 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Jsail
Newbie Poster
1 post since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
sakk
Newbie Poster
1 post since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
imranjavedpk
Newbie Poster
2 posts since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need to confirm that is really worked?

Member Avatar
jpdeleon
Newbie Poster
5 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

that's cool

Member Avatar
fiorentinese
Newbie Poster
1 post since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
tiggsy
Junior Poster
132 posts since Jul 2009
Reputation Points: 4 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 2 [?]
 
0
 

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
|-|x
Posting Whiz
353 posts since Apr 2008
Reputation Points: 113 [?]
Q&As Helped to Solve: 56 [?]
Skill Endorsements: 8 [?]
 
0
 

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
powerpotatoe
Newbie Poster
1 post since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
Post:
Start New Discussion
Tags Related to this Article