HTML Form to Email with Attachment using PHP
Do you know How the HTML to email with attachments works? I will explain that in this post
Lets start with our Goal
Our goal is to create an HTML form with a file field, when someone clicks the submit button we want to receive the data with attachment in our email.
Before we started to this everyone for the big support my HTML form to PHP mail post. As per my subscriber’s request, I made this new tutorial. to send any attachment via HTML form to the email we can use phpmail function. here is am giving an example of how to do it.
Create form for html to email with attachmentsÂ
First create an HTML form with enctype=”multipart/form-data” because we are transferring files .
<form action="mail.php" method="post" enctype="multipart/form-data"> <input type="text" name="name" placeholder="Name"> <br> <input type="text" name="email" placeholder="email"> <br> <input type="text" name="message" placeholder="Message"> <br> <input type="file" name="file"> <br> <input type="submit" value="Submit"> </form>
this is just a HTML boilerplate. you can add your class and modify with your website design.
Now we have to create our PHP file which will process the data when the form submit.
Here we are getting the data from the form and making a message string and encoding the uploaded attachment into base64 format and send it to your email address,
When the email hit your inbox it will decode the base64 object and display the attachment
mail.php file
<?php $filenameee = $_FILES['file']['name']; $fileName = $_FILES['file']['tmp_name']; $name = $_POST['name']; $email = $_POST['email']; $usermessage = $_POST['message']; $message ="Name = ". $name . "\r\n Email = " . $email . "\r\n Message =" . $usermessage; $subject ="My email subject"; $fromname ="My Website Name"; $fromemail = '[email protected]'; //if u dont have an email create one on your cpanel $mailto = '[email protected]'; //the email which u want to recv this email $content = file_get_contents($fileName); $content = chunk_split(base64_encode($content)); // a random hash will be necessary to send mixed content $separator = md5(time()); // carriage return type (RFC) $eol = "\r\n"; // main header (multipart mandatory) $headers = "From: ".$fromname." <".$fromemail.">" . $eol; $headers .= "MIME-Version: 1.0" . $eol; $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol; $headers .= "Content-Transfer-Encoding: 7bit" . $eol; $headers .= "This is a MIME encoded message." . $eol; // message $body = "--" . $separator . $eol; $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol; $body .= "Content-Transfer-Encoding: 8bit" . $eol; $body .= $message . $eol; // attachment $body .= "--" . $separator . $eol; $body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol; $body .= "Content-Transfer-Encoding: base64" . $eol; $body .= "Content-Disposition: attachment" . $eol; $body .= $content . $eol; $body .= "--" . $separator . "--"; //SEND Mail if (mail($mailto, $subject, $body, $headers)) { echo "mail send ... OK"; // do what you want after sending the email } else { echo "mail send ... ERROR!"; print_r( error_get_last() ); }
Important Notes :
- This will not work in your desktop or Localhost
- You have to host it in a server to make it work.
- Some free hosting providers not allow you to send emails from your website (eg : 000webhost)
- Sometimes you have to add a valid from email address to work with this
- Maximum file size is 25 , if you are attaching bigger files, it may not work. anyway its depend on your server provider.
Check out the video for more details and to know how to implement html to email with attachments
Download Source Code :Â
If you are still having doubts about how to work with HTML to email with attachments forms you can check out my Youtube video explaining everything in detail.
Can i send emails with attachments in PHP
How many emails i can send a day
What is the attachment size Limit
can i send emails from my localhost ?
Post Your Questions on our forum
Post a question on Forum
Share with your friends:
New Open Source CRM for project Management and Invoicing
I’m excited to announce the launch of my new open source project: Gmax CRM. an invoicing and project management tool […]
December 31, 2022
Post View Counts WordPress plugin (Documentation)
The Post View Counts plugin is a simple tool for tracking and displaying the number of views for each post […]
December 30, 2022
How to create Laravel Flash Messages
Laravel flash messages are a convenient way to display one-time notifications to the user after a form submission or other […]
December 22, 2022
How to make Custom Artisan Command in Laravel
Custom Artisan commands are a useful feature of Laravel that allow you to define your own command-line commands for tasks […]
December 22, 2022
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now