PHP Code For Contact Form to Send Email (Using mail())

Last Updated on January 30, 2023 by Roshan Parihar

In this post, learn the PHP code for a contact form to send an email with validation. If you want to communicate with your audience, you have to collect your audience queries from contact forms via emails.

Creating a beautiful contact form using HTML and CSS is easier to design. However, sending emails containing data users fill on contact forms requires some PHP knowledge and PHP coding.

PHP Code For Contact Form to Send Email

Bonus Tip

Looking to create contact forms and send emails without coding? Create beautiful-looking contact forms with JotForm.

If you are a beginner and just started learning PHP, you need to know the right way of writing PHP code to send emails from your contact form. You can easily capture audience queries and send emails with the PHP code given below.

So, let’s get started.

PHP Code For Contact Form to Send Email

You have to first design a contact form for your website using HTML and CSS. After that, store the form data in a variable and use the mail() of PHP to send emails.

Let’s find out with the examples given below containing the PHP code for a contact form to send an email.

Contact Form Design Using HTML Code

Firstly, you have to create a contact form design for your website to allow your audience to fill out and submit. If you are a website owner, you need to create a contact form that can help you collect queries and make communication.

To make communication, you can add an input field to collect the email address of your audience. To get queries from your audience, add a text area message box where the audience can write their queries. For personal communication, a name field in the contact form is also useful for you.

So, below are the form fields we are going to add to the contact form:-

  • Name input field
  • Email input field
  • Message box text area field

To create a simple contact form design, you can use a few HTML codes as given below:-

To make your contact form beautiful, you can add CSS code for styling as given below:-

Now, after adding the above HTML code and CSS for styling, the final appearance of your contact is as shown in the image below.

contact form in html css

Let’s add some PHP code for a contact form to send an email.

Send Email with Validation Using PHP Code

If you want to send emails from your contact form, you have to first get the form data using PHP global variable $_POST. Use the name of the input field inside the $_POST[]> to get form data.

As all the form fields in the HTML code above contain required attribute. That’s why it’s not required to add validation to make all the fields required to fill. However, you need to add validation for an email address to collect a valid email address from your audience.

Tips: Use isset() function to execute the PHP code only when your audience clicks the submit button.

After that, you can use the mail() as given in the PHP code below to send emails.

Description of PHP Codes to Send Emails

Let’s understand each variable to send emails using PHP code:-

  • $to: The recipient email address is the email address of the website owner who wants to get emails to their email inbox.
  • $subject: It is the subject of the email the recipient will receive.
  • $message: The main content body of the email you send to the recipient. You can add the data in this variable to get from the contact form.
  • $headers:
    • ‘From’ email is the email address of the website owner that should be domain specific.
    • ‘CC’ is the email address of the second person who wants to receive the same email.
    • “Reply-To’ is the email address of the audience who fills out the contact form.
  • mail(): It is the main function in PHP to send emails. You have to pass the above 4 variables as an argument of the function to make it work in PHP.

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.