Check Out The Latest Themes And Exclusive Deals And Offers On Themes
In Our Deals For Web Developers Section


Join Today And Get All The 87 Themes In Just $69! Join Today

Send Mail With Attachments Using PHP

Saturday, Jan 21, 2017

Tags:- PHP HTML


There are times when you want to send mail with attachments but that process is very hard and there is no surity of delivering of your mail with attachments but now this process becomes easy and best way to send emails with attachments with the help of PHP and PHP Mailer Library. So, in this tutorial we will show you how to send mail with attachment using php.You may also like Send Email Using SMTP And PHP Mailer.


Send Mail With Attachment Using PHP


To Send Mail With Attachment It Takes Only Two Steps:-

  1. Make a HTML file and define markup to send mail data
  2. Make a PHP file to get data and send mail


Step 1.Make a HTML file and define markup to send mail data

We make a HTML file and save it with a name mail.html

<html>
<body>
<div id="wrapper">
 <form method="post" action="send_mail.php">
  <p>Enter Your Name : <input typ="text" name="sender_name"></p>
  <p>Enter Your Email : <input typ="text" name="sender_email"></p>
  <p>Enter Recipient Email : <input typ="text" name="reciever_email"></p>
  <p>Enter Email Subject : <input typ="text" name="subject"></p>
  <p>Enter Message : <textarea id="text" name="message"></textarea></p>
  <p>Select File : <input type="file" name="attach_file"></p>
  <p><input type="submit" name="send_mail" value="Send"></p>
 </form>
</div>
</body>
</html>

In this step we create a form and add some text field required for sending mail and add file field to uploading attachment for mail and then submit all the data to send_mail.php file which we were going to create in next step.You may also like Send Beautiful Emails Using PHP.



Step 2.Make a PHP file to get data and send mail

We make a PHP file and save it with a name send_mail.php

<?php
if(isset($_POST['send_mail']))
{
 $name=$_POST['sender_name'];
 $sender_email=$_POST['sender_email'];
 $send_to=$_POST['reciever_email'];
 $subject=$_POST['subject'];
 $message=$_POST['message'];
	
 $attachment=$_FILES["attach_file"]["tmp_name"];
 $folder="files/";
 $file_name=$_FILES["attach_file"]["name"];
 move_uploaded_file($_FILES["attach_file"]["tmp_name"], "$folder".$_FILES["attach_file"]["name"]);
	
 require_once('class.phpmailer.php');	
 $send_mail = new PHPMailer();
 $send_mail->From = $sender_email;
 $send_mail->FromName = $name;
 $send_mail->Subject = $subject;
 $send_mail->Body = $message;
 $send_mail->AddAddress($send_to);

 $attach_file = $folder."".$file_name;
 $send_mail->AddAttachment($attach_file);

 return $send_mail->Send();
}
?>

In this step we get all the data required to send mail and then upload the file to directory.We include class.phpmailer.php required to send mail then we add the details in mail and attach the file and send the mail to recievers email address.You may also like Account Verification System Through Email Using PHP.



Thats all, this is how to send mail with attachment using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email