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.

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

Export html table to excel, pdf, csv format using Datatable

Do you know how to export html table to excel, pdf, CSV, or excel format using Datatable? If you don’t […]

August 4, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Instagram Hashtag API PHP and JavaScript Integration with RapidAPI

Do you know how to integrate Instagram Hashtag API with your website? In this tutorial, I will show you how […]

August 2, 2022

Digital Marketing Toolkit

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

Get Free Access Now