Send Email with Multiple Attachments in PHP

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

Along with the HTML content, you can send the attachment with the email using PHP. Use some additional headers in the PHP mail() function to send emails with pdf/image/word attachments. This tutorial will show you how to send emails with single or multiple attachments in PHP.

Our example script helps web developers to send text or HTML emails including any type of file (like image, .doc, .docx, .pdf, .txt, etc.) as an attachment in PHP. To make the whole process simple, we will group the entire code into a function. You can send emails with multiple attachments by calling a single function.

Send HTML Email with Multiple Attachments

We’ll create a custom PHP function and all the code will be grouped in this function for better usability. The multi_attach_mail() helps to send email with multiple attachments using PHP.
The multi_attach_mail() function requires the following parameters.

  • $to – Required. Specify the recipient’s email address.
  • $subject – Required. Specify email subject.
  • $message – Required. Specify email body content (text or HTML).
  • $senderEmail – Required. Specify the sender’s email address.
  • $senderName – Required. Specify sender name.
  • $files – Optional. An array of files path to attach with the email.

This function returns TRUE on success or FALSE on error.

/* 
 * 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()){
   // Sender info 
    
$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
    
if(!empty($files)){
        for(
$i=0;$i<count($files);$i++){
            if(
is_file($files[$i])){
                
$file_name basename($files[$i]);
                
$file_size filesize($files[$i]);
                
                
$message .= "--{$mime_boundary}\n";
                
$fp =    @fopen($files[$i], "rb");
                
$data =  @fread($fp$file_size);
                @
fclose($fp);
                
$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
    
if($mail){
        return 
true;
    }else{
        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 email with multiple attachments. Call the multi_attach_mail() function and pass the required parameters.

  • $to – Receiver email address.
  • $from – Sender email address.
  • $fromName – Sender name.
  • $subject – Subject of the email.
  • $files – Attachment files path in array format.
  • $htmlContent – Email body content.
// Email configuration 
$to 'recipient@example.com';
$from 'sender@example.com';
$fromName 'Sender Name';

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

// Attachment files
$files = array(
    
'files/codex-web-view.png',
    
'files/codex-site.pdf'
);

$htmlContent '
    <h3>Email with Multiple Attachments by CodexWorld</h3>
    <h4>This HTML email is 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
if($sendEmail){
    echo 
'The email is sent successfully.';
}else{
    echo 
'Email sending failed!';
}

Send Email with Attachment on Form Submission using PHP

Conclusion

The email with attachment functionality is very useful when you want to allow the user to submit the form with files and send the form data to an email. Our example script allows you to send any type of file as an attachment with the email from the script. You can also enhance or customize the functionality of multiple email attachments script as per your needs. Download the source code to get the email form script with attachments sending functionality.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

19 Comments

  1. Jonathan Said...
  2. Ivan Said...
  3. Mike Said...
  4. Charles Wood Said...
  5. Thieberson Said...
  6. Raveendran Said...
  7. Brizbhushan Yadav Said...
  8. Phani Said...
  9. Pradeep Said...
    • CodexWorld Said...
  10. Sameer Said...
  11. Meryn Said...
  12. Sankhnad Mishra Said...
  13. Aryan Pradhan Said...
  14. Sukman Said...
  15. Rajeev Ranjan Said...
  16. Marco Said...
    • CodexWorld Said...
  17. Manoj Said...

Leave a reply

keyboard_double_arrow_up