Creating a PDF and sending by email
I am looking at the ablity for users to enter some information into a form and send it via mail()
.
What I'd like is for the details to be sent as a PDF attachment. Possibly with the name of the the send开发者_开发技巧er and a date/time. test_user_06052011.pdf
I have a design for the PDF, but I'm not sure how I'd integrate this design to create a PDf in PHP.
Does anyone have an examples or ways in which I could do this?
Very easy way to create PDF server-site is using wkhtmltopdf. However, you will need a shell access to server to set it up.
To create PDF you need two files: one is PHP which generates HTML you want to convert into PDF. Let's say this is invoice.php:
<?php
$id = (int) $_GET['id'];
?>
<h1>This is invoice <?= $id ?></h1>
<p>some content...</p>
And the other one, which will fetch the invoice and convert it into PDF using wkhtmltopdf:
<?php
$tempPDF = tempnam( '/tmp', 'generated-invoice' );
$url = 'http://yoursite.xx/invoice.php?id=123';
exec( "wkhtmltopdf $url $tempPDF" );
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=invoice.pdf');
echo file_get_contents( $tempPDF );
unlink( $tempPDF );
Once you have created a PDF file you can also send mail with attachment this way:
<?php
$to = "abc@gmail.com";
$subject = "mail with attachment";
$att = file_get_contents( 'generated.pdf' );
$att = base64_encode( $att );
$att = chunk_split( $att );
$BOUNDARY="anystring";
$headers =<<<END
From: Your Name <abc@gmail.com>
Content-Type: multipart/mixed; boundary=$BOUNDARY
END;
$body =<<<END
--$BOUNDARY
Content-Type: text/plain
See attached file!
--$BOUNDARY
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="your-file.pdf"
$att
--$BOUNDARY--
END;
mail( $to, $subject, $body, $headers );
Can't find reason for using native mail()
function today. In mostly trivial situations we can use PHPMailer library, which in OOP style gives us opportunity to send emails even without understanding of header.
The solution even without saving physical file is
$mail = new PHPMailer();
...
$doc = $pdf->Output('S');
$mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf');
$mail->Send();
If you made a Fillable PDF with Acrobat, here is a good chunk of code that will help you get started. This code requires the newest version of phpmailer to work, so just download that and put it in a class folder in the same directory you put this code in. Have your pdf form submit to a page with this code.
/* Branden Sueper 2012
// PDF to Email - PHP 5
// Includes: PHPMailer 5.2.1
*/
<?php
if(!isset($HTTP_RAW_POST_DATA)) {
echo "The Application could not be sent. Please save the PDF and email it manually.";
exit;
}
echo "<html><head></head><body><img src='loading.gif'>";
//Create PDF file with data
$semi_rand = md5(time());
$pdf = $HTTP_RAW_POST_DATA;
$file = $semi_rand . ".pdf";
$handle = fopen($file, 'w+');
fwrite($handle, $pdf);
fclose($handle);
//
require_once('class/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.xxxxxxx.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxx@xxxxxxx.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->AddAddress('Recipient@xxxxxxx.com', 'First Last');
$mail->SetFrom('reply-to-address@xxxxxxx.com', 'First Last');
$mail->Subject = 'Your Subject';
$mail->Body = 'Hello!';
$mail->AddAttachment($file); // attachment
$mail->Send();
//Delete the temp pdf file then redirect to the success page
unlink($file);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL="success.php">';
exit;
} catch (phpmailerException $e) {
//you can either report the errors here or redirect them to an error page
//using the above META tag
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
//Verify the temporary pdf file got deleted
unlink($file);
?>
PHPMailer Just download PHP mailer and adjust the above code to your liking. For more info on how to create a fillable PDF go to http://www.adobe.com/products/acrobatpro/create-fillable-pdf-forms.html
Good Luck! I remember spending 3+ days trying to figure out a very similar issue!
Here is a complete code http://codexhelp.blogspot.in/2017/04/php-email-create-pdf-and-send-with.html
/**/
$mailto = $_POST['mailto'];
$mailfrom = $_POST['mailfrom'];
$mailsubject = $_POST['mailsubject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$description = $_POST['description'];
$description = wordwrap($description, 100, "<br />");
/* break description content every after 100 character. */
$content = '';
$content .= '
<style>
table {
border-collapse: collapse;
}
table{
width:800px;
margin:0 auto;
}
td{
border: 1px solid #e2e2e2;
padding: 10px;
max-width:520px;
word-wrap: break-word;
}
</style>
';
/* you css */
$content .= '<table>';
$content .= '<tr><td>Mail To</td> <td>' . $mailto . '</td> </tr>';
$content .= '<tr><td>Mail From</td> <td>' . $mailfrom . '</td> </tr>';
$content .= '<tr><td>Mail Subject</td> <td>' . $mailsubject . '</td> </tr>';
$content .= '<tr><td>Firstname</td> <td>' . $firstname . '</td> </tr>';
$content .= '<tr><td>Lastname</td> <td>' . $lastname . '</td> </tr>';
$content .= '<tr><td>Description</td> <td>' . $description . '</td> </tr>';
$content .= '</table>';
require_once('html2pdf/html2pdf.class.php');
$to = $mailto;
$from = $mailfrom;
$subject = $mailsubject;
$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->WriteHTML($content);
$message = "<p>Please see the attachment.</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "pdf-document.pdf";
$pdfdoc = $html2pdf->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;
$body = '';
$body .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "This is a MIME encoded message." . $eol; //had one more .$eol
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
$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 . "--";
if (mail($to, $subject, $body, $headers)) {
$msgsuccess = 'Mail Send Successfully';
} else {
$msgerror = 'Main not send';
}
Have a look into FPDF - http://www.fpdf.org/ - It's free and a great tool for generating PDF's
There is a PDF generator which is recomended by PHP, however it'd very expensive and the name elludes me now, however I've used FPDF several times whith great success.
Depending on your requirements, you could also have a look at TCPDF, I use it a lot to create PDFs on the fly from PHP... It has (limited) HTML to PDF functionality built-in and is very easy to use (just look at the examples). And another major benefit : it's still in active development (a bit too active for some probably :p).
Old question but still relevant, so posting it here for the benefit of new visitors to this question.
PDFMark offers an API to generate PDF and can also deliver it on an email. You can create a PDF from HTML and specify an email address, to which the generated PDF will be sent as an attachment.
Disclosure: I am the developer
精彩评论