How to Send Email with Multiple Attachments in PHP

 


The dynamic web application can send emails directly from the script. PHP can be used to integrate email sending functionality. From a PHP script, you can create an email by using the mail() function. With PHP mail(), you can send emails that contain text or HTML.

You can attach a file to the email along with HTML content using PHP. To send an email with a pdf/image/word attachment, add a few additional headers to the PHP mail() function. The following tutorial demonstrates how to send emails with attachments in PHP.

As an example script, it shows how web developers can attach any type of file (image, .doc, .docx, .pdf, .txt, etc.) to text or HTML emails in PHP. Rather than grouping all the code in one function, we will group it into one. We can send emails to multiple recipients by calling one function.

How to Send Email with Multiple Attachments in PHP

In order to make the code more usable, we will create a custom PHP function. Using multi_attach_mail(), emails can be sent with multiple attachments.Following are the parameters required by multi_attach_mail().

  • $to – Required. Specify recipient email address.
  • $subject – Required. Specify email subject.
  • $message – Required. Specify email body content (text or HTML).
  • $senderEmail – Required. Specify sender 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

This example demonstrates how to send multiple attachments via our custom PHP function. You simply call the function and pass the parameters you wish to use.

  • $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!'; 

Conclusion

If you want to let your users submit forms with attachments and send the form data to an email, the attachment functionality is very useful. As an attachment to the email, this script allows you to send any type of file. Multiple email attachments script can also be enhanced or customized as per your requirements.

*

Post a Comment (0)
Previous Post Next Post