All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Send Mail With Attachments Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Dikshita

In this tutorial we will show you how to send mail with attachment using php, 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.

You may also like Send Email Using SMTP And PHP Mailer.

Send Mail With Attachments 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.

I hope this tutorial on send email with attachment in php using phpmailer helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