PHP Contact Form with Attachment (and Send Emails)

Last Updated on February 9, 2023 by Roshan Parihar

In this tutorial, learn how to create a PHP contact form with attachment and send data via email. It can help you know the query of your customers visually.

If you are running a business online, you need a contact form that collects customers’ queries. You can collect attachments like jpg, png, pdf, and doc along with text content from it.

PHP Contact Form with Attachment (and Send Emails)

Bonus Tip

Looking to create contact forms with attachments without coding required? Create beautiful-looking contact forms with JotForm.

Sometimes text messages of customers are not enough to know the exact queries. You need screenshots that point to the exact problem of your customers to help you solve it easily. Attachments help you collect documents with visual data to understand questions in detail.

You should define what type of attachment you want and what is the limit of its size. The attachment can be of type JPG, PNG, PDF, and DOC type and the size can be less than 2MB or more. Limiting size helps you reduce the extra load on your server.

Let’s find out how you can collect attachments with your contact form using PHP with a valid file type and size.

How to Create PHP Contact Form with Attachment (Step-by-step)

Here is the step-by-step process on how to create PHP contact form with an attachment:-

Step 1: Create Contact Form Structure

First of all, you have to create a structure for your contact form to collect data from your customers. The data should be complete enough to help you know their queries and make one-to-one communication with them.

You can add fields like Name, Email, and Message to collect text content from customers. To collect attachments from your contact form, you can add <input type="file"> as shown below code.

Also, make all the fields required for users by adding required attribute to all the form fields. This attribute makes all the fields compulsory to fill for users.

The main structure of the contact form is shown in the image below. It contains a label with each form field and a submit button to submit the form.

Structure contact form with attachment in html

The above contact form is a simple structure and will not looks better on your website. To make the design better, you must consider adding some CSS given in the next step.

Step 2: Add CSS to Make a Better Design

If you want to change the overall look and appearance of your contact form, you should consider adding some CSS code. The CSS should make all the form fields look better. It should also make the file field looks good and attractive to users.

Below is the complete CSS code that you have to add in the <head> section of your above HTML contact form design. Also, make sure the CSS code button color should match your brand. You can change the below code as per your requirements.

After you add some CSS, your contact form appearance will change as shown in the image below. It looks better and beautiful enough to add to your website. The upload button looks different and better to attract your customers.

Add CSS to contact form with attachment

The contact form also contains a background color to display the area it covers on a page on your website.

Step 3: Contact Form Email and File Validation Using PHP

You have to first collect all form data using PHP global variable $_POST. However, to collect attachments, you have to use PHP global variable $_FILES. After collecting the data, you can start validating them.

To validate the email address, you can match the user’s email address with the regular expression format. To validate the file attachment, you have to validate its file type and size.

The file type can be JPG, PNG, PDF, and Doc type for attachment. Its size can be 2MB or more and should be less to reduce the extra load on your server.

See the code below to validate your form data. It executes only when the users submit the form.

After the validation, you proceed further to send contact form data via email with an attachment.

Step 4: Send Contact Form Data with Attachment Via Email Using PHP (PHPMailer Gmail SMTP)

To send emails in PHP with attachments, the PHPMailer is the easiest method to use. It requires less coding and is easy to send attachments. I going to use Gmail SMTP which is free and easy to set up quickly.

Gmail SMTP is the most trusted mailer SMTP to send emails in PHP when you don’t have email services on your server. So, let’s set up it with the code given below.

Requirements to Setup PHPMailer Gmail SMTP

Firstly, download the PHPMailer by clicking on the button below and save it to the base location of your main files. Extract the folder to get all the PHPMailer required docs to send emails.

Download PHPMailer

You will have to change the below things in the code given below to make it work:-

  • Change ‘[email protected]’ with your Gmail id
  • Change ‘yourgmailpassword’ with the app password of your Gmail id. You can read the guide Sign in with app password in Gmail to create your app password on Gmail.
  • Change ‘[email protected]’ with the recipient’s email address. Also, change the ‘recipientname’ with your name to appear on the emails.

PHP Code to Send Emails Using PHPMailer Gmail SMTP

After you complete the above requirements, copy the below code and paste it after the validation code of PHP given in the previous step. The below code is self-explanatory and easy to understand with comments as it is well-commented.

After you have done with the above code and followed the requirements, you are all set!

Now, open the form on your favorite browser (mine is Google Chrome) and fill out the form and press the submit button to test it. You will see the emails received in your email inbox.

If you want to send emails without a page refresh, you can read our tutorial on Ajax Contact Form with Attachment in PHP (Using Mail() or PHP Mailer).

If you have any problem regarding sending emails or need any help, please comment below. I will be happy to help you.

You May Also Like to Read

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.