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

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

Step 3:  

Create a file called mailto.php using following code

<?php
// Contact subject
$subject =$_REQUEST[‘subject’];
// Details
$message=$_REQUEST[‘detail’];
//echo $subject;
// Mail of sender
$mail_from=$_REQUEST[‘customer_mail’];
$name=$_REQUEST[‘name’];
// From
//$header=”from: $name <$mail_from>”;
// Enter your email address
$to =$_REQUEST[‘customer_mail’];
$send_contact=mail($to,$subject,$message);
// Check, if message sent to your email
if($send_contact){
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.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25


; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain


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

 

to

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=587

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = prcybercafes@gmail.com

; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
; http://php.net/sendmail-path
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

[sendmail]

smtp_server=smtp.gmail.com

smtp_port=587

smtp_ssl=tls

auth_username=****@gmail.com
auth_password=*******
force_sender=****@gmail.com

  • 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: smtp.gmail.com
  • Gmail SMTP user name: Your full Gmail address (e.g. example@gmail.com)
  • 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:

WordPress.com Logo

You are commenting using your WordPress.com 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.