How to attach two or multiple files and send mail in PHP

How to attach two or multiple files and send mail in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Multiple Attachments in PHP Email

The provided snippet demonstrates how to construct a raw MIME message using mail(), but it is fundamentally limited to a single attachment. The logic relies on reading one file into memory, encoding it with Base64, and appending it to the email body string before closing the multipart boundary. To attach two files—such as a .rar archive and a PDF—you cannot simply duplicate this block for the second file; doing so would break the MIME structure or result in only the last attachment being rendered correctly depending on how the parser handles duplicate boundaries.

The core issue lies in the Content-Type: multipart/mixed header. This header defines a container that holds multiple parts. Each part represents either the email body or an attachment. To add a second file, you must insert another part between the closing delimiter of the first attachment and the final boundary marker (--{$mime_boundary}--).

Constructing the MIME Body Manually

If you are sticking with the manual mail() approach shown in your source material, you need to treat the email body as a string builder. You read the first file, encode it, and append it to the message. Then, before closing the loop or the boundary, you open the second file, encode it, and append that block.

Consider this corrected logic flow for attaching both example.rar and document.pdf:

$email_to = "recipient@example.com";
$email_from = "online@example.co.in";
$email_subject = "Report with attachments";
$email_txt = "Please find the attached RAR and PDF.";

// Path 1: RAR file
$fileatt1 = "files/example.rar";
$fileatt_type1 = "application/x-rar-compressed";
$fileatt_name1 = "example.rar";

// Path 2: PDF file
$fileatt2 = "files/document.pdf";
$fileatt_type2 = "application/pdf";
$fileatt_name2 = "document.pdf";

$fileatt1_data = fread(file($fileatt1, 'rb'), filesize($fileatt1));
fclose(file($fileatt1, 'r')); // Close after read
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers = "From: $email_from\r\n" .
    "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"";

$message = "This is a multi-part message in MIME format.\r\n\r\n" .
    "--{$mime_boundary}\r\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" .
    "Content-Transfer-Encoding: 7bit\r\n\r\n" . $email_txt;

// Append First Attachment
$message .= "\r\n--{$mime_boundary}\r\n" .
    "Content-Type: {$fileatt_type1}; name=\"{$fileatt_name1}\"\r\n" .
    "Content-Disposition: attachment; filename=\"{$fileatt_name1}\"\r\n" .
    "Content-Transfer-Encoding: base64\r\n\r\n" .
    chunk_split(base64_encode($fileatt1_data)) . "\r\n--{$mime_boundary}\r\n";

// Append Second Attachment
$fileatt2_data = fread(file($fileatt2, 'rb'), filesize($fileatt2));
fclose(file($fileatt2, 'r'));
$message .= "Content-Type: {$fileatt_type2}; name=\"{$fileatt_name2}\"\r\n" .
    "Content-Disposition: attachment; filename=\"{$fileatt_name2}\"\r\n" .
    "Content-Transfer-Encoding: base64\r\n\r\n" .
    chunk_split(base64_encode($fileatt2_data)) . "\r\n--{$mime_boundary}--\r\n";

mail($email_to, $email_subject, $message, $headers);

Notice the critical addition of Content-Disposition: attachment for the second file. Without this header, some email clients might treat the second chunk as inline content or fail to recognize it as a downloadable attachment. Also, ensure you do not forget the final --{$mime_boundary}-- at the very end of the string, which signals the end of the MIME container.

The Memory Limit Trap

A significant pitfall with this manual approach is PHP's memory usage. The code reads the entire file into a variable using fread(). If you are sending large files (e.g., 50MB+), filesize() might exceed php.ini's memory_limit, causing a fatal error before the email even sends. Additionally, storing binary data in variables can sometimes trigger warnings depending on the PHP version and configuration regarding large string allocations.

For production environments handling multiple or large files, relying on mail() with raw string manipulation is often brittle. It requires meticulous attention to line endings (CRLF vs LF) and header formatting. Modern frameworks abstract this complexity away. For instance, the Laravel framework provides a robust mailer that handles multipart encoding automatically. You can use the attach() method multiple times on a single message instance:

use Illuminate\Support\Facades\Mail;

Mail::raw('This is the email body', function ($message) {
    $message->to('recipient@example.com')
            ->subject('Multi-attachment email')
            ->attach(public_path('files/example.rar'), [
                'as' => 'example.rar',
                'mime' => 'application/x-rar-compressed',
            ])
            ->attach(public_path('files/document.pdf'), [
                'as' => 'document.pdf',
                'mime' => 'application/pdf',
            ]);
});

This approach pushes the boundary management to the framework, reducing the risk of malformed headers. If you are maintaining a legacy PHP application without a framework but need reliability for multiple attachments, consider upgrading to PHPMailer or using an SMTP library rather than building the MIME structure manually from scratch.

Our network