Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

How to send attachments using PHP Mail()

How to send attachments using PHP Mail()

In this post, we will going to learn how to can send an email with attachments using PHP mail() function. Sending an attachment with PHP mail() function is painful so people prefer to use PHP Mailer library, but in this post we are not going to use any library, we send attachments using PHP  mail() function and will Jquery and Ajax, so can send mail with out any page refresh.

We have to create to file on question.php , send.php , question.php .
Create question.php with the code given below better copy paste in body tag…

<link href="//" rel="stylesheet" />
<script src=""></script>
<form id="preparingMathForm" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-12 form-group">
<label for="">Student's Name *</label>
<input type="text" name="student_name" class="form-control" placeholder="Name of Student " required/>
<div class="row">
<div class="col-md-12 form-group">
<label for="">Parent / Guardian's Name *</label>
<input type="text" name="parent_name" class="form-control" placeholder="Parent / Guardian's Name" required/>
<div class="row">
<div class="col-md-6 form-group">
<label for="">Contact No *</label>
<input type="text" name="contact_no" class="form-control" placeholder="Contact No" required/>
<div class="col-md-6 form-group">
<label for="">Email ID *</label>
<input type="text" name="email" class="form-control" placeholder="Email ID" required/>
<div class="row">
<div class="col-md-6 form-group">
<label for="">School Name *</label>
<input type="text" name="school_name" class="form-control" placeholder="School Name" required/>
<div class="col-md-6 form-group">
<label for="">Select a Class *</label>
<select name="student_class" id="student_class" class="change form-control" required>
<option value="">Select a Class</option>
<option value="V">V</option>
<option value="VI">VI</option>
<option value="VII">VII</option>
<option value="VIII">VIII</option>
<option value="IX">IX</option>
<option value="X">X</option>
<div class="row">
<div class="col-md-6 form-group">
<label for="">Topic Name *</label>
<input type="text" name="topic_name" class="form-control" placeholder="Topic Name" required/>
<div class="row">
<div class="col-md-12 form-group">
<label for="">Write the Problem *</label>
<textarea name="problem" class="form-control" rows="7" placeholder="Write the Problem" required></textarea>
<div class="row">
<div class="col-md-12 form-group">
<label for="">Attach the Screen shot ( Can use Jpg, PNG, Maximum size: 2MB)*</label>
<input type="file" name="my_files[]" />
<div class="row">
<div class="col-md-4 form-group">
<button type="submit" name="submit" class="btn btn-success btn-md ">Submit</button>
<div class="col-md-8 form-group">
<div id="processpm"></div>
var formData = new FormData($(this)[0]);

$("#processpm").html('<i class="fa fa-spinner fa-spin fa-3x fa-fw margin-bottom"></i><span class="sr-only">Loading...</span>');

dataType: "json",
data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false,
enctype: 'multipart/form-data',
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(sp) // A function to be called if request succeeds
if(sp.res== 'error'){
$('#processpm').html('<div class="alert alert-danger">'+sp.msg+'</div>');
$('#processpm').html('<div class="alert alert-success">'+sp.msg+'</div>');



Create File send.php and use code give below, don’t forget to change email id of to and from.

function cspamcheck($field) {
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;



$tmp_name = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server
$name = $_FILES['my_file']['name']; // get the name of the file
$size = $_FILES['my_file']['size']; // get size of the file for size validation
$type = $_FILES['my_file']['type']; // get type of the file
$error = $_FILES['my_file']['error']; // get the error (if any)

$msg['msg']="Enter Student Name";
}else if($parent_name==""){
$msg['msg']="Enter Parent / Guardian Name";
}else if($contact_no==""){
$msg['msg']="Enter a Contant no.";
}else if($email==""){
$msg['msg']="Enter a Email ID";
}else if($school_name==""){
$msg['msg']="Enter School Name";
}else if($student_class==""){
$msg['msg']="Select a Class";
}else if($size>=200000){
$msg['msg']="Check File Size";
$mailcheck = cspamcheck($email);
if ($mailcheck==FALSE) {
$msg['msg']="Email ID is not Valid ";
else {
ini_set('display_errors', 1);
$subject="ASK A QUESTION";
$to='[email protected]';
$from='[email protected]';

$attachments = $_FILES['my_files'];
$file_count = count($attachments['name']); //count total files attached
$boundary = md5("");
// $headers = "From: $from";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n";
$headers .= "Reply-To: ".$to."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

$msgi="Name : ".$student_name."\r\n";
$msgi.="Parent / Guardian Name : ".$parent_name."\r\n";
$msgi.="Contact no. : ".$contact_no."\r\n";
$msgi.="School : ".$school_name."\r\n";
$msgi.="Class : ".$student_class."\r\n";
$msgi.="Topic Name : ".$topic_name."\r\n";
$msgi.="Problem : ".$problem."\r\n";

$attachments = $_FILES['my_files'];

$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($msgi));

for ($x = 0; $x < $file_count; $x++){

if($attachments['error'][$x]>0) //exit script and output error if we encounter any
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
print $mymsg[$attachments['error'][$x]];

//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];

//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}if(mail($to, $subject, $body, $headers)){
$msg['msg']="Your request sent, we will soon notify you regarding your query";}else{
$msg['msg']="Something went Wrong";
echo json_encode($msg);
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Do you have any Query / Suggestion