, ,

PHP Script to Send Email with Attachment

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…

21
php script to send mail with attachment

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   = "[email protected]";     // SMTP server username
	//$mail->Password   = "password";            // SMTP server password

	$mail->IsSendmail();  // tell the class to use Sendmail
	$mail->AddReplyTo("[email protected]");
	$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.

.

21 responses to “PHP Script to Send Email with Attachment”

  1. Madhulika

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

  2. muni

    hi Madhu,

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

    Regards,

    Muni

  3. I have subscribed to you blog but haven’t received download link.
    Please try sending it ASAP.
    My email id is – [email protected]

    Thank You !

  4. Vinod Verma

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

  5. sam

    hey, am getting this error

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

    any help…?

  6. Venkatesh Badigeru

    not able to download the script.. to mail [email protected]

  7. Subagja

    Could not execute: /usr/sbin/sendmail

  8. ROYAL GIRISH PAPPUCHETTY

    Hi Even I activated I am not getting download link can you please send code to [email protected] plz

  9. Josh Franklin

    Hi, I have subscribed but not able to download the script..
    ([email protected])

  10. 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 [email protected]

  11. 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 [email protected]

  12. Abdul Jalil Mridah

    I think you are great clever.

  13. hey, am getting this error

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

    any help…?

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

  15. 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 [email protected]

  16. krishna

    plz sir give me script urgently

  17. 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 [email protected]

  18. 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

  19. Sandeep Baradwaj

    i am getting this error.

    Could not execute: /var/qmail/bin/sendmail

  20. john moracal

    I’m unable to download your script after subscribing your blog. Please try to send it in a single file into my email [email protected].. Waiting for you mail..

Leave a Reply