Creating & Sending an HTML Email with a Dynamic PDF Attachment
May 6, 2009 | Posted in HTML, PHPThis tutorial will explain how to get the data from a submitted form, create a PDF from that data, and then attach that PDF to an HTML email and finally send it. We will be using dompdf for the PDF generation, and Swift Mailer for sending the email.
Step 1 - Download Libraries
Before we begin, we need to download the necessary files to create the PDF and email.
PDF Creation

After doing a bit of research on a PHP class that can create dynamic PDF's, I found that dompdf was the best, most flexible, and easiest to use solution. It takes in straight HTML like you would find on any webpage, and converts it to a PDF. It can handle pretty much all CSS 2.1 selectors and attributes, and the same goes for its HTML capabilities.
You can download the package here.
HTML Mime Email Creation

Swift Mailer is the best PHP email library available right now. It's incredibly easy to use, and makes sending HTML emails with attachments and what not a simple task. You can get the package here.
Step 2 - File Structure
On top of our two libraries, we'll be creating 3 simple PHP files.
- form.php - PHP page with form, will also process form
- pdf.php - The layout for our PDF
- html.php - Our HTML email template

Step 3 - HTML Form

The form is where we'll be getting the "dynamic data" from for the PDF creation.
For this tutorial, it's just an average form with a few inputs and a submit button. There's also some simple CSS applied to make it look a little better on the eyes.
Normally, you would have some validation and all, but it's not necessary for this tutorial.
You can view the full source and CSS in the preview.
form.php
<form method="post" action="">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" class="input" />
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" class="input" />
<label for="language">Favorite Language:</label>
<select name="language" id="language">
<option value="PHP">PHP</option>
<option value="HTML">HTML</option>
<option value="Javascript">Javascript</option>
<option value="CSS">CSS</option>
<option value="Other">Other</option>
</select>
<label for="about">Tell me about yourself:</label>
<textarea name="about" id="about" rows="4" cols="40"></textarea>
<p><button type="submit">Submit!</button></p>
</form>
We'll simply leave the action blank to submit right back to the same page
Step 4 - Process Form
Now that we have the form, lets get ready to do something when its submitted. We'll start with the code below:
<?php
if ( ! empty($_POST)) {
// Used for later to determine result
$success = $error = false;
// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;
// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
// Check for blank fields
if (empty($post->name) OR empty($post->email) OR empty($post->about))
$error = true;
else {
// Create PDF and send email
}
}
?>
So what we're doing is creating a new object ($post) just b/c I prefer the syntax of objects rather than array's. They look a lot simpler and are easier to use to me also.
Then we loop through the $_POST array and do some very basic filtering. Normally you'd probably want to be a little safer, but this will do for now.
And again, if any fields are blank, show an error message. This would be replaced by possible Javascript validation or whatever.
Step 4 - Creating our PDF Layout
Now we'll need to figure out how we want our generated PDF to look. For this tutorial, it's going to be "stupid simple". Basically just a table echoing what the user entered in the form.
pdf.php
<html>
<head>
<style>
body {font-family:Helvetica, Arial, sans-serif; font-size:10pt;}
table {width:100%; border-collapse:collapse; border:1px solid #CCC;}
td {padding:5px; border:1px solid #CCC; border-width:1px 0;}
</style>
</head>
<body>
<h1>Form Results</h1>
<table>
<tr>
<td>Name:</td>
<td><?php echo $post->name; ?></td>
<td>Email:</td>
<td><?php echo $post->email; ?></td>
</tr>
<tr>
<td>Favorite Language:</td>
<td colspan="3"><?php echo $post->language; ?></td>
</tr>
<tr>
<td>About Yourself:</td>
<td colspan="3"><?php echo $post->about; ?></td>
</tr>
</table>
<p>You can make anything you want b/c dompdf accepts almost all 2.1 CSS attributes and selectors and almost all HTML elements.</p>
</body>
</html>
Step 5 - Creating the Actual PDF
Since we're going to be attaching the PDF to an email, we can't just output it straight to the browser. We need to capture its contents into a variable for later use.
To do this, we'll be using output buffering. Why? Since we need these PHP files to actually be executed (ie: echoing variables from the form data), we can't use something like file_get_contents() because it would just read the file as is without executing any PHP.
The following code will go inside that else statement we created above in step 4
// Get current directory
$dir = dirname(__FILE__);
ob_start(); // Start buffer
require_once($dir.'/pdf.php') // Include PDF layout file
$pdf_html = ob_get_contents(); // Get the contents of file into variable for later use
ob_end_clean(); // Close buffers
So now lets include dompdf and create the PDF
// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
Pretty simple huh?
Step 6 - Creating HTML Email Template
We're going to be sending an HTML email, so we will create another very simple template to go along with the PDF in the email. Again, I'm keeping it simple for this tutorial and just Saying "Hello (name entered in form)", and then telling them to open the PDF that is attached.
html.php
<html>
<body>
<h1 style="font-family:Helvetica, Arial, sans-serif;">Hello <?php echo $post->name; ?></h1>
<p style="font-family:Helvetica, Arial, sans-serif;">Thanks for filling out the form. There should be a PDF attached to this message with your info. Check it out!</p>
</body>
</html>
And again, we'll have to start another output buffer to include the contents of our HTML email template with the included PHP.
// Get the contents of the HTML email into a variable for later
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
Step 7 - Creating & Sending the Email
Now that we have our PDF and HTML template created and ready to be attached, let start the email message.
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('How To Create and Send An HTML Email w/ a PDF Attachment') // Message subject
->setTo(array($post->email => $post->name)) // Array of people to send to
->setFrom(array('no-reply@coreyworrell.com' => 'Corey Worrell')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'coreyworrell.pdf', 'application/pdf')); // Attach the generated PDF from earlier
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
Again, very simple with Swift Message. The code should explain itself, but the important parts are setBody() & attach()
We set the body of our message with our HTML template we created in step 6.
Then we add the PDF as an attachment ($pdf_content), set its name (coreyworrell.pdf), and the mime type (application/pdf).
It's as simple as that!
Then finally, send the email. If it works then set $success to true so we can show the appropriate message to the user. If something went wrong, then show error message.

Step 8 - Filling In the Missing Pieces
So now we have $success & $error defined, so we'll put those to use in our form layout. This will go above the form.
form.php
<?php if ($success) { ?>
<div class="message success">
<h4>Congratulations! It worked! Now check your email.</h4>
</div>
<?php } elseif ($error) { ?>
<div class="message error">
<h4>Sorry, an error occurred. Try again!</h4>
</div>
<?php } ?>
Final Code
Here's what form.php should look like at the end
<?php
if ( ! empty($_POST)) {
// Used for later to determine result
$success = $error = false;
// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;
// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
// Check for blank fields
if (empty($post->name) OR empty($post->email) OR empty($post->about))
$error = true;
else {
// Get this directory, to include other files from
$dir = dirname(__FILE__);
// Get the contents of the pdf into a variable for later
ob_start();
require_once($dir.'/pdf.php');
$pdf_html = ob_get_contents();
ob_end_clean();
// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
// Get the contents of the HTML email into a variable for later
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('How To Create and Send An HTML Email w/ a PDF Attachment') // Message subject
->setTo(array($post->email => $post->name)) // Array of people to send to
->setFrom(array('no-reply@coreyworrell.com' => 'Corey Worrell')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'coreyworrell.pdf', 'application/pdf')); // Attach the generated PDF from earlier
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
}
}
?>
Final Words
Hopefully you now know more about dompdf and Swift Mailer and sending HTML emails with attachments.
Again, check out the source code in the demo or download the source to see exactly how everything works together.
And be sure to try out the demo and view the results of our hard work.









