Please support us by disabling ad blocker for smarttutorials.net ...

PHP Script to Send Email with Attachment

Posted by & filed under CSS, HTML5, PHP.

I had searched in Google for long time for the script to send an email with file attachment (all types of file pdf files,word files and images etc……). It was hard thing to find out exact script. Here I had source code as well steps to implement email form attachment.

More importantly the mail will reaches inbox, mainly avoids as spam message.

This article shows you how to create a PHP based email form that supports file attachment.

The whole process consists of two steps

1. create an html form with attachment.
2. Then create php file that process data from the html form.

php script to send mail with attachment

Demo

 

You can also refer my another cool tutorial on Send an E-Mail with Attachment Using jQuery, Ajax and PHP without Refrshing a Page.

The HTML form with file upload box :

The code for an HTML form with a file upload box is given below. User can click on the ‘Browse’ button to select the file from his/her local machine.

<div class="title">
<h1>Mail Demo Page</h1>
</div>
<div class="mail">
<form action='mail.php' method='post' id='mailForm' enctype='multipart/form-data'>
<table>
<tr>
<td class="label"> Name : </td>
<td><input type="text" id="name" name="name" class="form-input" placeholder='User Name'/>
<div id="invalid-name" class="error_msg"></div>
</td>
</tr>
<tr>
<td class="label"> E-mail : </td>
<td><input type="email" id="email" name="email" class="form-input" placeholder='E-Mail'/>
<div id="invalid-email" class="error_msg"></div>
</td>
</tr>
<tr>
<td class="label"> From E-mail : </td>
<td><input type="email" id="femail" name="femail" class="form-input" placeholder='From E-Mail'/>
<div id="invalid-femail" class="error_msg"></div>
</td>
</tr>

<tr>
<td class="label"> Phone : </td>
<td><input type="tel" id="phone" name="phone" class="form-input" placeholder='Phone'/>
<div id="invalid-phone" class="error_msg"></div>
</td>
</tr>
<tr>
<td class="label"> Image : </td>
<td><input type="file" id="image" name="image" class="form-input" placeholder='Image' accept="image/*"> <div id="invalid-image" class="error_msg"></div></td>

</tr>

<tr>
<td class="label"> Message : </td>
<td>
<textarea cols="27" rows="5" id="message" name="message" placeholder='Message'></textarea>

<div id="invalid-message" class="error_msg"></div></td>

</tr>

<tr>
<td colspan="2"> <input type="submit" value="Send Mail!" id='submit_btn' name="submit_btn" class="submit_btn"/></td>
</tr>
</table>
</form>
</div>

Now add css given in the download file to add styling to the form.

The form will look like this:

php script to send mail with attachment

HTML form for Sending Email with File Attachment in PHP.

Please note that we have added:

“enctype=”multipart/form-data”
while defining the <form> tag. This is to tell the browser that this form will be used to upload files.
php script to send mail with attachment

Download

Now we almost finished form designing part.

Now just follow php part coding. I am using php mail plugin to send an email with attachment.

Step 1:

Create mail.php file and add the following php file (class.phpmailer.php).

require 'class.phpmailer.php';

Step 2:

Now add to email ID.

$to = "Your email ID";
$mail->AddAddress($to);

Step 3:

Now get from email id and name of the user.

$mail->From = $_POST['femail'];
$mail->FromName = $_POST['name'];

Step 4:

Now add subject of the mail.

$mail->Subject  = "Test Email using PHP";

Step 5:

Now get message from the html form.

 

$body             = "<table>
	                         <tr>
							    <th colspan='2'>This Sample Mail</th>
							 </tr>

							 <tr>
							    <td style='font-weight:bold'>Name :</td>
								<td>".$_POST['name']."</td>
							 </tr>

							 <tr>
							  <td style='font-weight:bold'>E-mail : </td>
							  <td>".$_POST['email']."</td>
							</tr>

							<tr>
							  <td style='font-weight:bold'>Phone : </td>
							  <td>".$_POST['phone']."</td>
							</tr>

							<tr>
							  <td style='font-weight:bold'>Message : </td>
							  <td>".$_POST['message']."</td>
							</tr>
	                     <table>";
	$body             = preg_replace('/\\\\/','', $body); //Strip backslashes
	$mail->MsgHTML($body);

Step 6:

Finally get the attachment and send it.

$mail->AddAttachment($_FILES['image']['tmp_name'],
                         $_FILES['image']['name']);
	$mail->IsHTML(true); // send as HTML
	$mail->Send();
	echo 'Message has been sent.';

