Hello, Folks!

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.
Firstly, we’ll take a look at thePHPMailer library we’re going to use.
You can take this library as an alternative of mail() function in PHP.
After that we’ll discuss the process.


 

Send Email via Gmail SMTP server in PHP


 

You can purchase our service Mailget for the complete solution.


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.

Gmail SMTP Enable Settings

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.


Files to be needed:

We’re going to use a PHPMailer library which you can find on github repository.
You can take it as an alternative to the mail() function of PHP.


Process:

Let’s take a look at the full code of the PHP file.

PHP file : index.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send email via Gmail SMTP server in PHP</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<meta name="robots" content="noindex, nofollow">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-43981329-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="main">
<h1>Send email via Gmail SMTP server in PHP</h1>
<div id="login">
<h2>Gmail SMTP</h2>
<hr/>
<form action="index.php" method="post">
<input type="text" placeholder="Enter your email ID" name="email"/>
<input type="password" placeholder="Password" name="password"/>
<input type="text" placeholder="To : Email Id " name="toid"/>
<input type="text" placeholder="Subject : " name="subject"/>
<textarea rows="4" cols="50" placeholder="Enter Your Message..." name="message"></textarea>
<input type="submit" value="Send" name="send"/>
</form>
</div>
</div>
<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>
</body>
</html>

Let’s take a look at our form code :

 //Form Code

<form action="index.php" method="post">
<input type="text" placeholder="Enter your email ID" name="email"/>
<input type="password" placeholder="Password" name="password"/>
<input type="text" placeholder="To : Email Id " name="toid"/>
<input type="text" placeholder="Subject : " name="subject"/>
<textarea rows="4" cols="50" placeholder="Enter Your Message..." name="message"></textarea>
<input type="submit" value="Send" name="send"/>
</form>

In this code, I’ve made 3 input fields for sender’s username and password and recipient’s email address. Along with this, I’ve also made a text area field for the message, sender wants to send.


 //PHP Script
<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
// Fetching data that is entered by the user
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];

// Configuring SMTP server settings
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;

// Email Sending Details
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);

// Success or Failure
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>

The above code is responsible for the Gmail SMTP server settings, mail details and error messages on success and failure.

Now, lets understand this code line by line.


require 'PHPMailerAutoload.php';

The above code is used to include PHPMailerAutoload file. PHPMailerAutoload is present in PHPMailer library.


$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];

The above code is used to fetch data entered by the user in the form.


$mail = new PHPMailer;

Here, we’re creating the object of PHPMailer class.


$mail->isSMTP();

isSMTP() is used to tell that we want to send message using SMTP service particularly. There can be other ways to send mail like mail() function of php etc.


$mail->Host = 'smtp.gmail.com';

The above code is use to set the hostname of the mail server.


$mail->Port = 587;

The above code is used to set the SMTP port number for the authenticated TLS.


$mail->SMTPSecure = 'tls';

The above code is used to set the encryption system. TLS is the successor of SSL (deprecated).


$mail->SMTPAuth = true;

The above code is used to enable the SMTP authentication.


$mail->Username = $email;

The above code is used to set the username of the sender.


$mail->Password = $password;

The above code is used to set the password of the sender.


$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);

The above code is used to set the recipient’s email address.


if (!$mail->send())
{
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else
{
echo '<p id="para">Message sent!</p>';
}

So this is the complete process of sending email via Gmail SMTP server in PHP.


CSS file : style.css

@import url(http://fonts.googleapis.com/css?family=Raleway);

h1{
text-align:center;
//color: black;
font-size: 2em;
margin-top: 40px;
margin-bottom: 40px;
}

#main{
margin: 25px 100px;
font-family: 'Raleway', sans-serif;
}

h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px 40px;
color: black;
font-weight: bolder;
font-size: 1.5em;
margin-top: -1px !important;
// margin-bottom: -19px !important;
}

hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}

#login{
width:580px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 0px 40px 0px;
margin-top: 70px;
//margin: 50px;
margin: 0% 25%;
}

input[type=text],input[type=email],input[type=password]{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}

textarea{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
margin-bottom: 5px;
font-size: 16px;
font-family:raleway;
}

input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 40px;
}
#para{
clear: both;
margin: 0 35%;
}

Conclusion:

I hope that at this point you must be feeling yourself comfortable with sending email via Gmail SMTP server in PHP. You can also try sending mail using other SMTP servers. Please comment for any query. Keep visiting our website.