Send Mail With Attachments Using PHP

Saturday, Jan 21, 2017


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

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

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

 move_uploaded_file($_FILES["attach_file"]["tmp_name"], "$folder".$_FILES["attach_file"]["name"]);
 $send_mail = new PHPMailer();
 $send_mail->From = $sender_email;
 $send_mail->FromName = $name;
 $send_mail->Subject = $subject;
 $send_mail->Body = $message;

 $attach_file = $folder."".$file_name;

 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