PHP

How to send attachment in an email using PHP

This tutorial will explain to you How to send attachment in an email using PHP by uploading the file using HTML form with a simple example. You will also learn how you can send multiple attachments by some little modification in the code of single file attachment.

First of all, we need an HTML Form through which we will upload the file. Below is the HTML Form code:

<form action="sendemail.php" method='post' enctype="multipart/form-data">
<input type='file' name='file' />
<input type='submit' value='Submit' />
</form>

Note the “enctype” attribute in the form tag. This is an attribute that specifies the encoding type used while sending data to the server. Whenever we use any file input tag in our we need to specify this attribute in form tag with value “multipart/form-data”. This encoding type “multipart/form-data” tells that no character will be encoded while sending. It helps in preserving the data of the uploaded file. One more thing to note that the enctype attribute is used only with post method.

Now let’s take a look at the PHP file sendemail.php that does the actual task of sending uploaded file to the email address.

<?php
$fromemail = "[email protected]";
$subject="Uploaded file attachment";	
$body="Please find the attachment";
$strSid = md5(uniqid(time()));
$headers ="From: $fromemail" . "\n";
$headers .="MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n"; 
$headers .="--".$strSid."\n";  
$headers .="Content-type: text/html; charset=utf-8\n";  
$headers .="Content-Transfer-Encoding: 7bit\n\n";  
$headers .=$body."\n";
if($_FILES["file"]["name"]!= ""){  
  $strFilesName = $_FILES["file"]["name"];  
  $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));  
  $headers .= "--".$strSid."\n";  
  $headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
  $headers .= "Content-Transfer-Encoding: base64\n";  
  $headers .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n";  
  $headers .= $strContent."\n";  
}
$toemail="[email protected]";	
if(mail($toemail, $subject, $body, $headers))
   echo "Email send successfully with attachment";

Files in the email are being sent in base64 encoding format. In the above code, we are converting the content of the uploaded file into base64 format and then splitting the content into small chunks using chink_split function in case the file size is bigger.

Similarly, if we want to send multiple files in attachment, we just need to made some modification in the code of both files to make it work in case of multiple file attachment and sending it to an email address. Below we’ll take a look of the revised version of both files to accomplish the task of sending multiple files in an email attachment.

<form action="sendemail.php" method='post' enctype="multipart/form-data">
<input type='file' name='file[]' multiple='multiple' />
<input type='submit' value='Submit' />
</form>

Note the two addition I made in the above code. First, addition is adding brackets in name attributes of input file tag and the second addition is multiple attributes. This attribute “multiple” allows us to upload multiple files and those files are assigned to an array of files specified by name “file[]”.

<?php
$fromemail = "[email protected]";
$subject="Uploaded file attachment";	
$body="Please find the attachment";
$strSid = md5(uniqid(time()));
$headers .="From: $fromemail" . "\r\n";
$headers .="MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n"; 
$headers .="--".$strSid."\n";  
$headers .="Content-type: text/html; charset=utf-8\n";  
$headers .="Content-Transfer-Encoding: 7bit\n\n";  
$headers .=$body."\n\n";
for($i=0;$i<count($_FILES['file']['name']);$i++){
  if($_FILES["file"]["name"][$i]!= ""){  
    $strFilesName = $_FILES["file"]["name"][$i];  
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"][$i])));  
    $headers .= "--".$strSid."\n";  
    $headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
    $headers .= "Content-Transfer-Encoding: base64\n";  
    $headers .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
    $headers .= $strContent."\n\n";  
  }
}
$toemail="[email protected]";	
if(mail($toemail, $subject, $body, $headers))
  echo "Email send successfully with attachment";

 

In above code, there is just one addition that is a for loop around the if condition that collects each individual files from the “file[]” array passed from the HTML Form and pass it for further processing as we done in the earlier version of sendemail.php

About the author

Sujeet Kr Singh