How To Send Email Using PHPMailer and GMail

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 = '[email protected]';  
// Change this to your gmail password
$mailer->Password = 'yourpassword';  
// Change this to your gmail address
$mailer->From = '[email protected]';  
// 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
$mailer->AddAddress('[email protected]');  
    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.

Hi! I'm Mike Dalisay, the co-founder of, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.