Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Sending email with multiple attachment using php’s mail() function

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.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    
    // multipart boundary
    $msg = "This is a multi-part message in MIME format.\n\n" . "–{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";

    
    // 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: base64\n\n" . $data . "\n\n";

    }
    $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.
Share:

23 Comments

  • hi,
          I checked your multiple file attachment code in your site. I have a doubt regarding the files that we sent. can we send any type of file(pdf,zip,doc…)?
    Can you please give the code for sending form(contact form) with multiple file attachments?
        It will be helpful to me if u send the code to my email(kranthi.goli@vectramind.in).
                                                                               Kranthi. G

  • hi,
          I checked your multiple file attachment code in your site. I have a doubt regarding the files that we sent. can we send any type of file(pdf,zip,doc…)?
    Can you please give the code for sending form(contact form) with multiple file attachments?
        It will be helpful to me if u send the code to my email(kranthi.goli@vectramind.in).
                                                                               Kranthi. G

  • Susanta

    please give me full php code for an email with attachment

  • Shahbazahmed999

    Dear brother Please Can u Send me the Complete Code with Form Tags upload file , i trying to use but not getting success, plz i m waiting

  • Mayur Bhuva

    sir, please give me full php code for sending email

Leave a Reply

Your email address will not be published.