Send Email via Gmail SMTP Server in PHP

Hi Friends,

We’re going to learn that how we can send an email using Gmail SMTP server from a PHP page/script.
SMTP is an abbreviation for Simple Mail Transfer Protocol.
SMTP is an internet standard for electronic mail, i.e., email.
All mail servers and other mail transfer agents use SMTP to send and receive emails.

We will use mail() function in PHP.

Steps to be followed:

Step 1: Change in Gmail settings

First of all you need to make some changes in your gmail account settings.
You need to open your gmail account settings. The following page will get open.

You need to click Signing in to Google. It will redirect you to the following page.

Gmail SMTP enable settings

There are 2 steps :

1. You need to keep the 2-step verification off, if you’re using it.
2. Then click on Connected apps & sites. It will redirect you to the following window.

Gmail SMTP enable settings

Here, you need to keep Allow less secure apps : ON .
And it’s done. That’s all you need to do.

Step 2:  

Create a file called sendmailform.php using follwing code

<table width=”400″ border=”0″ align=”center” cellpadding=”3″ cellspacing=”1″>
<td><strong>Send mail Form</strong></td>
<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″>
<td><form name=”form1″ method=”post” action=”mailto.php”>
<table width=”100%” border=”0″ cellspacing=”1″ cellpadding=”3″>
<td width=”16%”>Subject</td>
<td width=”2%”>:</td>
<td width=”82%”><input name=”subject” type=”text” id=”subject” size=”50″></td>
<td><textarea name=”detail” cols=”50″ rows=”4″ id=”detail”></textarea></td>
<td><input name=”name” type=”text” id=”name” size=”50″></td>
<td><input name=”customer_mail” type=”text” id=”customer_mail” size=”50″></td>
<td><input type=”submit” name=”Submit” value=”Send”> <input type=”reset” name=”Submit2″ value=”Reset”></td>

Step 3:  

Create a file called mailto.php using following code

// Contact subject
$subject =$_REQUEST[‘subject’];
// Details
//echo $subject;
// Mail of sender
// From
//$header=”from: $name <$mail_from>”;
// Enter your email address
$to =$_REQUEST[‘customer_mail’];
// Check, if message sent to your email
echo “Your Message has been sent”;
else {
echo “ERROR”;

Step 4:  

We have to make a few changes in php.ini configuration file. The changes are:

  • Open php.ini file by entering into XAMPP Installed folder.
    • Example: C:\xampp\php
  • Search for the line that reads [mail function]

Default php.ini file settings

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
sendmail_from = you@yourdomain

; For Unix only.  You may supply arguments as well (default: “sendmail -t -i”).
;sendmail_path =



[mail function]
; For Win32 only.

; For Win32 only.
;sendmail_from =

; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
sendmail_path =”\”C:\xampp\sendmail\sendmail.exe\” -t”

  • Save/close the php.ini file

Step 5:  

We have to make a few changes in sendmail.ini configuration file. The changes are:

  • Open sendmail.ini file by entering into XAMPP Installed folder.
    • Example path: C:\xampp\sendmail
  • Make the following changes





  • Save/close the sendmail.ini file
  • Restart your web server. (XAMPP Server)

Step 6:  Now run your program from your browser. It will work fine.

That’s all

Description for above Settings:

Gmail SMTP (Simple Mail Transfer Protocol) allows you to run any emails from an external email software program through Gmail in order to check for spam or viruses and to utilize Gmail’s security settings before you send them out.

The Gmail SMTP server settings for sending mail through Gmail from any email program are:

  • Gmail SMTP server address:
  • Gmail SMTP user name: Your full Gmail address (e.g.
  • Gmail SMTP password: Your Gmail password
  • Gmail SMTP port (TLS)587
  • Gmail SMTP port (SSL): 465
  • Gmail SMTP TLS/SSL required: yes

4 thoughts on “Send Email via Gmail SMTP Server in PHP

  1. I feel that is among the such a lot important info for me.
    And i’m satisfied studying your article. But wanna remark
    on some common issues, The web site style is ideal, the articles is really excellent : D.

    Just right task, cheers

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.