Send email with multiple attachments in PHP


Email is very useful thing for every web project. Every web project need an email system for sending notifications or information to the users. Many times we needs to send email with text or html content and attachment files.

In this tutorial we will discuss how to send email with single or multiple attachments in PHP. Our tutorial will help web developers to send text or html email including any types of files as an attachment (like images, .doc, .docx, .pdf, .txt, etc.). We have been made the whole process are very simple and easy. Please follow the short and easy tutorial given below. Also you can view the live demo from the above "Demo" link and download the full source code with step by step instruction from the above "Download" link.

Custom Function for Multiple Attachments Email:

At first we will create a function named multi_attach_mail(). This function required some arguments, like receiver email, email subject, email message, sender email, sender name and attachment files array. Please see the source code given below and follow the comment lines for better understanding.

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; }
}

Use the Function and Send Email:

Declare some variables, like receiver email, email subject, email message, sender email, and sender name. $to variable is used for hold the receiver email id, $from variable is used for hold the sender email id, $from_name variable is used for hold the sender name, $attachment_files variable is used for hold the attachment files path array, $subject variable is used for hold the email subject, $html_content variable is used for hold the email body content.

You only need to change two variables value. Firstly change the $to variable value with the receiver email id and secondly change the $attachment_files value with the attachment files path array. Now call the previously created function and pass the variables as the function arguments. Please see the source code given below and follow the comment line.

//email variables
$to 'receiver email id';
$from 'info@codexworld.com';
$from_name 'CodexWorld';
//attachment files path array
$files = array('screencapture-www-codexworld-com.png','Send_Email_with_pdf_attachment_in_PHP.docx');
$subject 'PHP Email with multiple attachments by CodexWorld'
$html_content '<h1>PHP Email with multiple attachments by CodexWorld</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 not SEND</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 'receiver email id';
$from 'info@codexworld.com';
$from_name 'CodexWorld';
//attachment files path array
$files = array('screencapture-www-codexworld-com.png','Send_Email_with_pdf_attachment_in_PHP.docx');
$subject 'PHP Email with multiple attachments by CodexWorld'
$html_content '<h1>PHP Email with multiple attachments by CodexWorld</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 not SEND</h1>";
?>

 If you have any query about this tutorials, please feel free to contact CodexWorld or you can post your valuable comments at the below comment form. We are always happy to hear from you.

9 Comments

  1. Sameer Said...
  2. Meryn Said...
  3. Sankhnad Mishra Said...
  4. Aryan Pradhan Said...
  5. Sukman Said...
  6. Rajeev Ranjan Said...
  7. Marco Said...
    • CodexWorld Said...
  8. Manoj Said...

Leave a reply

Connect With CodexWorld