How to Create a PHP Contact Form with Send to Email

Perhaps you are one of those who manage a website or have requested a quote through a contact form; Most of the Web pages make use of these forms and create databases necessary for their sites; Now, How to create PHP contact form with email sending step by step? Here we will explain it to you in a simple way.

Step 1: Encode the markup

To create a PHP contact form with sending to email you must first download and install the necessary software , then the first thing we must take into account is what elements you want your form to have; an input field is required for the name of the user who is contacting you; as well as a field for the email address where the timely response can be sent.

Also an input field in order for the user to contact you; as well as a text-area that allows them to write their message. With all this in mind, you proceed to encode the markup . The HTML code is written to add all the aforementioned fields, you could even use the notepad to make your HTML code .

<form action = »contact.php» method = »post»>

<div class = »elem-group»>

<label for = »name»> Your Name </label>

<input type = »text» id = »name »Name =» visitor_name »placeholder =» John Doe »pattern = [AZ \ sa-z] {3.20} required>

</div>

<div class =» elem-group »>

<label for =» email »> Your E-mail </label>

<input type = »email» id = »email» name = »visitor_email» placeholder = » [email protected] » required>

</div>

<div class = »elem-group »>

<Label for =» department-selection »> Choose Concerned Department </label>

<select id =» department-selection »name =» concerned_department »required>

<option value =» »> Select a Department </ option>

<option value = »billing»> Billing </option>

<option value = »marketing»> Marketing </option>

<option value = »technical support»> Technical Support </option>

</select>

</div>

<div class = »elem-group»>

<label for = »Title»> Reason For Contacting Us </label>

<input type = »text» id = »title» name = »email_title» required placeholder = »Unable to Reset my Password» pattern = [A-Za-z0-9 \ s] {8,60}>

</div>

<div class = »elem-group»>

<label for = »message»> Write your message </label>

<textarea id = »message» name = »visitor_message» placeholder = »Say whatever you want.» required> </textarea>

</div>

<button type = »submit»> Send Message </button> </form>

Let’s highlight some important details of this code; the action character specifies where the form data should be sent. The name character allows access to the values ​​of the elements on the server side, that is, it allows you to know the name of the user who is contacting you using $ _Post [visitor- name] in PHP contacts. We use the Placeholer character so that users have an idea of ​​the input for each field in the form.

The requier character optimizes that no important fields are left blank before you hit the submit button. The paltern character is used to consign rules of the type of values ​​that can go within certain fields.

In this case we will only allow the user to use letters and the space character in the names they send; They even limit the number of accessible characters from 3 to 20.

Step 2: Make the form functional using PHP

The second step is to make the PHP email send contact form functional using PHP; start by creating a PHP file that you will run later, contact.php and write the following code:

In this way, some validation has been done on the user’s side in the user’s input. It’s also important to do server-side validation. To do this, the fillter_var () function is used to purify the name provided by the user. In the same way we purify the value of $ email_title and $ concern_department.

Security is important when handling user or input data. Validation allows you to verify that the user’s name does not have a number.

For example, if a malicious user tries to contact you using the form, you might add script tags in the text-area to download a malicious script ; this could be dangerous, especially on public forum websites.

As you can see, to create a PHP contact form with sending to email requires two fundamental steps. We must first take into account what we want the form to have and then proceed to encode the markup.

Second, make our form workable with PHP ; without neglecting its validation and security. We hope this article has been useful to you.

 

Leave a Comment