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 integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

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

Get Free Access Now