How to send mail from localhost using Gmail SMTP server in PHP

In this topic we will discuss about how you can send mail from your Gmail account using PHP in localhost. If you have a Gmail account you can send mail from your Gmail account using a simple mail() function in PHP. If you are interested you can also read below two topics for sending mail in PHP.

  1. How to send mail using PHP PEAR mail with SMTP authentication
  2. How to send mail using mail() function in PHP with example

We will create a simple form with three input fields (to_email, subject and message) to send mail from Gmail. Before that we need to change some settings. Let us see below steps and then discuss each step in detail:

Step 1 - Setup your php configuration file (php.ini)

There are few settings in PHP that we have to set here. Let's see them:

i) Update php.ini

Open php.ini file in notepad from xampp/php folder or wamp/php folder in your local system. After opening the file search for "[mail function]". Once you get there, in the next few lines, use below parameter values. Do not change default values, instead comment out existing value by adding ";" at the beginning of the line and add new line for the modified value.

Add below three lines:
SMTP = smtp.gmail.com
smtp_port = 587
sendmail_from = your_gmail_id@gmail.com (Your actual Gmail id should be here)

Next, if your xampp is installed in C: drive use below path for sendmail_path parameter. Otherwise use the drive where xampp is installed in your system.
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

For my case xampp is installed in D: drive, so sendmail_path should be as below:
sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"

Below is the section of php.ini; before and after I changed the parameter values:

Before change

After change, changes are highlighted

ii) Update sendmail.ini

Now go to xampp\sendmail folder or wamp\sendmail folder and open sendmail.ini file in notepad. Use below parameter values. Comment existing values using ";" at the beginning of the line. Same way, update sendmail.ini as we did for php.ini. Use below parameters and their values to update:

smtp_server=smtp.gmail.com
smtp_port = 587
auth_username = your_gmail_id@gmail.com (Your actual Gmail id should be here)
auth_password = password

Once all changes are done, save them and restart xampp or wamp.

Step 2 - Change your Google account settings to allow less secure apps

Google does not recommend to turn on "Allow less secure apps" because of security, if you turn it on, make sure to turn it off after testing. Otherwise, Google automatically turns it off anyway if it is not being used. Follow below steps to turn on "Allow less secure apps" if it is not turned on already:

i) Login to your Gmail and click on the settings icon

send email in Gmail using PHP

ii) Click on "See all settings"

send an email using PHP

iii) Click on "Accounts and Import" tab

Gmail account setting for less secure app

iv) Click on "Other Google Account settings"

send mail from localhost using Gmail SMTP in PHP

v) Click on "Security" from left side menu

Gmail account setting sending mail from gmail in php

vi) Scroll down until you see "Less secure app access" and click on "Turn on access(not recommended)" link. Then click to turn allow less secure apps. Again, Google does not recommend to turn on "Allow less secure apps" because of security, if you turn it on make sure to turned it off after testing.

Send Email Using Gmail SMTP Server From PHP

Now it will be turned on.

Send Emails from gmail with PHP Mail() Function

Google Account setting is completed now.

Step 3 - Create a form to send mail

We will create a simple form with three input fields and a submit button. We will add a "To" field for for email_id, a subject field and a message textarea field to type the message. You have to enter the email_id of the recipient to whom you want to send mail with a subject and the message.

send mail using smtp in php example

I have created a folder named "gmail" under xampp/htdocs. index.php and style.css are in this folder.

Send mail using gmail in php

Let us look at the code below for the form written in index.php

We have just added three input fields - email Id, subject and message. After that we added a submit button to send mail.

Send Mail PHP code

Once the form is submitted, below PHP code is written to send mail:

You can see it takes three input values and validates them. One additional check is there for email id, it uses filter_var() function to validate the email id. If everything looks okay, it formats the mail body in html. It uses mail() function to send the mail.

mail() function uses 4 parameters.
To - To email id which user enters in the form
Subject - Subject of the email, user types in the subject in the form
Mail Body - body of the mail, user types in the message in the form
Header - header of the message which we have formed it in the code as $headers. Usually it contains the document type, form email id, cc and bcc.

index.php and style.css are given below:

index.php

style.css

Test the Application

To test it, just open localhost/gmail in your browser, enter to_email id (a valid email id), give a subject and type in the message in the form. Then hit "Send Mail" button. If you did not get mail just check the PHP settings as described above, make sure Gmail userid/password are correct and do not forget to restart your xampp/wamp if you change any settings in php.ini or sendmail.ini.

Send mail using gmail in phpAlert: Once your testing is done and if you are not using this application, you should restore previous settings in php.ini and sendmail.ini. Also, make sure you turn off less secure app access in your Gmail account.

send mail using smtp in php example download source codeDownload Source Code

I have put all codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the code or you can modify them as per your requirements.

send mail using smtp in php example Conclusion

You can write a routine for sending mail and call it from anywhere in the application with the parameters. In this topic, it was an example to show how you can send mail from localhost using Gmail. Refer to the related topics below for other ways to send mail using PHP.