PHP Contact Form with Multiple Attachments

Last Updated on February 18, 2023 by Roshan Parihar

PHP contact form with multiple attachments helps you collect more screenshots from users. You can easily understand users’ queries visually.

If you are running a business online, contact would be the better option to get connected with your audience. Sometimes, a single attachment in your contact form is not enough to understand audience queries.

PHP Contact Form with Multiple Attachments

Bonus Tip

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

You can allow users to attach more files to collect more screenshots and documents from users. Collecting more information helps you easily analyze the requirements of your users.

After collecting more information from your users, you can contact them to stay connected with your business. This can help you increase the revenue of your business.

In this post, you will learn how to create a contact form with multiple attachments using PHP. So, let’s get started.

How to Create PHP Contact Form with Multiple Attachments (Step-by-step Process)

Here is the step-by-step process on how to create a PHP contact form with multiple attachments:-

Step 1: Create an HTML Structure Contact Form with Multiple Attachments

First of all, create an HTML structure of the contact form with multiple attachments. The contact form should contain fields that are complete enough to help you easily make communication after collecting emails.

The fields like Name, Email, and Message are complete fields to get the message and make personal communication. So, let’s add these fields to the contact form with an attachment field.

The main HTML structure of a webpage is as given below. You have to use it and place the form HTML code in the indicated place below. You will get the HTML form and CSS code below.

HTML Structure of a Webpage

Contact Form HTML Code

To make the contact form with multiple attachments structure, you have to add the Name field using <input type="text">, Email field using <input type="email">, message field using <textarea">, and attachment field using <input type="file">.

Also, make sure that the attachment field should contain multiple attributes to allow multiple file uploads. It also requires adding name=inputname[] attribute of the attachment field with [] as you can see below the HTML code.

CSS Code to Make Contact Form Design Looks Better

The HTML structure requires some better design for look-n-feel. For this, you have to add some CSS as given below. The below CSS style forms field elements, attachments, and buttons. You can change the button and attachment color to match them with your brand color.

After you add the above CSS, your contact form will convert into a better design as shown in the image below.

design

Now, you have created the design of the contact form with multiple attachments. Let’s add PHP code to it to validate the email address and the multiple attachment files.

Step 2: Validate Email Addresses and Attachments Using PHP

To validate the contact form, you can use the PHP code given below. It first collects the contact form data using PHP global variable $_POST. The name and message fields are not required to validate. However, it is required to validate email addresses and attachments.

The below PHP code is well commented on enough to easily understand each line of code for validation. To validate the email address, you have to use the preg_match() with regular expression.

The attachment type and size are the main part to validate using PHP. We should validate that its size should be less than 2MB and type should be PDF, DOC, JPG, JPEG, and PNG. 2MB size helps you reduce the extra load of uploading larger files to your hosting server.

The multiple attachment fields require foreach loop to validate each attachment one by one. See the code below to validate the user’s entered information.

As you have validated your contact form and multiple attachments. You can now send the contact form data in the next step.

Step 4: Send Contact Form with Multiple Attachments Via Emails Using PHP

To send the contact form data with multiple attachments via emails, PHPMailer is the more convenient and easiest way.

You have to first download the PHP Mailer library and include it in the base location of your contact form. Click the below button and save the PHPMailer library in the base location of your main files. After that, extract the folder to get the required PHPMailer files and folders.

Download PHPMailer

Now, copy the below code and paste it after the above PHP validation code. Also, don’t forget to change the below things in the code:-

  • Change ‘[email protected]’ with your Gmail id
  • Change ‘yourgmailpassword’ with the Gmail app password. If you don’t have a Gmail app password, you can create one using the guide Sign in with app password in Gmail.
  • Change ‘[email protected]’ with the recipient’s email address. Also, change the ‘recipientname’ with your name to appear on the emails.

After you complete the above step-by-step process, your contact form with multiple attachments is ready to use. Now, open your contact form in your favorite browser and enter your information. After that, submit the form to test its working.

If you have any problem with the contact form with multiple attachments, please comment below. I will be very 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.