Email PDF Attachment with PHP Using FPDF

Email PDF Attachment with PHP Using FPDF

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Email PDF Attachment with PHP Using FPDF

Emailing a PDF generated dynamically by FPDF requires careful handling of MIME boundaries and content types. The core issue in the provided code snippet stems from how the attachment data is constructed and sent within the multipart/mixed structure. Specifically, the boundary string used for the mixed part does not match the boundary string used for the alternative part (the text body). While this might seem like a minor inconsistency, modern mail clients are often strict about header integrity. However, the most critical failure point lies in the MIME type and filename handling of the attachment itself.

The code generates a PDF document using FPDF and retrieves its output as a base64-encoded string:

$doc = $pdf->Output('test.pdf', 'S');

This is correct; the S parameter tells FPDF to return the document as a raw string rather than sending it directly to the browser. The problem arises when this string is split into chunks and attached to the email. In the provided example, the header for the attachment declares the content type as application/zip:

Content-Type: application/zip; name="attachment.zip" 

This is a definitive mismatch. FPDF produces PDF files, not ZIP archives. Email clients parse the Content-Type header to determine how to render or save the file. If an email client sees application/zip, it may attempt to extract the attachment as a compressed folder, leading to corruption of the binary data or a failure to recognize the file as a document. The correct MIME type for FPDF output is application/pdf.

Furthermore, the filename in the Content-Disposition header must accurately reflect the file extension expected by the client. Declaring it as attachment.zip reinforces the wrong content type. To fix this, you must align the Content-Type with application/pdf and update the filename to end in .pdf.

When constructing the multipart message using ob_start() and manual header injection, precision is key. The boundary strings must be unique across the entire email to prevent collisions between the text body parts and the attachment part. A common practice is to generate a unique hash based on the current time or random entropy:

$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

Once the headers are defined, the body of the email is constructed. The text alternative part should use multipart/alternative to support both plain text and HTML versions of the message. The attachment part follows immediately after, separated by the same boundary string.

Here is a corrected structure for the attachment section:

--PHP-mixed-".$random_hash." 
Content-Type: application/pdf; name=\"test.pdf\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename=\"test.pdf\" 

<?php echo $attachment; ?>
--PHP-mixed-".$random_hash."--

Using the native PHP mail() function is often a limiting factor in production environments. It lacks robust error handling, does not provide delivery receipts by default, and offers limited control over SMTP authentication or TLS encryption. If you are building an application where email reliability is critical, consider migrating to a library like PHPMailer or utilizing a framework-specific mailer if available. For instance, Laravel provides a highly configurable mail driver that supports SMTP directly, removing the need to manually construct MIME headers.

If you find yourself needing to generate complex PDFs with dynamic data and send them via email in a Laravel environment, the combination of FPDF for generation and the framework's built-in mailer ensures that headers are managed correctly without manual intervention. For more details on modern PHP email practices, you can refer to resources like https://laravelcompany.com, which often covers best practices for handling attachments and SMTP configuration in enterprise applications.

By correcting the MIME type from application/zip to application/pdf and ensuring filename consistency, the PDF attachment will render correctly in most email clients. Always verify that your output buffer is cleared correctly after constructing the message body before passing it to the mail function to avoid data leakage or corruption.

Our network