How to send multiple files through email using 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.
Please check my latest Video tutorial on: Send Email with Attachment Using PHP, jQuery, PHPMailer & Ajax from Scratch to Advanced
Video Tutorials on Youtube: Send Email with Attachment Using PHP, Ajax, jQuery & PHPMailer
Steps:
Create to two files
1. contact_form.php
2. mail.php
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Send mutiple files</title> </head> <body> <div style="width:40%"> <img src="email.jpg" style="float:left; margin-top:80px; margin-left:60px" /> </div> <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" /> <br> <label>Phone :</label><input name="phone" type="text" style="margin-left:40px; margin-top:20px" /> <br> <label>Email :</label><input name="email" type="text" style="margin-left:43px; margin-top:20px" /> <br> <label>Message :</label><textarea name="message" cols="20" rows="5" style="margin-left:24px; margin-top:20px"></textarea> <br> <label>Cover Letter Attachment :</label><input name="upload" id="upload" type="file" style="margin-left:15px; margin-top:20px"/> <br> <label>CV Attachment :</label><input name="upload1" id="upload1" type="file" style="margin-left:88px; margin-top:20px"/> <br> <label>Resume Attachment :</label><input name="upload2" id="upload2" type="file" style="margin-left:50px; margin-top:20px"/> <br> <label>Other Attachment :</label><input name="upload3" id="upload3" type="file" style="margin-left:65px; margin-top:20px"/> <br> <input name="submit" type="submit" style="margin-left:120px; margin-top:20px"/> </form> </div> </body> </html>
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
<?PHP error_reporting(0); $to='muni2explore@gmail.com'; $email = $_POST['email']; //$bodymsg=$_POST['message']; $name=$_POST['name']; $phone=$_POST['phone']; $message=$_POST['message']; $sub=" <table width='500' border='1' align='center'> <tr> <td>Name :</td> <td>$name</td> </tr> <tr> <td>Phone :</td> <td>$phone</td> </tr> <tr> <td>Message</td> <td>$message</td> </tr> <tr> <td>Email</td> <td>$email</td> </tr> </table>"; $bound_text = "jimmyP123"; $bound = "--".$bound_text."\r\n"; $bound_last = "--".$bound_text."--\r\n"; $headers = "From: admin@server.com\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" .$bound; $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n" ."$sub\r\n" .$bound; //getting temporary file locations with name $a=file_get_contents($_FILES['upload']['tmp_name']); $b=file_get_contents($_FILES['upload1']['tmp_name']); $c=file_get_contents($_FILES['upload2']['tmp_name']); $d=file_get_contents($_FILES['upload3']['tmp_name']); //getting original name of the file you uploaded $name=$_FILES["upload"]["name"]; $name1=$_FILES["upload1"]["name"]; $name2=$_FILES["upload2"]["name"]; $name3=$_FILES["upload3"]["name"]; $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" ."Content-Transfer-Encoding: base64\r\n" ."Content-Disposition: attachment;\n" . " filename=\"$name\"\n" ."\r\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" ."\r\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" ."\r\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" ."\r\n" .chunk_split(base64_encode($d)) .$bound_last; 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]gmail.com
Note: But it will be charged based on your customization requirement