Use gmail to send email in php

This time I’m going to show you how you can send emails in php using gmail smtp server.

 

Requirements

 

First you have to download phpMailer from the link that I gave above. What I’m going to use in this tutorial is phpmailer for php version 5 and 6.

image

Just download the .zip file so that you won’t have any trouble extracting it. Especially if you are on windows.

Extract the file inside the wamp directory. Rename the extracted folder to phpmailer. So that it will be easier to remember.

image

To be consistent. After opening your phpmailer folder. It should contain most of these files:

image

Now, open up the class.phpmailer.php in a decent text editing tool like notepad++ or programmer’s notepad.

Press ctrl + h in your keyboard and search for $host or anything similar:

image

Until you find these lines:

public $Host          = 'localhost';
public $Port          = 25;

Those are the defaults. Change those to:

public $Host          = 'ssl://smtp.gmail.com';
public $Port          = 465;

You can also change it to other hosts. But this tutorial is only for using gmail smtp. So just search it on your own if you want to use other smtp servers.

 

Next, create a new php file. This time, inside your project folder. Include those 2 files that you edited earlier. Remember to change the example below based on the actual location of your phpmailer folder.

include('../../phpmailer/class.phpmailer.php');

Then create an object of the phpMailer class. And specify that it will be using SMTP or Simple Mail Transfer Protocol to send emails.

$mail = new PHPMailer();  
$mail->IsSMTP();

Also add this line to enable SMTP authentication. To authenticate that you are indeed a real human. And not a spammer bot.

$mail->SMTPAuth = true; 

Then add these lines. To specify which email address you are going to use as the sender email address:

$mail->Username = "blablabla@gmail.com"; 
$mail->Password = "mypasswordisthis"; 

I haven’t tested yet if you can use other  email addresses besides gmail. But you can certainly send emails to yahoo or hotmail account using this method.

Next, specify the email address where the recipient is going to reply:

$webmaster_email = "replyemail@gmail.com"; 

Then, the recipients email address. This can be yahoo, hotmail , or other email accounts as I have said a while ago.

$email="recipient@gmail.com"; 

Then, specify the name of the recipient together with the  email address that the recipient will see as the one who sent the mail.

$name="Master Buten"; 
$mail->From = $webmaster_email;

In the example above, we will send the email to master buten.

You can also specify your name after that:

$mail->FromName = "Master Kayosama";

Then we call the phpMailer function which uses the variables that we defined earlier as an argument:

$mail->AddAddress($email,$name);

Then call the other function that adds reply to information to the email that we are going to send:

$mail->AddReplyTo($webmaster_email, $mail->FromName);

You can also set the word wrap attribute to anything you want. Word wrap will allow you to format the body of your email in a nice way. If you specify it to be 50 then it creates a new line before or when 50 characters have been entered.

$mail->WordWrap = 50;

Then for adding attachments. You call the function addAttachment:

$mail->AddAttachment("embarassingmoments.jpg");

Then specify if the email is html.

$mail->IsHTML(true);

Add the subject and the body of the email:

$mail->Subject = "Training"; $mail->Body = "I send you this email because

we will have a training inside Mt. Pinatubo on Mar 31, 2011";

You can also add an alternative body if you want. This is the one that the recipient is going to see if he switches to plain text format.

$mail->AltBody = "I send you this email because
 we will have a training inside Mt. Pinatubo on Mar 31, 2011";

Lastly, add these lines in order for you to determine if the mail is indeed sent or not:

if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

Oops, were not ready to fire up that file yet. There’s one more thing to do.

Left click on the wamp tray icon. Select php-> php extensions.

image

Then you just have to ensure that php_openssl is checked. If not, then click it.

image

Next, restart all services.

image

Wait for the wamp tray icon to turn white before you do anything funny.

Fire up the php file that you have created. You should see something like the one below. If not, then review the instructions that I provided and see if you followed them well.

image

 

Conclusion

That’s an easy way to send email in php using gmail smtp. Please share this article if you like it. If you have questions or suggestions just throw them in the comments. Thanks for reading!

20 thoughts on “Use gmail to send email in php

  1. Pingback: Introduction to premium tutorials « Data Integrated Entity

  2. How do you protect this file from being downloaded online? I.e. how do you ensure no one will be able to access your email account?

  3. Thanx a lottttt….:)
    I had been trying this thing from a long time but dint find an appropriate solution…:P
    and ur code worked like magic…:)

  4. I’m having a error like this:
    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

  5. in which folder i have to paste the php mailer?. Though I pasted it into both in wamp folder and in www folder inside wamp. And i edited class.phpmailer.php file as you mentioned. You mentioned to include 2 files. one file is class.phpmailer.php and which is the other one?. And i created a file using your code inside the project folder with the following code.

    IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = “pradeepravin90@gmail.com”;
    $mail->Password = “vppkaA1!”;
    $webmaster_email = “pradeepravin90@gmail.com”;
    $email=”pradeeppravin90@gmail.com”;
    $name=”pradeep”;
    $mail->From = $webmaster_email;
    $mail->AddAddress($email,$name);
    $mail->AddReplyTo($webmaster_email, $mail->FromName);
    $mail->IsHTML(true);
    $mail->Subject = “example”;
    $mail->Body = “testing the code”;
    if(!$mail->Send())
    {
    echo “Mailer Error: ” . $mail->ErrorInfo;
    }
    else
    {
    echo “Message has been sent”;
    }
    ?>

    And the output which i got while running this code is:

    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

    Plz help me out..

  6. which folder i have to paste the php mailer?. Though I pasted it into both in wamp folder and in www folder inside wamp. And i edited class.phpmailer.php file as you mentioned. You mentioned to include 2 files. one file is class.phpmailer.php and which is the other one?. And i created a file using your code inside the project folder with the following code.

    IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = “pradeepravin90@gmail.com”;
    $mail->Password = “vppkaA1!”;
    $webmaster_email = “pradeepravin90@gmail.com”;
    $email=”pradeeppravin90@gmail.com”;
    $name=”pradeep”;
    $mail->From = $webmaster_email;
    $mail->AddAddress($email,$name);
    $mail->AddReplyTo($webmaster_email, $mail->FromName);
    $mail->IsHTML(true);
    $mail->Subject = “example”;
    $mail->Body = “testing the code”;
    if(!$mail->Send())
    {
    echo “Mailer Error: ” . $mail->ErrorInfo;
    }
    else
    {
    echo “Message has been sent”;
    }
    ?>

    And the output which i got while running this code is:

    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

    Plz help me out

    • One thing that you could do is to search Google, this article was written over a year ago so it might be possible that google has already changed their SMTP.
      As for the other file there’s no other file since you only need to include the class.phpmailer.php file in the file where you want to use phpmailer

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 )

Google+ photo

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

Connecting to %s