Sending Filled-in PDF Files as an Email Attachment

Quite often I am asked how to send the results of a filled PDF through email. This is just a quick overview of the steps needed in order to make that possible. I will assume the following:

  • You have a form which has matching field names as found in your PDF form.
  • The form posts to a script on your web host that can generate the XFDF string with the createXFDF() function.
  • The data you are using has been tested, filtered and escaped as per safe web development practices.
  • You have tested that the XFDF result is valid for the PDF and does fill in the fields as expected.

After all, if you don't have this portion together, you aren't ready for the next steps, right? If you haven't gotten this far, please refer back to the main article describing how to fill in a PDF form from a web page as well as the sub articles.

Sending the XFDF via Email

When all our prerequisites have been met, it is time to define some additional pieces we will need in order to send our email:

$mailto = '';
$subject = 'Subject of email with attachment';
$from = '';
$message = 'A user submitted data to fill your PDF file with. Attached is the result.';
$attached_file_name = $data['last_name'] . '-' . $data['last_name'] . time() . '.xfdf';
$attached_file_type = "application/vnd.adobe.xfdf";

To send the XFDF, all we need to do is generate the email message. We need to use a MIME email to create the attachments. I won't explain the format of the email and what everything means since that is outside the scope of this document.  Using the data and variables above, we can send the email with the following:

$email_data = chunk_split( base64_encode( $xfdf ) );
$mime_boundary = '==Multipart_Boundary_x' . md5( time() ) . 'x'; 
$headers = 'From: ' . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    ' boundary="' . $mime_boundary . '"';
$message = 'This is a multi-part message in MIME format.' . "\n\n" .
    '--' . $mime_boundary . "\n" .
    'Content-Type: text/plain; charset="iso-8859-1"' . "\n" .
    'Content-Transfer-Encoding: 7bit' . "\n\n" .
    $message . "\n\n" .
    '--' . $mime_boundary . "\n" .
    'Content-Type: ' .$attached_file_type . ';' . "\n" .
    ' name="' . $attached_file_name . '"' . "\n" .
    'Content-Disposition: attachment;' . "\n" .
    ' filename="' . $attached_file_name . '"' . "\n" .
    'Content-Transfer-Encoding: base64' . "\n\n" .
    $email_data . "\n\n" .
    '--' . $mime_boundary . "\n";
$res = mail( $mailto, $subject, $message, $headers);
    // mail failed! Send inline as a backup method
        'ERROR: '. $subject,
        'Unable to send xfdf file via attachment. Data follows:' .
        "\n----- Begin -----\n$xfdf\n----- End -----\n"

At this point, the email should have been sent (either with the attachment or the data inline). You can now do any additional processing that you wish. When you receive the email, you should now be able to oen the XFDF file in Adobe Acrobat and have the fields filled in for you. Sometimes Live Cycle creates the PDFs in a way where it doesn't Just Work™, for those files you may need to use the menus to import form data and select the XFDF file after opening the PDF.

What About Sending a PDF File With the Data in it Already?

I know, I know. No matter how many times I have said on my website in the past that my function doesn't make an actual PDF file, I get asked how to do it at least once a week! Therefore, I have added a section to the end of the mail article about using HTML forms to fill in PDFs. Check for a heading stating "Additional Code Examples" for that information, then come back here when you are ready and have the ability to create an actual filled PDF form.

Great, you're back! At this point, you should be able to do the following:

  • submit a form to generate XFDF
  • email the XFDF as an attachment (outlined above)
  • generate a PDF file as in the article linked above

You are now ready to ditch that ugly XFDF file attachment in favor of a PDF!  Luckily for you, only a few minor changes need to be done:

  • Change the extension of $attached_file_name from "xfdf" to "pdf"
  • Change $attached_file_type to be "application/pdf"
  • Find the line:
    $email_data = chunk_split( base64_encode( $xfdf ) );
    add the following code before that line:
    $xfdf = file_get_contents( $result_pdf );

You should now be able to send the filled PDF in an email as an attachment.

Suggestions or Improvements?

Do you have something that may benefit others regarding what you find on this page? Would you like to share it? If so, simply contact me and I will review it and possibly post it here.

Did this article help you save time for a project? Did this information help you create your website or web application? If so, please send a donation my way - no amount is too small to show your appreciation.