PHP Mail with Attachment

Most of the webinfopedia users asking me to post article regarding the PHP email with attachment. So, here am going to explain you about PHP email with attachment. This will help you in website where you need to get some files from user and send to an email address. You can download the PHP email with attachment script.

PHP Mail with Attachment Script

You can download the original PHP email with attachment script from below:


First we need to create a HTML for to attach a file and send it across the PHP mail.

   // Set the "To" email address
	//Subject of the mail
   $subject="Join Us E-mail with Resume attachment";
   // Get the sender's name and email address plug them a variable to be used later
   $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">";
	// Check for empty fields
   if(empty($_POST['name'])  || empty($_POST['email']) || empty($_POST['message']))
		$errors .= "
 Error: all fields are required";
	// Get all the values from input
	$name = $_POST['name'];
	$email_address = $_POST['email'];
	$message = $_POST['message'];
	// Check the email address
	if (!eregi(	"^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email_address))
		$errors .= "Error: Invalid email address";
   // Now Generate a random string to be used as the boundary marker
   // Now Store the file information to a variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $file_name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   // Now here we setting up the message of the mail
$message = "Name: $name
            Email: $email_address 
            Message: $message 
            Here is your file: $file_name";
   // Check if the upload succeded, the file will exist
   if (file_exists($tmp_name))
      // Check to make sure that it is an uploaded file and not a system file
         // Now Open the file for a binary read
         $file = fopen($tmp_name,'rb');
         // Now read the file content into a variable
         $data = fread($file,filesize($tmp_name));
         // close the file
         // Now we need to encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));
      // Now we'll build the message headers
      $headers = "From: $from" .
         "MIME-Version: 1.0" .
         "Content-Type: multipart/mixed;" .
         " boundary="{$mime_boundary}"";
      // Next, we'll build the message body note that we insert two dashes in front of the  MIME boundary when we use it
      $message = "This is a multi-part message in MIME format." .
         "--{$mime_boundary}" .
         "Content-Type: text/plain; charset="iso-8859-1"" .
         "Content-Transfer-Encoding: 7bit" .
         $message . "";
      // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}" .
         "Content-Type: {$type};" .
         " name="{$file_name}"" .
         //"Content-Disposition: attachment;" .
         //" filename="{$fileatt_name}"" .
         "Content-Transfer-Encoding: base64" .
         $data . "" .
      // Thats all.. Now we need to send this mail... :)
      if (@mail($to, $subject, $message, $headers))
         echo '<div>
         <h1>Mail Sent successfully !!</h1>
         echo '<div><center>
         <h1>Error !! Unable to send Mail..</h1>

Done.. !! Now save the file in PHP and run.. You can send any file as attachment in PHP using this script. If you want to restrict for any file type give a condition before processing the attachment..

Hope that this will be helpful for you..

6 Comments PHP Mail with Attachment

  1. alagu

    I’m trying this code, but not sent, error only displayed, that Error !! Unable to send Mail… Can you pls help me.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>