Tuesday, January 14, 2014

Using Gmail’s SMTP as your SMTP Server with a PHP example.

Gmail provides amazing features for developers; they provide developers to use their SMTP server to send emails from the application. In this article we’ll be configuring Gmail’s SMTP server for our application, we’ll also use an open source project called PHPmailer to send email from our Application.

Using Gmail’s SMTP servers for your application assures you that your email is sent, whereas sometimes when we use our own SMTP server we face problems like getting blocked or marked as spam by the automated spam filtering features. Another benefit I’ll like to mention here is if you use Gmail’s SMTP server then the emails which are sent from your application will be stored in Gmail’s Database. Another reason for using Gmail’s SMTP is that it’s not using Port 25 because there are many ISPs who are blocking the emails sent using Port 25.

Points to be noted:
Before starting to configure the Gmail’s SMTP server, it’s important for you to know that Gmail’s SMTP server only allows 99 emails per day. It means that you can only send 99 emails every day. The limit before was 250 emails per day, but because of high usage the limit decreased and came from 250 to 99 emails per day. Second point to note is that Gmail’s SMTP server requires authentication before sending emails. So make sure you have the password of the email which you’ll be using to send emails.

First you’ll need to login into Gmail account. After logging in navigate the settings button at the top right corner of your browser.

Now click on Settings, after clicking you’ll see a page like this:

Now click on Forwarding and POP/IMAP then you’ll see a like this:

Now just make sure all the setting which is shown above is the same for your account. If not then you can do it, it’s damn easy to do.
Below are the SMTP information which you’ll need to use in your application.
SMTP Server: smtp.gmail.com
SMTP Port: 465
SMTP Username: (your gmail email address, eg: example@gmail.com)
SMTP Password: (your gmail account password)

Example Application:
Let us make an example PHP application which sends email using Gmail’s SMTP server.
In this example we’ll be using PHPmailer which is an open source project you can know more about it on their official Github. Here’s the PHP code:

function mail_sender($to, $subject, $body) {
   require_once 'class.phpmailer.php';
   $from = "YOU_EMAIL_ADDRESS";
   $mail = new PHPMailer();
   $mail->SMTPAuth = true;
   $mail->Mailer = "smtp";
   $mail->Host = "tls://smtp.gmail.com";
   $mail->Port = 465;
   $mail->Username = "YOUR_EMAIL_ADDRESS";
   $mail->Password = "YOU_PASSWORD";
   $mail->SetFrom($from, 'YOUR_NAME');
   $mail->Subject = $subject;
   $address = $to;
   $mail->AddAddress($address, $to);

   if($mail->Send()) {
return true;
   }else {
return false;

Above we have created a function called mail_sender, this function will help us to send email without writing the SMTP details again and again. We have saved this function in a PHP file called mailer.php.
Now we are going to use this function.

   require_once 'mailer.php';
   $subject = "Test Mail Subject";
   $body = "Hi<br/>Test Mail<br/>Localhost Test";
   if(mail_sender($to, $subject, $body)) {
echo 'Sent!';
   }else {
echo 'Error!';

When the email is sent then it will look something like this:

If you have followed all the steps and done everything thing properly then surely you’ll send an Email. If you have got any errors or problem then first thing you should do is to check whether you have configure the Forwarding and POP/IMAP properly. If still you find any problem then check that the details you used in your application is correct. If still you receive any error then comment below.

Liked the post ? subscribe us with your email to get upcoming tutorials directly in your inbox:


No comments:

Post a Comment

Your Comment Will Be Visible After Approval, Thanks !