Send Email with Multiple Attachments in PHP

Sending email from the script is the very useful functionality for every web project. Every web project needs an email system for sending notifications or information to the users. Sometimes we need to send email with text or HTML content and attachments. You can easily send email from script using mail() function in PHP.

In this tutorial, we will show you how to send email with single or multiple attachments in PHP. It helps web developers to send text or HTML email including any types of files as an attachment (like images, .doc, .docx, .pdf, .txt, etc.). Our example script makes the whole process simple and you can send email with multiple attachments by calling a single function.

Send HTML Email with Multiple Attachments

For better usability, we’ll create a function named multi_attach_mail() and all the code will be grouped together in this function. The multi_attach_mail() function requires the following parameters.

  • $to – Required. Specify recipient email address.
  • $subject – Required. Specify email subject.
  • $message – Required. Specify email body content (text or HTML).
  • $senderMail – Required. Specify sender email address.
  • $senderName – Required. Specify sender name.
  • $files – Required. An array of files path to attach with the email.
function multi_attach_mail($to$subject$message$senderMail$senderName$files){

    
$from $senderName." <".$senderMail.">"
    
$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 attachments
    
if(count($files) > 0){
        for(
$i=0;$i<count($files);$i++){
            if(
is_file($files[$i])){
                
$message .= "--{$mime_boundary}\n";
                
$fp =    @fopen($files[$i],"rb");
                
$data =  @fread($fp,filesize($files[$i]));
                @
fclose($fp);
                
$data chunk_split(base64_encode($data));
                
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" 
                
"Content-Description: ".basename($files[$i])."\n" .
                
"Content-Disposition: attachment;\n" " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" 
                
"Content-Transfer-Encoding: base64\n\n" $data "\n\n";
            }
        }
    }
    
$message .= "--{$mime_boundary}--";
    
$returnpath "-f" $senderMail;
    
//send email
    
$mail = @mail($to$subject$message$headers$returnpath); 
    
//function return true, if email sent, otherwise return fasle
    
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. You need to call the multi_attach_mail() function and pass the required parametters – $to (receiver email id), $from (sender email id), $from_name (sender name), $attachment_files (attachment files path array), $subject (email subject), $html_content (email body content).

//email variables
$to '[email protected]';
$from '[email protected]';
$from_name 'semicolonworld';
//attachment files path array
$files = array('semicolonworld-image.png','semicolonworld-file.docx');
$subject 'PHP Email with multiple attachments by semicolonworld'
$html_content '<h1>PHP Email with multiple attachments by semicolonworld</h1>
            <p><b>Total Attachments : </b>'
.count($files).' attachments</p>';
//call multi_attach_mail() function and pass the required arguments
$send_email multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);
//print message after email sent
echo $send_email?"<h1>Mail Sent</h1>":"<h1>Mail sending failed.</h1>";

Full Source Code

<?php
function multi_attach_mail($to$subject$message$senderMail$senderName$files){
    
$from $senderName." <".$senderMail.">"
    
$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 attachments
    
if(count($files) > 0){
        for(
$i=0;$i<count($files);$i++){
            if(
is_file($files[$i])){
                
$message .= "--{$mime_boundary}\n";
                
$fp =    @fopen($files[$i],"rb");
                
$data =  @fread($fp,filesize($files[$i]));
                @
fclose($fp);
                
$data chunk_split(base64_encode($data));
                
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" 
                
"Content-Description: ".basename($files[$i])."\n" .
                
"Content-Disposition: attachment;\n" " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" 
                
"Content-Transfer-Encoding: base64\n\n" $data "\n\n";
            }
        }
    }
    
$message .= "--{$mime_boundary}--";
    
$returnpath "-f" $senderMail;
    
//send email
    
$mail = @mail($to$subject$message$headers$returnpath); 
    
//function return true, if email sent, otherwise return fasle
    
if($mail){ return TRUE; } else { return FALSE; }
}
//email variables
$to '[email protected]';
$from '[email protected]';
$from_name 'semicolonworld';
//attachment files path array
$files = array('semicolonworld-image.png','semicolonworld-file.docx');
$subject 'PHP Email with multiple attachments by semicolonworld'
$html_content '<h1>PHP Email with multiple attachments by semicolonworld</h1>
            <p><b>Total Attachments : </b>'
.count($files).' attachments</p>';
//call multi_attach_mail() function and pass the required arguments
$send_email multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);
//print message after email sent
echo $send_email?"<h1>Mail Sent</h1>":"<h1>Mail sending failed.</h1>";
?>

Related Articles

Comments 0