Contact Form Using PHPMailer Gmail SMTP to Send Emails

Last Updated on March 9, 2024 by Roshan Parihar

In this post, learn how to create a contact form using PHPMailer with Gmail SMTP to send emails. When your default mail function is working, you can use SMTP to send your form data via email.

It can possible that your hosting server does not provide email services to send emails. In that case, you will be able to send emails from your contact using the default mail function of PHP.

Contact Form Using PHPMailer Gmail SMTP to Send Emails

Bonus Tip

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

PHPMailer can help you send emails using an SMTP from a trusted platform like Gmail. Yes, you can use Gmail SMTP to send emails when your hosting provider does not provide email services.

Let’s find out how you can use PHPMailer to send emails from your contact form with the step-by-step guide given below.

how to Create Contact Form Using PHPMailer SMTP to Send Emails

Here is the step-by-step process on how to create a contact form using PHPMailer Gmail SMTP:-

Step 1: Create HTML Contact Form Structure

In this step, you have to first create a contact form design for your website that users can fill out and submit. The structure of the form should contain enough form fields to collect users’ data. The form data should be complete enough to help you make communication and answer their queries.

I am going to add the Name, Email, and Message fields to the contact form using HTML. If you want to learn them in detail, you can read my guide simple HTML contact form. After that, you can read the further code given here.

When you execute the above code, you will get a very simple design that is not enough to add to your website contact us page. You will get the design as shown in the image below.

Step 2: Add CSS to Make it Beautiful

If you want to make your contact form beautiful, you will have to just add a small CSS code given below. It can make the Name, Email, and Message fields beautiful. I have added a blue-like color to the button but you can change it as per your requirements.

Add the above CSS code inside the <head> section of your page. You will get the form design as shown in the image below after adding the CSS code.

Now, your design is ready to add to your website. But, the contact form will not work for you without PHP code to collect users’ submitted form data. So, let’s move further to the next section to add PHP code.

Step 3: Send Contact Form Data Via Email Using PHPMailer Gmail SMTP

Here, I am going to show you how to send emails using PHPMailer. Gmail SMTP is the most trusted SMTP to send emails quickly.

To use the PHPMailer, you need to first download the PHPMailer zip file whose link is given below. Extract it to the base location of the file containing the HTML and PHP code.

Download PHPMailer

Now, you just have to copy the below code and paste it just below the </form> tag in the HTML code specified above. You have to change the below things in the PHP code:-

  • 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.

When you have completed the above code, you are now ready to check your contact form on the browser to see if you received emails.

If you want to use the attachment with PHPMailer, you can read our post on Ajax Contact Form with Attachment in PHP (Using Mail() or PHP Mailer)

If you have any queries regarding the above tutorial, please comment below. I will solve your problem and give you solutions.

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.