How to send multiple files through email using php

Posted by & filed under HTML5, PHP.

Here is the php script to send multiple files in single mail. I had modified previous tutorial php script  (how to send an email with file attachment using php) little bit to send multiple files in a single mail.  I had given both html form codes and php scripts, and you just need to follow steps to implement this script in your site.


Create to two files

1. contact_form.php

2. mail.php

Send mutiple files in email using php- html email form

HTML Email Form





I had designed html form with eight fields to get an user’s input. Here is html form script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send mutiple files</title>

<div style="width:40%">
<img src="email.jpg" style="float:left; margin-top:80px; margin-left:60px" />
<div  style="width:60%; float:right; margin-left:20px;margin-top:80px; color:#06F; font-size:20px">
<form action="mail.php" method="post" enctype="multipart/form-data" name="form">
<label>Name :</label><input name="name" type="text" style="margin-left:40px; margin-top:20px" />
<label>Phone :</label><input name="phone" type="text" style="margin-left:40px; margin-top:20px" />
<label>Email :</label><input name="email" type="text" style="margin-left:43px; margin-top:20px" />
<label>Message :</label><textarea name="message" cols="20" rows="5" style="margin-left:24px; margin-top:20px"></textarea>
<label>Cover Letter Attachment :</label><input name="upload" id="upload" type="file" style="margin-left:15px; margin-top:20px"/>
<label>CV Attachment :</label><input name="upload1" id="upload1" type="file" style="margin-left:88px; margin-top:20px"/>
<label>Resume Attachment :</label><input name="upload2" id="upload2" type="file" style="margin-left:50px; margin-top:20px"/>
<label>Other Attachment :</label><input name="upload3" id="upload3" type="file" style="margin-left:65px; margin-top:20px"/>
<input name="submit" type="submit" style="margin-left:120px; margin-top:20px"/>


In the php script I am getting information from html form using super global variable $_FILE and and concatenated each files in the $message variable to send an email. I had given comments in between scripts to explain each lines of code.

Here is php script



$email = $_POST['email'];


<table width='500' border='1' align='center'>
    <td>Name :</td>

    <td>Phone :</td>




$bound_text = "jimmyP123";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From:\r\n";
$headers .= "MIME-Version: 1.0\r\n"."Content-Type: multipart/mixed; boundary=\"$bound_text\"";

$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"

//getting temporary file locations with name

//getting original name of the file you uploaded



$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment;\n" . " filename=\"$name\"\n"
.chunk_split(base64_encode($a)).$bound ;

$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name1\"\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment;\n" . " filename=\"$name1\"\n"
.chunk_split(base64_encode($b)).$bound ;

$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name2\"\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment;\n" . " filename=\"$name2\"\n"
.chunk_split(base64_encode($c)).$bound ;

$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name3\"\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment;\n" . " filename=\"$name3\"\n"

mail($to, $subject, $message, $headers);

echo "success";



If you liked this blog tutorial don’t forget subscribe (using subscribe box in the right sidebar) this blog tutorials to keep in touch regular updates.




Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni2explore[at]

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

%d bloggers like this:

Get Instant Script Download Access!