Hello Friends,

All we know how to send email in php. You might not be aware with sending email with attachment in php. If so than refer my previous article which explains how to send email with attachment(single). Today i have a special requirement where i need to send multiple attachments (more than one) in mail. If you are looking for send function for sending email with multiple attachment in php than here i am sharing the function using which you can send an email with mulitple attachment using php's mail function.

<?php
 
    $files = array("file_1.ext","file_2.ext","file_3.ext",……); // files
    
    $to = "raxit4u2@gmail.com";
    $from = "raxit4u2@yahoo.com";
    $sub ="Testing for multiple attachment";
    $msg = "Attachment mail testing";
    $headers = "From: $from";
    
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    // headers for attachment
    $headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}"";
    
    // multipart boundary
    $msg = "This is a multi-part message in MIME format.nn" . "–{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $msg . "nn";

    
    // attachments code starts
    for($x=0;$x<count($files);$x++)
    {         $msg .= "–{$mime_boundary}n";
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {"application/octet-stream"};n" . " name="$files[$x]"n" .
        "Content-Disposition: attachment;n" . " filename="$files[$x]"n" .
        "Content-Transfer-Encoding: base64nn" . $data . "nn";

    }
    $msg .= "–{$mime_boundary}–n";     
    $res = @mail($to, $sub, $msg, $headers);
    
    if($res)
    {
        echo "<p>mail sent with attachments</p>";
    }
    else
    {
        echo "<p>error in processing email!</p>";
    }
 
?>

Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.

To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database and Open-source, enter your email address below. We will send you free tutorials.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.
rax rss Sending email with multiple attachment using phps mail() function   rax twitter Sending email with multiple attachment using phps mail() function   rax facebook Sending email with multiple attachment using phps mail() function