Send Email with PDF attachment

Send Email with PDF attachment


Posted in : PHP Posted on : January 19, 2011 at 6:17 PM Comments : [ 0 ]

This section contains the detail about the Sending Email with PDF attachment

Send Email with PDF attachment

In this tutorial you will learn how to send email with PDF attachment in a PHP web application. The code of  "emai-with-pdf-attachment.php" given below :

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

class SimpleTable extends FPDF
{
function generateTable($no)
{

for($i=1;$i<=10;$i++)
{
$this->cell(20,10,$no,1,0,"C");
$this->cell(20,10," * ".$i,1,0,"C");
$this->cell(20,10," = ".$i*$no,1,1,"C");
}
}
}

$pdf=new SimpleTable();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->generateTable(2);
// open pdf file
//$pdf->Output();

// Filename that will be used for the file as the attachment
$fileatt_name = "test.pdf";
$dir='pdffile/';
// save pdf in directory
$pdf ->Output($dir.$fileatt_name);

//....................

$data = $pdf->Output("", "S");

//..................


$email_from = "roseindia@gmail.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_to = "b1@localhost"; // Who the email is to


$semi_rand = md5(time());
$data = chunk_split(base64_encode($data));

$fileatt_type = "application/pdf"; // File Type
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

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

// set email message......................
$email_message = "Thanks for visiting ";
$email_message .= "Thanks for visiting.<br>";// Message that the email has in it
$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";
$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";

$sent = @mail($email_to, $email_subject, $email_message, $headers);
if($sent) {
echo "Your email attachment send successfully.";
} else {
die("Sorry but the email could not be sent. Please try again!");
}
?>

In this tutorial send email with PDF attachment and PDF save in "pdffile" folder.

Output :

When  run "emai-with-pdf-attachment.php" then generate PDF and save in  "pdffile" folder and send email with PDF attachment. If email send successfully display message as :

If open "pdffile" folder display save PDF as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics