Responsive image

Call Us Today! 305 434 4698

Email :

Banner Image2
Recent Articles
Search Article
Article Category
A tutorial on how to send an e-mail with attached file using the PHP code
Tuesday, May 7, 2013    11:58 am

In this increasing and breath-taking competition in the Web Development field, you need to focus on all the points which are required to develop a good website. You can become the best Website Developer if you can survive in this competition and bring latest developments.

Today, everything has gone online. Even we can fill forms online. If you have such option in your website and want to know how to send an e-mail with attached file using the PHP code, then here you will find answers of your questions.  The form is created using PHP, when user submits that form with a file, we should receive an e-mail with attached file. To get this, we will discuss the code in this article. While developing the website in HTML/PHP, you need a code for contact form which can make it workable. Sometimes you provide provision to users to submit their image or any kind of attachment. For this, you need to follow the code with directions:

Following code is for the process of submitting contact form. Go through this tutorial, you will get thorough idea.

1.Make a file say contact_email.php with following code

For example

//Collect the information from the front end…

$email = $_REQUEST['email'];

$name = $_REQUEST['name'];

$phone = $_REQUEST['phone'];

$message = $_REQUEST['message'];

//Display information

$str = “



<tr><td><b>Users Information:</b></td></tr>

<tr><td width=’168′>First Name : </td><td>”.$name.”</td></tr>

<tr><td width=’168′>Email Id : </td><td>”.$email.”</td></tr>

<tr><td width=’168′>Phone Number : </td><td>”.$phone.”</td></tr>

<tr><td width=’168′>Message : </td><td>”.$message.”</td></tr>


<br /><br />”;

$message = $str;

$to  = ‘’;

//This is the code for file attachment

$fileatt      = $_FILES['image']['tmp_name'];

$fileatt_type = $_FILES['image']['type'];

$fileatt_name = $_FILES['image']['name'];

if (is_uploaded_file($fileatt)) {

$headers = “From: “.$email ;

$file = fopen($fileatt,’rb’);

$data = fread($file,filesize($fileatt));


// Generate a boundary string

$semi_rand = md5(time());

$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;

// Add the headers for a file attachment

$headers .= “\nMIME-Version: 1.0\n” .

            ”Content-Type: multipart/mixed;\n” .

            ” boundary=\”{$mime_boundary}\”";

// Add a multipart boundary above the plain message

$message = “This is a multi-part message in MIME format.\n\n” .

           ”–{$mime_boundary}\n” .

           ”Content-Type: text/html; charset=\”iso-8859-1\”\n” .

           ”Content-Transfer-Encoding: 7bit\n\n” .

           $message . “\n\n”;

// Base64 encode the file data

$data = chunk_split(base64_encode($data));

 // Add file attachment to the message

 $message .= “–{$mime_boundary}\n” .

            ”Content-Type: {$fileatt_type};\n” .

            ” name=\”{$fileatt_name}\”\n” .

            ”Content-Disposition: attachment;\n” .

            ” filename=\”{$fileatt_name}\”\n” .

            ”Content-Transfer-Encoding: base64\n\n” .

            $data . “\n\n” .





$headers  = ‘ MIME-Version: 1.0′ . “\r\n”;

$headers = ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;

$headers .= “From: “.$email ;


$subject = “ contact_us page”;

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


2. Then connect it with HTML form.

3. Now, reload the page.

4. Fill the form and press on submit button

5. Check the e-mail on client’s e-mail id for getting attached file.

If you need to understand it more clearly, you can watch the video here.

By: admin

[Go Back]