Send Mail with pdf or doc Attachment in php

Send Mail with pdf or doc Attachment in php

Send Mail with pdf or doc Attachment in php

To send an email with mixed content requires to set Content-type header to multipart/mixed. Then text and attachment sections can be specified within boundaries.

Create a HTML form with file input field similar to below, enctype attribute should be “multipart/form-data“, so that form-data is encoded as “multipart/form-data” when sent to server.


Send Mail with pdf or doc Attachment in php


<h2>Send Mail With Attachment</h2>
<form enctype=”multipart/form-data” method=”POST” action=””>
<td>Name </td><td><input type=”text” name=”name” placeholder=”NAME” /></td>
<td>Email </td><td><input type=”email” name=”email” placeholder=”EMAIL”/></td>
<td>Subject </td><td><input type=”text” name=”subject” placeholder=”SUBJECT”/></td>
<td>Message </td><td><textarea name=”message” rows=”4″ placeholder=”YOUR MESSAGE”></textarea></td>
<td>Attachment </td><td><input type=”file” name=”myFile” /></td>
<td><input type=”submit” name=”button” value=”SEND MAIL” /></td>

PHP Mail with pdf or doc Attachment

Here’s the total PHP code that sends PHP email with connection. Simply make a PHP document with code beneath and point your HTML frame to the record, and bear in mind to change beneficiary address and email address in the code.

// echo “<pre>”;print_r($_REQUEST);echo “</pre>”; //comment here
// echo “<pre>”;print_r($_FILES);echo “</pre>”; //comment here
if($_POST && isset($_FILES[‘myFile’]))

$from_email = ‘’; //from mail, it is mandatory with some hosts
$recipient_email = ‘’; //recipient email (most cases it is your personal email)

//Capture POST data from HTML form and Sanitize them (Remove HTML tags from String)
$name = filter_var($_POST[“name”], FILTER_SANITIZE_STRING); //sender name
$reply_to_email = filter_var($_POST[“email”], FILTER_SANITIZE_STRING); //sender email used in “reply-to” header
$subject = filter_var($_POST[“subject”], FILTER_SANITIZE_STRING); //get subject from HTML form
$message = “Name : “.$name.”\nMessage : “.$_POST[“message”]; //message

//Get the uploaded file data
$file_tmp_name = $_FILES[‘myFile’][‘tmp_name’];
$file_name = $_FILES[‘myFile’][‘name’];
$file_size = $_FILES[‘myFile’][‘size’];
$file_type = $_FILES[‘myFile’][‘type’];
$file_error = $_FILES[‘myFile’][‘error’];

if($file_error > 0)
die(‘Upload error or No files uploaded’);
//read from the uploaded file like pdf,doc,txt & base64_encode content for the mail
$handle = fopen($file_tmp_name, “r”);
$content = fread($handle, $file_size);
$encoded_content = chunk_split(base64_encode($content));

$boundary = md5(“sanwebe”);
$headers = “MIME-Version: 1.0\r\n”;
$headers .= “From:”.$from_email.”\r\n”;
$headers .= “Reply-To: “.$reply_to_email.”” . “\r\n”;
$headers .= “Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n”;

//body plain text
$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($message));

//attachment file details
$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;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or error messages
die(‘Thank you for your email’);
die(‘Could not send mail!’);

Leave a Comment