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.
- How to send mail using PHP PEAR mail with SMTP authentication
- 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
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 = firstname.lastname@example.org (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:
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_port = 587
auth_username = email@example.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
ii) Click on "See all settings"
iii) Click on "Accounts and Import" tab
iv) Click on "Other Google Account settings"
v) Click on "Security" from left side menu
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.
Now it will be turned on.
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.
I have created a folder named "gmail" under xampp/htdocs.
style.css are in this folder.
Let us look at the code below for the form written in
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.
style.css are given below: