PHP - how do i attach pdf file to my mail - Asked By Olusegun A. on 06-Oct-09 06:06 PM

Hello Success-Oriented People,

I am designing a retail-site and my client want an automated receipt to be generated and attached to the autoresponder mail that his customers will receive after every purchase. I know how to code an autoresponder mail to the customers but i don't know how to generate a receipt covering the purchase in PDF format and attach it to the mail. Please help

Regards,
Olusegun

attach PDF File - Venkat K replied to Olusegun A. on 06-Oct-09 10:12 PM

<?php
require('fpdf/fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdfcontent = $pdf->Output("helloworld.pdf", "S");

require_once('Mail.php');
require_once('Mail/mime.php');

// email address of the recipient
$to = "youremail@yahoo.com";

// email address of the sender
$from = "senderemail@yahoo.com";

// subject of the email
$subject = "Hello world from coolersport";

// email header format complies the PEAR's Mail class
// this header includes sender's email and subject
$headers = array('From' => $from,
'Subject' => $subject);

// We will send this email as HTML format
// which is well presented and nicer than plain text
// using the heredoc syntax
// REMEMBER: there should not be any space after PDFMAIL keyword
$htmlMessage = <<<PDFMAIL
<html>
<body bgcolor="#ffffff">
<p align="center">
Please find the pdf attached in the email.<br>
This is generated by <b style="font-size:18pt;">FPDF</b>
</p>
</body>
</html>
PDFMAIL;

// create a new instance of the Mail_Mime class
$mime = new Mail_Mime();

// set HTML content
$mime->setHtmlBody($htmlMessage);

// IMPORTANT: add pdf content as attachment
$mime->addAttachment($pdfcontent, 'application/pdf', 'helloworld.pdf', false, 'base64');

// build email message and save it in $body
$body = $mime->get();

// build header
$hdrs = $mime->headers($headers);

// create Mail instance that will be used to send email later
$mail = &Mail::factory('mail');

// Sending the email, according to the address in $to,
// the email headers in $hdrs,
// and the message body in $body.
$mail->send($to, $hdrs, $body);

?>
Thanks,

reply - Deepak Sonawane replied to Olusegun A. on 07-Oct-09 12:36 AM

Hi,
You can create pdf file in php as follows:

Generate PDF from within your PHP script.

A number of libraries enable you to create PDF using PHP. The standard PHP documentation includes a PDF Functions section that describes the popular PDFlib module (http://www.pdflib.com). However, this PDF extension is not free software. Typically, you must purchase a license and then recompile PHP to take advantage of these functions.

Consider some of these free alternatives. They are native PHP, so they are easy to install; just include one in your script.

6.15.1 R&OS PDF-PHP

With the R&OS PDF-PHP library (http://www.ros.co.nz/pdf/), you can add text, bitmaps, and drawings to new PDF pages. Formatting includes running headers and footers, multicolumn layout, and tables. PDF features include page labels, links, and encryption. Programming features include callbacks and transactions.

<?php // hello world with R&OS, from readme.pdf

include ('class.ezpdf.php');



$pdf =& new Cezpdf( );

$pdf->selectFont('./fonts/Helvetica.afm');

$pdf->ezText('Hello World!', 50);

$pdf->ezStream( );

?>

6.15.2 FPDF

FPDF (http://www.fpdf.org) enables you to add text, bitmaps, lines, and rectangles to new PDF pages. Formatting includes running headers and footers, multicolumn layout, and tables. PDF features include metadata and links. The home page provides an active user forum and user-contributed scripts. The following PHP script produces the PDF document shown in Figure 6-17.

Figure 6-17. Dynamically generated PDF from FPDF
figs/pdfh_0617.gif

<?php // hello world with FPDF, adapted from the tutorial for IndigoPerl users

define('FPDF_FONTPATH','C:\\indigoperl\\apache\\htdocs\\pdf_hacks\\fpdf\\font\\');

require('C:\\indigoperl\\apache\\htdocs\\pdf_hacks\\fpdf\\fpdf.php');



$pdf=new FPDF( );

$pdf->AddPage( );

$pdf->SetFont('Arial','B',16);

$pdf->Cell(40,10,'Hello World!');

$pdf->Output( );

?>

6.15.3 pdf4php

pdf4php (http://www.gnuvox.com/pdf4php/) provides basic operations for creating PDFs. Add text, JPEG bitmaps, lines, and rectangles to new PDF pages. PDF features include compression. The PHP library file size is smaller, so runtime parsing goes faster.

<?php // hello world with pdf4php, adapted from the home page

include('pdf4php.php');



$pdf=new PDFClass( );

$pdf->startPage(8.5 * 72, 11 * 72);

$pdf->SetFont(48, 'Helvetica');

$pdf->SetStrokeColor(1,0,0);

$pdf->DrawTextAt(4.25*72, 45, "Hello World!", ALIGN_CENTER);

$pdf->endPage( );

$pdf->end( );

?>

6.15.4 phppdflib

phppdflib (http://www.potentialtech.com/ppl.php) provides basic operations for creating PDFs. You can add text, bitmaps, lines, rectangles, and circles to new PDF pages. Special features include templates.

<?php // hello world with phppdflib, adapted from example.php

require('phppdflib.class.php');



$pdf=new pdffile;

$pdf->set_default('margin', 0);

$firstpage=$pdf->new_page("letter");

$pdf->draw_text(10, 100, "Hello World!", $firstpage);

$temp=$pdf->generate( );

header("Content-type: application/pdf");

header("Content-length: '.strlen($temp));

echo $temp;

?>
Sending email with attachment in php:
<?php
$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment

$email_from = ""; // Who the email is from
$email_subject = ""; // The Subject of the email
$email_txt = ""; // Message that the email has in it

$email_to = ""; // Who the email is too

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>

 
Y replied to Deepak Sonawane on 20-Jul-10 08:44 PM
Slight correction, the lines that read:
 "Content-Transfer-Encoding: 7bit\n\n" . 
$email_message . "\n\n";
Should be:
 "Content-Transfer-Encoding: 7bit\n\n" . 
$email_txt. "\n\n";