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

Yes, you can send emails with attachments in PHP

How many emails i can send a day

Depends on your hosting provider. As PHP mail() worked based on your hosting.

What is the attachment size Limit

Normally its 25MB. and its depend on hosting provider

can i send emails from my localhost ?

By using this PHP mail() function. You cant send emails from your desktop or local server. But you can if you config a mail server in your localhost.

Ajith Jojo Joseph

Hire Me for Freelancing $30/Hour ,
Contact via email : [email protected]

Share with your friends:

Comments are closed.

Drag and Drop with Swapy and PHP

Hello , You might had issues with drag and drop options in your dashboard. here i found a new javascript […]

September 17, 2024

How To Generate A PDF from HTML in Laravel 11

Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]

June 22, 2024

How to get the next value of an array and loop it in the array in PHP

Do you know How to get the next value of an array and loop it in the array in PHP […]

July 28, 2023

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

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now