How To Send Email With Multiple Attachments

How To Send Email With Multiple Attachments

How To Send Email With Multiple Attachments

0 Sales


Sending email from a script is very useful for a dynamic web application. The email send functionality can easily be integrated with PHP. The PHP mail) (function provides a simple way to send emails from the script. You can send emails containing text or HTML content using the PHP mai(l) function.


In addition to your HTML content, you can send an attachment to your email using PHP. Use some additional headers in the mail() function of PHP to send email with pdf / image / word attachment. In this tutorial, we will show you how to send a single or multiple attachment email in PHP.


Our sample script enables web developers to send text or HTML message, including any file type (such as image,.doc,.docx,.pdf,.txt, etc.) as a PHP attachment. Our example software makes the whole process easy and you can send multiple attachments by calling a single function.


Send HTML Email with Multiple Attachments


To make it easier to use, we will build a custom PHP framework and all the code will be grouped together in this module. Multi_attach_mail() allows to send multiple attachments to email using PHP.

Multi attach mail() requires the following parameters.


  • $to — Required. Specify the email address of the sender.

  • $subject – Required. Specify the subject of the email.

  • $message — Required. Specify the substance of the email body (text or HTML).

  • $senderEmail - Required. Specify the email address of the sender.

  • $senderName — Required. Specify the name of the recipient.

  • $files — Optional. The path array of files to attach to the email.


This role returns TRUE to performance or FALSE to failure.



 * Custom PHP function to send an email with multiple attachments

 * $to Recipient email address

 * $subject Subject of the email

 * $message Mail body content

 * $senderEmail Sender email address

 * $senderName Sender name

 * $files Files to attach with the email


function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $files = array()){

    $from = $senderName." <".$senderEmail.">"; 

    $headers = "From: $from";

    // Boundary 

    $semi_rand = md5(time()); 

    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // Headers for attachment 

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

    // Multipart boundary 

    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .

    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

    // Preparing attachment




                $file_name = basename($files[$i]);

                $file_size = filesize($files[$i]);


                $message .= "--{$mime_boundary}\n";

                $fp =    @fopen($files[$i], "rb");

                $data =  @fread($fp, $file_size);


                $data = chunk_split(base64_encode($data));

                $message .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n" . 

                "Content-Description: ".$file_name."\n" .

                "Content-Disposition: attachment;\n" . " filename=\"".$file_name."\"; size=".$file_size.";\n" . 

                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";





    $message .= "--{$mime_boundary}--";

    $returnpath = "-f" . $senderEmail;


    // Send email

    $mail = @mail($to, $subject, $message, $headers, $returnpath); 


    // Return true, if email sent, otherwise return false


        return true;


        return false;




Send Email with Multiple Attachment using Custom PHP Function


The following example shows how you can use our custom PHP function to send multiple attachments to emails. 

Call a multi_attach_mail() function and pass the required parameters.


  • $to – Receiver email ID

  • $from – Sender email ID

  • $fromName – Sender name

  • $subject – Email subject

  • $files – Attachment file path in array format

  • $htmlContent – Email body content


// Email configuration

$to = '[email protected]';

$from = '[email protected]';

$fromName = 'Sender Name';

$subject = 'Send Email with Multiple Attachments in PHP by CodexWorld'; 

// Attachment files

$files = array(




$htmlContent = '

    <h3>Email with Multiple Attachments by CodexWorld</h3>

    <h4>This HTML email has sent from the script with multiple attachments using PHP.</h4>

    <p><b>Total Attachments:</b> '.count($files).'</p>';

// Call function and pass the required arguments

$sendEmail = multi_attach_mail($to, $subject, $htmlContent, $from, $fromName, $files);

// Email sending status


    echo 'The email has sent successfully.';


    echo 'Mail sending failed!';


The final conclusion:


An email with attachment feature is very useful if you want to allow the client to submit a form with a file and send the form data to an address. Our example script allows you to send any sort of file as an email attachment from the script. You can also enhance the functionality of multiple email attachments to the script as necessary.



You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : May 14, 2020

Updated Date : May 14, 2020


Comments : 0

Downloads : 0