useful post, thanks!
What if I want the email attachment to go to a specific email all the time..How do i do that?
@Robert:
just replace line 51 in form.php with something like this:
->setTo(array('your_email@website.com' => 'Your Name'))and you could remove the email input field from the form also if you wanted.
Hello!
Thanks for your guide it had helped me great. But I have one problem: image doesn't render along with html. When PDF is not emailed, image is rendered normally. But when PDF is sent, image is not included. Using different image type (png/jpg/gif) makes no difference.
I'm wondering if you can help me?
Thanks in advance.
@Jan:
Glad it has helped you.
When sending emails, all links and images must use the full URL.
So instead of:
<img src="images/photo.jpg" />
Do this:
<img src="http://mywebsite.com/images/photo.jpg" />
Works like a charm:) Thank you for fast reply, article and help!
Thanks for this great tutorial. I am planning to use this tutorial to build a online survey to get a pdf output. Is there a way to implement a function to check if required fields are not filled out without getting the error and the need of filling out te form again?
Regards,
Peter
@Peter:
Thanks, glad it could help.
To answer your question, look over the form.php file, you can first remove lines 19 and 62 (the else statement). Then on line 16 you could change what happens when a field is empty.
Hello,
I need use charset windows-1260 in PDF file. Please help me!
Viktor, Slovakia
@Viktor:
I'm pretty sure you can insert the meta tag with that charset in the head of your html document before converting it to a pdf.
This is a great tutorial, it has been of help. And I've learned about Swift Mailer and dompdf. You just earned a subscriber!
Hello!
I need not only send but also open the PDF file with one click. I am beginner in PHP programming.
Please help me and sorry for my English :)
Viktor, Slovakia
@Viktor:
If you want to directly output the PDF to the user, you can use this function:
$dompdf->stream('sample.pdf');Acrobat shows this errror message:
Acrobat could not open "ziadost.pdf" because it is either not a supported file type or because the file has been damaged (for example, it was sent an email attachment and wasn´t correctly decoded).
Where I must copy "$dompdf->stream..." command?
@Viktor:
Well if you want to email the pdf and have it open at the same time, you could put "$dompdf->stream..." inside the if statement if the email sent:
if ($mailer->send($message)) { $success = true; $dompdf->stream('ziadost.pdf'); }If that doesn't work then I'd take a look at the dompdf documentation.
Doesn't work :(
If you cannot solve this problem, it also helps when the script sends the PDF file to the address which I will fill-out in the form.
The problem is with the webhosting, probably on Apache doesnt enabled some of functions.
On other hosting script working fine!
Thanks for your support ;)
This is a solution for correct sending and opening at the same time
// Load the dompdf files for opening require_once($dir.'/dompdf/dompdf_config.inc.php'); $dompdf = new DOMPDF(); // Create new instance of dompdf $dompdf->load_html($pdf_html); // Load the html $dompdf->render(); // Parse the html, convert to PDF $dompdf->stream('ziadost.pdf'); // Load the dompdf files for sending $dompdf = new DOMPDF(); // Create new instance of dompdf $dompdf->load_html($pdf_html); // Load the html $dompdf->render(); // Parse the html, convert to PD $pdf_content = $dompdf->output();// Put contents of pdf into variable for laterHi,
I have document page document from : http://localhost/app/test.php?p=1_print&sid=0ms13cA5szja
How to generate page to pdf and email it as attachment..?
Thanks in advance
Nyoman
Hi
I followed the tutorial and it works great. I am just wondering if I can allow users to attach other files. I need to convert the email like this does but I also want to let users attach other pdf files from their own computers. I have tried adjusting the code and it seems to work but Adobe won't open the second pdf. I get a message saying something like the file has been damaged and if it was sent by email attachment it wasn't correctly decoded.
Any help greatly appreciated.
Thanks
Something like this should work:
I am still getting the error when I try to open the second pdf. Where the code above says ['tmp'] I changed to ['tmp_name'] otherwise I got a error: Undefined index: tmp. Originally I had some code similar to this before the attach line of code:
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
if ($_FILES["attachment"]["error"])
{
//Redirect if the upload has failed
header("Location: ./form4.php?error=upload_failed");
exit();
}
$file_path = $_FILES["attachment"]["tmp_name"];
$file_name = $_FILES["attachment"]["name"];
$file_type = $_FILES["attachment"]["type"];
}
So I tried swapping tmp_name with tmp but it made no difference and it does the same thing with or without this bit of code so at the moment I only have the line you suggested.
This is the code I ended up with but it still does the same as it did before and won't open:
$message->attach(Swift_Attachment::newInstance($_FILES['user_uploaded_file']['tmp_name'], $_FILES['user_uploaded_file']['name'], 'application/pdf'));
I don't know a lot about this sort of thing, but I am wondering if it could be something to do with my form, this is what iI have:
This is what I have at the beginning of form
This is what I added for the attachment
Attachment
The rest of the form is like your tutorial.
Thanks
Sorry I should have put the code like this
Hmm.. I don't know what you mean by "it won't open". Email me from my contact form if you haven't figured it out yet.
Hi foks,
great tutorial, works awesome.
Could you give me a hint on how to send the mail in TXT-format in addition?
Thank you for your answers.
@Daniel:
Thanks! You can add this line after the 'setBody' line:
->addPart('The email message in plain text', 'text/plain');hey, great thanks for this... and I made another template called "plain_text.php" like you did above with the html-template:
and then ...
->setFrom(array('mailer@mydomain.tld' => 'Subject text')) ->setTo(array($post->person_email => $post->person_lastname)) ->attach(new Swift_MimePart($txt_message, 'text/plain')) ->attach(new Swift_MimePart($html_message, 'text/html'));I'm getting it now in both formats, but how can i manage to display my text-format-email with newlines ("\n") in Outlook or thunderbird. There are no new lines, all in one big line without line-breaks.
I tried it with:
<?php printf('\r\n') .... echo '\n'; ?>inside the "plain_text.php", but nothing works. And I double-checked that my outlook/thunderbird clients don't remove new lines: they don't.Corey, do you have a hint for me for solving this? best regards,
Daniel
In PHP, new lines only work if you use double-quotes
Hi Corey,
that doesn't work, too... maybe it is a bug, read issues here:
http://swiftmailer.lighthouseapp.com/projects/21527/tickets/113-quoted-printable-soft-line-breaks-problem#ticket-113-10
http://swiftmailer.lighthouseapp.com/projects/21527/tickets/146-swift-406-multipart-html-txt-text-version-does-not-show-in-thunderbird#ticket-146-1
Tried just to send the text email and see: there are new lines in the mail. Seems that we can't combine Text and HTML at the moment because of a bug.
best regards,
Daniel
I am new at forms but what would be the easiest way to validate or at least make a field required and return that message at the top after submitted with an asterick next to the field?
I have this working but for some reason the file attachments are empty or correpted.
I am posting from a form and use the following line.
The attachement is there but if I open the PDF in this case I get the following error..
Acrobat could not open 'tes.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)
I have tried sending the email to other computers and they all get the same error.
We are using Windows 2003 SBS with PHP 5.2.2 and IIS6
If i open the PDF doc in word I see the actual path to the PHP temp DIR:
C:\Program Files\PHP\temp\upload\php2F04.tmp
can you please help with this....
sorry code should be like this...
and adding the line
to attache the file
I m having PHP 5.1 . i have uploded all the files to server. when i submit the form it's giving an blank page. it's displaying error nicly. if i comment $dompdf->render(); this it's generating pdf file with 0 content. please suggest where it's compatible for PHP 5.1
Hi this code working excelent .. thankz a lot ... i want to submit url name from form ... that should convert to the pdf and attached ......
if i want to send url from form where should i change this code
Hi I am new to php i have seen the demo and i just downloaded and tried in my server but its showing me an error Sorry, an error occurred. Try again!
can u please guide me where the problem is!
Hello Corey,
This is a very good contribution for all of us, but I would like to let you know that when I'm adding many text content in the text-area, then the layout is breaking in the PDF form. It's coming in one single line without any line break or word-wrap.
Could you please help me out? The PDF form structure is distorting when such long texts are entered in the HTML form.
Please have a look. Anybody who can help will be appreciated.
Thanks,
Siddhartha
I loaded your form onto a server to test it but I got this error:
parse error: syntax error, unexpected T_OBJECT_OPERATOR in \\frigga\home\users\web\b1405\as.daubach\form.php on line 50
The server runs php 4.4.8. I can change it to 5.25 but it did not solve the problem ? any ideas
Hi, I am trying to generate the pdf and everthing is perfctly fine. The only issue with me is generated pdf which is sent as attachment to email, open 62% by default. I want to open it 100%. Please let me know where and what changes need to be done. Any help will be highly appreciated.
Thanks,
Shree
Instead of using pdf.php, is it possible to use an already formatted PDF file and parse the information direct into the form in the pdf then email it?
Hi Cory,
After a lot of search on the net this tutorial was the only one that yielded progress. Will definitely be posting a ref to it on Stackoverflow.
Below is the code that gets me some pdf output. But I only get the last record to appear. As an array query with echo this routine works fine and prints all selected. What am I missing?
Thanks
Allen
hi
i uploaded the files and get an error when submitting the form
"Sorry, an error occurred. Try again!"
all fields were filled,
what can it be?
thanks
Sir,
How can create PDF without advertising.
Great tutorial... I figured it out after a couple of days of playing with it...
Would you know how to set it up to send to multiple email address, or CC to someone. I ultimately would like it to send a copy to the person uploading and an additional permeant email to my account
@Jeff: Just pass an array to the "setTo" method. Or use the "setCc" method.
Viagra prix et leurs effets secondaires ne sont pas toujours pu etre envisagee chez les patients ayant des problemes du systeme cardiovasculaire et, en particulier, les hommes atteints de diabete.
Thank you for this useful post. This is exactly what I am looking for.
I downloaded the whole files, uploaded it to my server and try to run form.php. But I simply got this error:
Parse error: parse error in E:\home\....\htdocs\form\pdf_email\form.php on line 50
->setSubject("How To Create and Send An HTML Email w/ a PDF Attachment") // Message subject
that is the code in line 50.
I don't know what I am going to change to make this work.
Any help is greatly appreciated.
Thanks
I think I already know what the problem is. My hosting does not support Swiftmailer because when I tried to upload this whole files in other hosting account it is working fine.
Hello, I am trying to do what you describe above but using adobe acrobat x pro to create my interactive pdf from a normal pdf form, and then sending with php. Please can you tell me if this is possible? I look forward to hearing from you shortly.
Kind regards
Emma
Hi - I'm really trying to make this work but I get this error - what didn't I install properly? Thank you
Fatal error: Call to undefined function spl_autoload_functions() in c:\program files (x86)\apache group\Apache\htdocs\swift\swift_required.php on line 54
Hello,
Nice tutorial I have not tested it yet but made good reading. I was wondering if you can help with the below please.
What kind of changes will be required if I wanted to use the mail function or sendmail??
Also is their a way that when the form is being attached it can be saved to a folder on the server with a unique name for each file or an option to chose your own custom name
Thank you.
Nice article and Tips.
More clue, informations and Tips
Great Tutorial.. It helps me a lot.
Is it possible to add Attached File? (like: jpeg, gif, png, etc.)
If possible, How to do it?
Any help would be great. Thanks :)
Hi,
Following error generated when i am going to create pdf from large html file through this libraries. Please help..
Fatal error: Uncaught exception 'DOMPDF_Internal_Exception' with message 'Frame not found in cellmap' in /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/cellmap.cls.php:237 Stack trace: #0 /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/table_cell_frame_reflower.cls.php(66): Cellmap->get_frame_position(Object(Table_Cell_Frame_Decorator)) #1 /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/frame_decorator.cls.php(387): Table_Cell_Frame_Reflower->reflow() #2 /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/table_row_frame_reflower.cls.php(70): Frame_Decorator->reflow() #3 /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/frame_decorator.cls.php(387): Table_Row_Frame_Reflower->reflow() #4 /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/table_row_group_frame_reflower.cls.php(70): Frame_Decorator->reflow() #5 /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/frame_decorator.cls.php(387): Table_Row_Group_Frame_Reflower->reflow() #6 /home in /home/keyideas/public_html/docubuddy/cron_mail/dompdf/include/cellmap.cls.php on line 237
Thanks
Abhishek Anand
Great tutorial, this is the same what i need...
but i have on problem when i want to use it,
when i try to use it i get the sorry error & the same error comes me for the demo, now i just download you given files & change the email address & subject with mine...
Please guide me soon
demo does not work.
Who would not enjoy having their hands on some inexpensive New Cheap Reebok NFL Dallas Cowboys Marion Barber Youth White Th
? authentic nfl jerseys china Some individuals acknowledge the truth that you can find much more other items which they will splurge on. No matter how inviting genuine New Reebok NFL Minnesota Vikings 12 Percy Harvin Black Jersey
are, broncos nike nfl jerseys 2012 they’re able to just be too expensive to be regarded bought by an typical football fan. So in order to compromise, inexpensive NFL jerseys are availed of instead. But the issue here is that low cost usually becomes synonymous with reduced high quality. discount nfl jerseys china In the end, you nevertheless don’t get your money’s really worth since you find yourself getting dysfunctional stuff. Even now, that does not indicate you need to not consider buying cheap New Reebok NFL Miami Dolphins 77 Jack Long Away White Jersey
in any respect. Indianapolis Colts Even though it really is a danger you need to take, the income you will save can still make you think twice when purchasing genuine ones. Jacksonville Jaguars Even so, there are several methods in which you are able to compromise your love for the game with what your wallet can actually afford. You can find some tricks and tips which will help provide you with access with low-cost St.Louis Rams
without risking good quality over cost.
Good Morning, I hope you can help me. I am trying to creat a registration form that once is filed out and the custumer press submit it will be email to me by an attach pdf file. but I am not having any luck getting it to work. Here is what I have:
Creating & Sending An HTML Email w/ a PDF Attachment
html, body, h1, h2, h3, h4, h5, h6, p, span, ul, li, div, form, input, select, textarea, button {margin:0; padding:0;}
ul {list-style:none;}
a, a:hover {text-decoration:none; outline:0;}
a img {border:0;}
body {font:12px/16px Verdana, Arial, sans-serif; background:#000066;}
#container {width:750px; margin:10px auto; padding:10px; overflow:hidden; border:1px solid #000; border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px; background:#F9F9F9;}
#container h1 {margin-bottom:20px; font-size:40px; line-height:40px; font-family:'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight:normal;}
.message {margin-bottom:10px; padding:5px;}
.success {color:#4F8A10; border:1px solid #4F8A10; background:#DFF2BF;}
.error {color:#D8000C; border:1px solid #D8000C; background:#FFBABA;}
label {display:block; margin-bottom:3px; cursor:pointer;}
.input, textarea, select, button {display:block; margin-bottom:10px; padding:3px; font:12px/12px 'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; border:1px solid #CCC; border-top-width:2px;}
textarea {font-size:13px; line-height:16px;}
select {}
button {float:center; width:60px; margin-bottom:0; padding:3px 30px; cursor:pointer; font-size:12px; border:1px solid #999; border-bottom-width:2px; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; background:#EEE;}
button:active {border-bottom-width:12px; padding:4px 30px 3px; background:#E9E9E9;}
Registration Form
Congratulations! Your Registration was Receive!
Sorry, an error occurred. Try again!
Date:
Checks payable to: FODT or Future Olympians Diving Team
Circle one payment form:
Check
Cash
MO
PO
Divers Information:
Full Name
Male
Female
D.O.B
Age
Address
City
State
Zip Code
Email Address
Phone Number
Cell Number
Parents Information:
Full Name
Cell Number
Full Name
Cell Number
Emergency Contact:
Full Name
Phone Number
Cell Number
Signature
Good Morning, I hope you can help me. I am trying to creat a registration form that once is filed out and the custumer press submit it will be email to me by an attach pdf file. but I am not having any luck getting it to work. Here is what I have:
Creating & Sending An HTML Email w/ a PDF Attachment
html, body, h1, h2, h3, h4, h5, h6, p, span, ul, li, div, form, input, select, textarea, button {margin:0; padding:0;}
ul {list-style:none;}
a, a:hover {text-decoration:none; outline:0;}
a img {border:0;}
body {font:12px/16px Verdana, Arial, sans-serif; background:#000066;}
#container {width:750px; margin:10px auto; padding:10px; overflow:hidden; border:1px solid #000; border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px; background:#F9F9F9;}
#container h1 {margin-bottom:20px; font-size:40px; line-height:40px; font-family:'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight:normal;}
.message {margin-bottom:10px; padding:5px;}
.success {color:#4F8A10; border:1px solid #4F8A10; background:#DFF2BF;}
.error {color:#D8000C; border:1px solid #D8000C; background:#FFBABA;}
label {display:block; margin-bottom:3px; cursor:pointer;}
.input, textarea, select, button {display:block; margin-bottom:10px; padding:3px; font:12px/12px 'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; border:1px solid #CCC; border-top-width:2px;}
textarea {font-size:13px; line-height:16px;}
select {}
button {float:center; width:60px; margin-bottom:0; padding:3px 30px; cursor:pointer; font-size:12px; border:1px solid #999; border-bottom-width:2px; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; background:#EEE;}
button:active {border-bottom-width:12px; padding:4px 30px 3px; background:#E9E9E9;}
Registration Form
Congratulations! Your Registration was Receive!
Sorry, an error occurred. Try again!
Date:
Checks payable to: FODT or Future Olympians Diving Team
Circle one payment form:
Check
Cash
MO
PO
Divers Information:
Full Name
Male
Female
D.O.B
Age
Address
City
State
Zip Code
Email Address
Phone Number
Cell Number
Parents Information:
Full Name
Cell Number
Full Name
Cell Number
Emergency Contact:
Full Name
Phone Number
Cell Number
Signature
Good Morning, I hope you can help me. I am trying to creat a registration form that once is filed out and the custumer press submit it will be email to me by an attach pdf file. but I am not having any luck getting it to work. Here is what I have:
<?php if (!empty($_POST)) {// Used for later to determine result $success = $error = false;
// Object syntax looks better and is easier to use than arrays to me $post = new stdClass;
// Usually there would be much more validation and filtering, but this // will work for now. foreach ($_POST as $key => $val) $post->$key = trim(strip_tags($_POST[$key]));
// Check for blank fields if (empty($post->Date) OR empty($post->FullNmae) OR empty($post->DOB) OR empty($post->Age) OR empty($post->Address) OR empty($post->City) OR empty($post->State) OR empty($post->Zip) OR empty($post->Email) OR empty($post->Phone) OR empty($post->FullNmae2) OR empty($post->Cell2) OR empty($post->FullNmae4) OR empty($post->Phone4) OR empty($post->Signature)) $error = true;
else {
// Get this directory, to include other files from $dir = dirname(__FILE__);
// Get the contents of the pdf into a variable for later ob_start(); require_once($dir.'pdf.php'); $pdf_html = ob_get_contents(); ob_end_clean();
// Load the dompdf files require_once($dir.'dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf $dompdf->load_html($pdf_html); // Load the html $dompdf->render(); // Parse the html, convert to PDF $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
// Get the contents of the HTML email into a variable for later ob_start(); require_once($dir.'html.php'); $html_message = ob_get_contents(); ob_end_clean();
// Load the SwiftMailer files require_once($dir.'swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance() ->setSubject('FODT Registration Form') // Message subject ->setTo(array('divecoach1@gmail.com' => 'Name')) // Array of people to send to ->setFrom(array('divecoach1@gmail.com' => 'Name')) // From: ->setBody($html_message, 'text/html') // Attach that HTML message from earlier ->attach(Swift_Attachment::newInstance($pdf_content, 'nettuts.pdf', 'application/pdf')); // Attach the generated PDF from earlier
// Send the email, and show user message if ($mailer->send($message)) $success = true; else $error = true;
}
} ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head>
Creating & Sending An HTML Email w/ a PDF Attachment
html, body, h1, h2, h3, h4, h5, h6, p, span, ul, li, div, form, input, select, textarea, button {margin:0; padding:0;} ul {list-style:none;} a, a:hover {text-decoration:none; outline:0;} a img {border:0;}
body {font:12px/16px Verdana, Arial, sans-serif; background:#000066;} #container {width:750px; margin:10px auto; padding:10px; overflow:hidden; border:1px solid #000; border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px; background:#F9F9F9;} #container h1 {margin-bottom:20px; font-size:40px; line-height:40px; font-family:'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight:normal;} .message {margin-bottom:10px; padding:5px;} .success {color:#4F8A10; border:1px solid #4F8A10; background:#DFF2BF;} .error {color:#D8000C; border:1px solid #D8000C; background:#FFBABA;} label {display:block; margin-bottom:3px; cursor:pointer;} .input, textarea, select, button {display:block; margin-bottom:10px; padding:3px; font:12px/12px 'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; border:1px solid #CCC; border-top-width:2px;} textarea {font-size:13px; line-height:16px;} select {} button {float:center; width:60px; margin-bottom:0; padding:3px 30px; cursor:pointer; font-size:12px; border:1px solid #999; border-bottom-width:2px; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; background:#EEE;} button:active {border-bottom-width:12px; padding:4px 30px 3px; background:#E9E9E9;}
</head> <body>
<div id="container">
<h1>Registration Form</h1>
<?php if ($success) { ?> <div class="message success"> <h4>Congratulations! Your Registration was Receive!</h4> </div> <?php } elseif ($error) { ?> <div class="message error"> <h4>Sorry, an error occurred. Try again!</h4> </div> <?php } ?>
<form method="post" action=""> <table width="100%" border="0"> <tr> <td colspan="8"><p align="center"><img src="http://fodt.org/images/PNG/FODT logo.png" width="401" height="72" alt="logo" /></td> </tr> <tr> <td width="3%"> </td> <td width="23%"> </td> <td width="15%"> </td> <td width="15%"> </td> <td width="14%"> </td> <td width="14%"> </td> <td colspan="2"><label for="Date">Date:</label> <input type="text" name="Date" id="Date" maxlength="10" width="77" height="10" class="input" /></td> </tr> <tr> <td colspan="5">Checks payable to: FODT or Future Olympians Diving Team</td> <td> </td> <td width="5%"> </td> <td width="11%"> </td> </tr> <tr> <td colspan="2">Circle one payment form:</td> <td><center><label for="Check">Check</label> <input type="checkbox" name="Check" id="Check" /></center></td> <td><center><label for="Cash">Cash</label> <input type="checkbox" name="Cash" id="Cash" /></center></td> <td><center><label for="MO">MO</label> <input type="checkbox" name="MO" id="MO" /></center></td> <td><center><label for="PO">PO</label> <input type="checkbox" name="PO" id="PO" /></center></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="2"><strong>Divers Information:</strong></td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td height="59" colspan="2"><label for="FullName">Full Name</label> <input type="text" name="FullName" id="FullName" maxlength="80" class="input" width="115" height="10" /></td> <td><center><label for="Male">Male</label> <input type="checkbox" name="Male" id="Male" /></center></td> <td><center><label for="Female">Female</label> <input type="checkbox" name="Female" id="Female" /></center></td> <td><center><label for="DOB">D.O.B</label> <input type="text" name="DOB" id="DOB" maxlength="10" width="65" height="10" class="input" /></center></td> <td><center><label for="Age">Age</label> <input type="text" name="Age" id="Age" maxlength="2" width="35" height="10" class="input" /> </center></td> <td colspan="2" rowspan="2" align="center"><img src="http://fodt.org/images/logo3.png" width="66" height="64" alt="diver" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="8"><label for="Address">Address</label> <input type="text" name="Address" id="Address" maxlength="100" width="550" height="10" class="input" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="2"><label for="City">City</label> <input type="text" name="City" id="City" maxlength="50" width="115" height="10" class="input" /></td> <td colspan="2"><label for="State">State</label> <input type="text" name="State" id="State" maxlength="2" width="50" height="10" class="input" /></td> <td colspan="2"><label for="Zip">Zip Code</label> <input type="text" name="Zip" id="Zip" maxlength="5" width="90" height="10" class="input" /></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="2"><label for="Email">Email Address</label> <input type="text" name="Email" id="Email" maxlength="80" width="115" height="10" class="input" /></td> <td colspan="2"><label for="Phone">Phone Number</label> <input type="text" name="Phone" id="Phone" maxlength="12" width="115" height="10" class="input" /></td> <td colspan="2"><label for="Cell">Cell Number</label> <input type="text" name="Cell" id="Cell" maxlength="12" width="115" height="10" class="input" /></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="2"><strong>Parents Information:</strong></td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td colspan="4" rowspan="5" align="center"><img src="http://fodt.org/images/parents.png" width="147" height="150" alt="parents" /></td> </tr> <tr> <td colspan="2"><label for="FullName2">Full Name</label> <input type="text" name="FullName2" id="FullName2" maxlength="80" width="115" height="10" class="input" /></td> <td colspan="2"><label for="Cell2">Cell Number</label> <input type="text" name="Cell2" id="Cell2" maxlength="12" width="115" height="10" class="input" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="2"><label for="FullName3">Full Name</label> <input type="text" name="FullName3" id="FullName3" maxlength="80" width="115" height="10" class="input" /></td> <td colspan="2"><label for="Cell3">Cell Number</label> <input type="text" name="Cell3" id="Cell3" maxlength="12" width="115" height="10" class="input" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="2"><strong>Emergency Contact:</strong></td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td colspan="4" rowspan="5" align="center"><img src="http://www.fodt.org/images/parentsemergency.png" width="188" height="136" alt="emergency" /></td> </tr> <tr> <td colspan="2"><label for="FullName4">Full Name</label> <input type="text" name="FullName4" id="FullName4" maxlength="80" width="115" height="10" class="input" /></td> <td colspan="2"><label for="Phone4">Phone Number</label> <input type="text" name="Phone4" id="Phone4" maxlength="12" width="115" height="10" class="input" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"><label for="Cell4">Cell Number</label> <input type="text" name="Cell4" id="Cell4" maxlength="12" width="115" height="10" class="input" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="3"><input type="text" name="Signature" id="Signature" maxlength="80" width="180" height="10" class="input" /> <label for="Signature">Signature</label></td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td colspan="8" align="center"><input name="submit" type="button" value="Click Here to Submit!" /> </td> </tr> </table>
</form>
</div>
</body> </html>
Thanks for your guide it had helped me greatly. I got my form to work, But I have one problem: I can't get the images to appear in the PDF file. When the PDF is sent to my email, the images are not included in the PDF. I am using PNG.
I'm wondering if you can help me?
Thanks in advance.
Thanks for your guide it had helped me greatly. I got my form to work, But I have one problem: I can't get the images to appear in the PDF file. When the PDF is sent to my email, the images are not included in the PDF. I am using PNG.
I'm wondering if you can help me?
Thanks in advance.
How am i able to have this send to a gmail account? it keeps getting an error or failing? Any help is appreciated thanks!
I got everything to work perfectly! Now all I need is to figure out a way to email a download link to the PDF rather than making it an attachment. I have a client that needs this feature as often times an attached PDF being sent from a server gets flagged as spam and ends up in the Junk Mail folder.
Hi!
This is really useful, and I would like to implement it in a site I'm developing.
I'm curious, what would be the best way to add a captcha/recaptcha form to this? I can see this being used by spammers and taxing both processing and bandwidth on the server's end.
Thank you for your time and code!
-Demir
hii gudmorning i want use for pdf attachment in payslip in mail for every pesn same plz help and reply
Corey,
Your tutorial is excellent! I've made a few simple modifications for my purposes and it is working great. I'm so grateful that you have shared this.
Thank you!
Christopher
Nice post, will try it
I love this post man!
I had multiple questions but now I only have one.
Can the pdf created have editable regions?
@Jose:
Not sure, you'd haveta look at the dompdf documentation.
Thank You!!! Corey I really appreciate you taking the time to answer so quick and writing this post. I've already post on their blog because they don't have anything like that. It looks like i'll be using FPDF to get the result I need. Thanks a lot!
Thanx for this but I have few questions:
I'm trying to merge this example with Jqtouch...which isn't going so well.
The example works great on its own, but I am using jqtouch to develop a web app, and I think the submit button in the form.php doesn't work because of jqtouch library code for submit button somehow....
Is there another way of passing the php code?
The example works if I remove the code below from jqtouch index page...
but I lose the look and feel of the jqtouch webapp.
any help would be greatly appreciated!!!
$('#swipeme').swipe(function(evt, data) {
$(this).html('You swiped ' + data.direction + '/' + data.deltaX +':' + data.deltaY + '!');
$(this).parent().after('swiped!')
Please help me to make this code to work in my system too...i downloaded the files and started working but getting a big error list.. what settings should i change to make it to work.Am able to send mail through smtp using pear...but not by mail function...please help me..
Below is the error list...
Uncaught exception 'PDFlibException' with message 'mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()' in C:\xampp\htdocs\xampp\sreelpg\pdf\swift\classes\Swift\Transport\SimpleMailInvoker.php:48 Stack trace: #0 C:\xampp\htdocs\xampp\sreelpg\pdf\swift\classes\Swift\Transport\SimpleMailInvoker.php(48): mail('Rohini
thanx for this code but i have some prob i send pdf only 2 k size i want 5MB size pdf attach so please help ...where change in code plz help sender code and any peson plz plz.......
Thank you!
Sumitdadhich
Has anybody had issues with the resulting email, with the PDF, getting caught up in spam filters?
I've heard sending it via SMTP authentication would cut the chances of the emails getting caught in spam. If true, please point me in the direction of what code needs to be modified. Thanks.
thanx for this code but i have prob ..in my code PDFs is covert but the pdf alignment not a good and not a match for html the PDFs alignment is more big compere ti HTML code please help urgent plz .....
Hi,
i get the below error on line 50 of form.php.
syntax error, unexpected T_OBJECT_OPERATOR in
please help.
thanks
Amazing. Thank you.
Hey Corey,
I was wondering how this can be executed if using XHR?
if you had the form in a html file, then trigger the php code in an external php with ajax XHR? using the php code in form.php
Thanks very much! that's exacly what I was looking for
Hi Corey,
I have followed all your instructions to the latter but my "success" page
gives a blank page.
Upon line by line diagnosis, i realized no code executes after $dompdf->render();
[Am still a beginner]
Hi, Nice tutorials.
But I have a question it is possible to insert this code to the plugins?
Cause in my website , I have using a plugins site that called a user submitted post, it's very nice and working but i get some problems or think another idea. instead of posting in the submission, I want a upload/attachment document in this plugin. the plugins has a code for upload in images file only, but our site more submission for the article journals publish not a blog site. now i am looking/searching the code that help me to comply this. but i can't.
I see this tutorials, but i can't perform this cause i don't how to insert this to my plugins.
nice code it is usefull
I've tried your solution to the rather primitive website i was asked to edit in order to add the form info to pdf attachment feature described above. At the starters everything was OK but as soon as i increased the number of fields the processing ends in a white screen (i guess the rendering halts at some point). I assume the memory limit is reached (and since i am using a shared web hosting i cant modify the php settings on the server) My website has around 100 Fields hence the information to be added to the final pdf is quite abundant. In addition (not that the things were wrong enough) my client is asking for a certain pdf template which makes the situation even worse. Some more experienced programmers insist that TCPDF library's performances are better than DOMPDF. If we are to believe them would there be any chance for you to adjust your script for using TCPDF instead of DOMPDF (at least for bigger forms)???