Send attachments with PHP Mail()?

Send attachments with PHP Mail()?

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

Stefan Bogdanescu

Founder & Senior Architect

Send attachments with PHP Mail()?

The provided code snippet demonstrates a basic usage of the mail() function, but it lacks the necessary headers to support file attachments. While the code correctly sets up an HTML message and defines standard headers like From and Content-type, omitting specific MIME handling instructions means the email server will treat the attachment as plain text or ignore it entirely. To successfully send a PDF or any other file, you must construct a multipart MIME message that explicitly tells the recipient's mail client how to interpret the different parts of the email.

The core issue lies in the Content-type header. In your example, this is set to text/html. This header applies only to the body of the email. When an attachment is involved, the email no longer consists of a single part; it becomes a composite document containing both the text message and the binary file. You need to add a Content-Type header that declares the MIME structure as multipart/mixed. This specific type indicates that the message contains multiple parts with potentially different content types.

Furthermore, you must specify how the boundaries between these parts are handled. The MIME-Version: 1.0 header is present in your code, which is good, but it is incomplete without a boundary string. This boundary acts as a delimiter separating the email body from the attachment and defining the structure of the multipart message. Without this boundary, the receiving mail server cannot parse the incoming data correctly, often resulting in the attachment being stripped out or the entire message failing to load.

Here is how you can modify the headers to include a simple PDF attachment:

$to = "recipient@example.com";
$subject = "Invoice #12345";
$message = "Please find attached the invoice for your review.";
// Start multipart/mixed headers with a unique boundary string
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"----=_Part_".getmypid()."_" . time() . ".pdf\"\r\n";
$headers .= "From: sender@example.com <sender@example.com>\r\n";

// Define the attachment filename and path
$filename = "/path/to/invoice.pdf";

// Open a stream to read the file content
$handle = fopen($filename, 'rb');
$data = fread($handle, filesize($filename));
fclose($handle);

// Encode binary data to Base64 to ensure safe transmission
$encoded_data = base64_encode($data);

// Construct the full MIME message manually
$body = "----=_Part_".getmypid()."_" . time() . ".pdf\r\n";
$body .= "Content-Type: application/pdf; name=\"invoice.pdf\"\r\n";
$body .= "Content-Disposition: attachment; filename=\"invoice.pdf\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "\r\n";
$body .= $encoded_data . "\r\n";
$body .= "----=" . str_replace(".", "", getmypid() . "_" . time()) . " END\r\n";

mail($to, $subject, $body, $headers);

In this implementation, the boundary string is generated dynamically using getmypid() and a timestamp. This ensures uniqueness for each email session, preventing conflicts if multiple emails are sent concurrently or if the same boundary string is reused across different messages. The file data is read into memory and then base64-encoded before being inserted into the message body. Base64 encoding is essential because raw binary data can corrupt standard text-based email protocols; it transforms the binary PDF into an ASCII-safe string that can traverse email servers without corruption.

The Content-Disposition header sets the attachment to be displayed as a downloadable file rather than inline content. This is critical for documents like PDFs, where users expect to save or open them separately from the email body. If you were sending an image intended to display directly in the email client, you would change this to inline and assign it a unique ID matching the one used in the HTML <img> tag.

While manually constructing these strings works, it introduces complexity regarding boundary generation and error handling. For production environments where reliability is paramount, using a dedicated library is often preferred over raw string manipulation. The Laravel framework, for instance, abstracts this complexity through its mail service. If you are building applications with Laravel, the Mailable class handles MIME formatting automatically, allowing you to attach files via simple methods like $this->attach($path) without worrying about boundary strings or base64 encoding logic. This approach reduces the risk of implementation errors and ensures consistent behavior across different email clients.

Understanding these underlying mechanics is vital when debugging delivery issues. If an attachment fails to appear, checking whether the multipart/mixed header is present and correctly formatted is usually the first step. Additionally, verifying that the file path exists and is readable by the web server process is a common pitfall that often goes unnoticed until deployment.

Our network