Here is the full php source code.

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
    $mail = new PHPMailer(true); //New instance, with exceptions enabled

    $to = $_POST['email'];
	$mail->AddAddress($to);
	$mail->From       = $_POST['femail'];
    $mail->FromName   = $_POST['name'];
	$mail->Subject  = "Test Email using PHP";

	$body             = "<table>
	                         <tr>
							    <th colspan='2'>This Sample Mail</th>
							 </tr>

							 <tr>
							    <td style='font-weight:bold'>Name :</td>
								<td>".$_POST['name']."</td>
							 </tr>

							 <tr>
							  <td style='font-weight:bold'>E-mail : </td>
							  <td>".$_POST['email']."</td>
							</tr>

							<tr>
							  <td style='font-weight:bold'>Phone : </td>
							  <td>".$_POST['phone']."</td>
							</tr>

							<tr>
							  <td style='font-weight:bold'>Message : </td>
							  <td>".$_POST['message']."</td>
							</tr>
	                     <table>";
	$body             = preg_replace('/\\\\/','', $body); //Strip backslashes
	$mail->MsgHTML($body);

	$mail->IsSMTP();                           // tell the class to use SMTP
	$mail->SMTPAuth   = true;                  // enable SMTP authentication
	$mail->Port       = 25;                    // set the SMTP server port
	//$mail->Host       = "mail.yourdomain.com"; // SMTP server
	//$mail->Username   = "name@domain.com";     // SMTP server username
	//$mail->Password   = "password";            // SMTP server password

	$mail->IsSendmail();  // tell the class to use Sendmail
	$mail->AddReplyTo("admin@smarttutorials.net");
	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
	$mail->WordWrap   = 80; // set word wrap

	$mail->AddAttachment($_FILES['image']['tmp_name'],
                         $_FILES['image']['name']);
	$mail->IsHTML(true); // send as HTML
	$mail->Send();
	echo 'Message has been sent.';
} catch (phpmailerException $e) {
	echo $e->errorMessage();
}
?>

Hope this code works well for all for sending all type of files (word files,PDF files and images etc………..)

php script to send mail with attachment

Example Php Email with File attachment and Images

Please refer this cool tutorial on Send an E-Mail with Attachment Using jQuery, Ajax and PHP without Refrshing a Page.

.

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni[at]smarttutorials.net

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

  • Madhulika

    I am unable to download the script. I have subscribed to your blog but still I am unable to download it.

  • muni

    hi Madhu,

    Thanks for subscribing, I think u got the download link now.

    Regards,

    Muni

  • I have subscribed to you blog but haven’t received download link.
    Please try sending it ASAP.
    My email id is – maitreyapatni@gmail.com

    Thank You !

  • Vinod Verma

    Hi I am geting “Could not execute: /usr/sbin/sendmail” error please help me

  • sam

    hey, am getting this error

    Could not execute: /usr/sbin/sendmail…..

    any help…?

  • Venkatesh Badigeru

    not able to download the script.. to mail venkatesh@multiplersolutions.in

  • Subagja

    Could not execute: /usr/sbin/sendmail

  • ROYAL GIRISH PAPPUCHETTY

    Hi Even I activated I am not getting download link can you please send code to p.girish888@gmail.com plz

  • Josh Franklin

    Hi, I have subscribed but not able to download the script..
    (frankjosh99@gmail.com)

  • George

    I am unable to download the script. I have subscribed to your blog but still I am unable to download it. you can send it to my email gestech11@gmail.com

  • George

    I am unable to download the script. I have subscribed to your blog but still I am unable to download it. please send it to my email gestech11@gmail.com

  • sent..

  • Abdul Jalil Mridah

    I think you are great clever.

  • hey, am getting this error

    Could not execute: /usr/sbin/sendmail…..

    any help…?

  • check normal php mail() functionality working in your server. Also check php mailing functionality enabled in your server …

  • krishna

    Hello sir I need this whole script urgently I am using Xampp…can u plz guide me….I am unable to download the script. I have subscribed to your blog but
    still I am unable to download it. please send it to my email kendregopalkrushna@gmail.com

  • krishna

    plz sir give me script urgently

  • Krishna Reddy Ch

    hi Please send me Code I am unable to download the script. I have subscribed to your blog but
    still I am unable to download it. please send it to my email krushnadial@gmail.com

  • Ramesh

    Warning: escapeshellcmd() has been disabled for security reasons in /home/u363719411/public_html/jagan2/class.phpmailer.php on line 894
    Could not execute: /usr/sbin/sendmail

Get Instant Script Download Access!