Email PDF Attachment with PHP Using Fpdf

Email PDF Attachment with PHP Using FPDF

This ended up working for me:

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

$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/image.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');

// email stuff (change data below)
$to = "myemail@example.com";
$from = "me@example.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 = "test.pdf";

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

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

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$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;

// 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
mail($to, $subject, $body, $headers);

?>

How to generate more than one PDF and send through email attachment using PHP?

Try using 'S' parameter istead of 'F'

$pdf1 = new FPDF();
$pdf1->AddPage();
$pdf1->SetFont('Arial', 'B', 16);
$pdf1->Cell(40, 10, 'Hello World1!');
$file1 = ".assets/temp/download1.pdf";
$pdf1content = $pdf1->Output('S');
file_put_contents($file1, $pdf1content);

$pdf2 = new FPDF();
$pdf2->AddPage();
$pdf2->SetFont('Arial', 'B', 16);
$pdf2->Cell(40, 10, 'Hello World2!');
/**/
$file2 = ".assets/temp/download2.pdf";
$pdf2content = $pdf2->Output('S');
file_put_contents($file2, $pdf2content);

// use phpmailer to send email.
// load attachments and messages.
// send unset files.

see http://www.fpdf.org/en/doc/output.htm

I want to email a PDF as an attachment that was created using FPDF. but the attachment never comes through

Found the solution and its working very fine,change the above code as follows

  // email stuff (change data below)
$to = "saxxxx@gmail.com";
$from = "aneexxxxx@gmail.com";
$subject = "Patient detials 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 = "patientdetials.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;

//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);
?>

Sending an email with a PDF Files attachment using PHP

// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
."Email: " .$_POST['email'] . "\n"
."Phone: " .$_POST['telephone'] . "\n"
."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
."Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffemail";

mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)

//once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
// creating a pdf file
$pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Title");
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Name:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['name']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Email:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['email']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Phone:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['telephone']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(40, 10, "Number of Guests:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(40, 10, $_POST['numberOfGuests']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(40, 10, "Date Of Reunion:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(40, 10, $_POST['date']);
// if file doesn't exists or if it is writable, create and save the file to a specific place
if(!file_exists($pdf_filename) || is_writable($pdf_filename)){
$pdf->Output($pdf_filename, "F");
} else {
exit("Path Not Writable");
}

// using the phpmailer class
// create a new instance called $mail and use its properties and methods.
$mail = new PHPMailer();
$staffEmail = "staffemail";
$mail->From = $staffEmail;
$mail->FromName = "name";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo($staffEmail, "name");

$mail->AddAttachment($pdf_filename);
$mail->Subject = "PDF file attachment";

$mail->Body = "message!";

// if mail cannot be sent, diplay error message
//if(!$mail->Send()){
//echo "<div id=\"mailerrors\">Message could not be sent</div>";
//echo "<div id=\"mailerrors\">Mailer Error: " . $mail->ErrorInfo . "</div>";
//} else { // else...if mail is sent, diplay sent message
//echo "<div id=\"mailerrors\">Message sent</div>";
//}

// delete the temp file
unlink($pdf_filename);
}
}

Not able to attach the generated PDF in Php using FPDF

You have left unclear which Phpmailer version you're using and which FPDF version, therefore it is hard to say what the error exactly is.

On first glance this hit my attention:

$pdfString = $pdf->output();

If the intend is to have $pdf->output() to return a string, then the invocation looks wrong to me.

That is because the invocation as $pdf->output() is doing the default output, which is sending to the browser.

Which you said is working, and that is already the line above:

$pdf->output();
$pdfString = $pdf->output();

PHP is an imperative language, that means, it does not make a difference to the method call whether or not you assign a variable to the return value.

So both method calls will behave the same:

$pdf->output();
$pdfString = $pdf->output();

It's like writing

$pdf->output();
$pdf->output();

Perhaps just an oversight in the heat of the debugging. For debugging, check the preconditions early, verify your script step-by-step. Don't ask longer than five minutes. Just verify then.

Potential fix:

$pdfString = $pdf->output('S');

may do it, better check with the documentation you have at hand for your version in use.

Is it possible to generate and send a pdf file via email without saving the pdf file locally or on a server

FPDF has a output function, which can generate the encoded String.
See http://www.fpdf.org/en/doc/output.htm

And PHPMailer can add Attachments from Strings.

So its

$content = $pdf->Output('S');
$email->addStringAttachment($content, 'my.pdf');

That simple.

php send e-mail with PDF attachment

You can use PHPMailer with FPDF . It works properly without any hassle. You need to change parameter for $pdf->Output . Download and copy class.phpmailer.php and PHPMailerAutoload.php to your work folder. Attach class.phpmailer.php below or above require('html2pdf.php'); . I have done this before so this will work. According to your code this should work.

function send_pdf_to_user(){
if($_REQUEST['action'] == 'pdf_invoice' ){
require('html2pdf.php');
require_once('class.phpmailer.php');
$pdf=new PDF_HTML();
$pdf->SetFont('Arial','',11);
$pdf->AddPage();

$text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
if(ini_get('magic_quotes_gpc')=='1')
$text=stripslashes($text);
$pdf->WriteHTML($text);

$mail = new PHPMailer(); // defaults to using php "mail()"
$body = "This is test mail by monirul";

$mail->AddReplyTo("webmaster@test.ch","Test Lernt");
$mail->SetFrom('webmaster@test.ch', 'Test Lernt');

$address = "monirulmask@gmail.com";
$mail->AddAddress($address, "Abdul Kuddos");
$mail->Subject = "Test Invoice";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);
//documentation for Output method here: http://www.fpdf.org/en/doc/output.htm
$pdf->Output("Test Invoice.pdf","F");
$path = "Walter Lernt Invoice.pdf";

$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
global $message;
if(!$mail->Send()) {
$message = "Invoice could not be send. Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Invoice sent!";
}

}
}


Related Topics



Leave a reply



Submit