How To Send Email Using PHPMailer and GMail

Photo of Mike Dalisay
Modified Monday, January 24, 2011
by - @ninjazhai
Hi there! Today we'll be sending email using PHPMailer Library and your Gmail account. This code works even if you test it on your local machine (localhost) since the mailer server it uses is the Google mail server. You just have to be connected on the internet and you don't have to install your own mailer server haha!

How To Send Email Using PHPMailer and GMail
Wanna send some mail?

You may download PHPMailer Library in this link:

$mailer = new PHPMailer();
$mailer->Host = 'ssl://';
$mailer->Port = 465; //can be 587
$mailer->SMTPAuth = TRUE;
// Change this to your gmail address
$mailer->Username = '';  
// Change this to your gmail password
$mailer->Password = 'yourpassword';  
// Change this to your gmail address
$mailer->From = '';  
// This will reflect as from name in the email to be sent
$mailer->FromName = 'Your Name'; 
$mailer->Body = 'This is the body of your email.';
$mailer->Subject = 'This is your subject.';
// This is where you want your email to be sent
    echo "Message was not sent<br/ >";
    echo "Mailer Error: " . $mailer->ErrorInfo;
    echo "Message has been sent";

If you want to add some attachments, just add a line something like this:


For instant testing, you may download this code with the library files used and change the variables to your settings:

You can find more examples here.
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.