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>";
}
?>
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.
Posts Related to Sending email with multiple attachment using php's mail() function
POP3/IMAP Email with attachment-Read & Forwarding with PHP
Hello Friends, Do you know how we can read, forward or transfer the POP3/IMAP email to another email address ? I came across this situation. ...
Sending email with attachment using php’s mail() function
Hello Friends, All we know how to send email in php. If you are begineer in php than i must say php provides inbuilt mail ...
How to send email from localhost(XAMPP or WAMP) in PHP in Windows
Hello Friends, Whenever you works in local server (XAMPP or WAMP) for developing any email functionality in website, than you might be wonder that why ...
Export MYSQL data into Excel/CSV via php
Hello Friends, Today i came across a functionality where i need to Export the MYSQL data into CSV/Excel file via PHP function/script. There are such ...
Import CSV/Excel data into MYSQL database via PHP
Hello Friends, Today i came across a functionality where i need to import the CSV/Excel file in to MYSQL database via PHP script. There is ...
You can leave a response or trackback from your own site.






I am
about 1 year ago
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
about 1 year ago
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
about 1 year ago
It is for any type of file. Your attachment can be pdf,doc,zip etc.
about 1 year ago
It is for any type of file. Your attachment can be pdf,doc,zip etc.
about 1 year ago
please give me full php code for an email with attachment
about 1 year ago
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
about 1 year ago
sir, please give me full php code for sending email
about 11 months ago
Thank you for this code, but would you be able to explain how to intergrate it with a form? Or, as requested by other people in this post, please email me the code if possible. Many thanks
about 11 months ago
Thank you for this code, but would you be able to explain how to intergrate it with a form? Or, as requested by other people in this post, please email me the code if possible. Many thanks
about 7 months ago
rakesh.,
i want upload xmlfile into mysql
but should not depend on tag’s…
has u done for excel sheet…
about 6 months ago
Copy and paste this and tell me you don’t get syntax errors all over.
There are a LOT of slashes missing here dude.
about 5 months ago
If sending from a web server, how do you indicate the path from the site root to each file if each might be different:
array(“file_1.ext”,”file_2.ext”,”file_3.ext”,……)
Is it “/files/file_1.ext”,”/images/file_2.ext”…?
about 3 months ago
You have done great job. I spent many hours to search the code for this and finally I came across this and I got the result. There is little quote issue which I have solved. It helps me a lot.
Great! Great! Thanks. You Rocks.
about 2 months ago
how to publish flash file hrough java script in word press