Today, We are going to learn steps to send an email with attachment using PHP and Ajax without refreshing the page. Below is the step by step explanation.

Before we are getting started, we need a little bit of knowladge about HTML, CSS, PHP and jQuery.

  1. HTML form that will collect the Information from the user
  2. Ajax will gather all the data submitted by the form and send to process.php to send an email
  3. process.php will check the whether attachment is there or not and trigger a email.


Filename : index.html

This is the page where we have a simple form that collect all the information from the user to send email via PHP mail.

Above form have fields like First Name, Email Address and File attachment. Each input filed has their unique ID to fetch the data once form has submitted.

Note : We have set  enctype=”multipart/form-data” . This is used for file attachment.

Script that gather all information submitted by the Form and pass the data to process.php to send email.




Demo | Download

7 thoughts on “Simple Ajax Form With Email Attachment Using PHP

  1. I do not even know how I ended up here, but I believed this submit used to be great. I don’t recognize who you might be but certainly you’re going to a well-known blogger should you are not already Cheers!

    1. Place below code above Line Number 80 . This will help you to upload only with extensions of PDF, DOC, DOCX files.

      $allowedExts = array( "pdf", "doc", "docx" );
      $extension = end(explode(".", $_FILES['file_attach']['name'] ));

      if ( ! ( in_array($extension, $allowedExts ) ) ) {
      $output = json_encode(array('type'=>'error', 'text' => 'Only PDF, DOC and DOCX extensions are allowed'));

Leave a Reply

Your email address will not be published. Required fields are marked *