Header Banner
wonderhowto.mark.png
Gadget Hacks Next Reality Food Hacks Null Byte The Secret Yumiverse Invisiverse Macgyverisms Mind Hacks Mad Science Lock Picking Driverless
Home
PHP

How to Send Emails using PHPMailer and Gmail

Jan 17, 2011 08:40 PM

PHPMailer is a fully featured email transfer class for PHP and its popularity has grown rapidly over the years. Recently I had to send e-mail messages using the GMail SMTP server for one of my clients.

Up to now I had only sent email messages using SMTP servers provided by the hosting company. The difference here is that I had to use SSL encryption to send emails. Using hosting company SMTP server has its drawbacks as there is a chance that your website IP address is already on a blacklist simply because someone else had abused the service before you.

Requirements

  • Latest version of PHPMailer
  • A web host having PHP 5 / 6 enabled with Open SSL
  • GMail or Google Apps account

Sending Email

  • Check with your web hosting provider if port 465 (TCP out) is open, if not check with your hosting provider. Port 465 is needed to connect with the GMail SMT server.
  • Include PHPMailer class in your mailing script as follows

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

  • Add the following code to your mailing script

$mail  = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP

$mail->Host         = “mail.googlemail.com”; // GMAIL SMTP server

$mail->Port         = 465;                   // set the SMTP port for the GMAIL server

$mail->SMTPDebug    = 0;  // debugging: 0 = none, 1 = errors and messages,

// 2 = messages only

$mail->SMTPAuth     = true;                  // enable SMTP authentication

$mail->SMTPSecure   = 'ssl'; // secure transfer enabled and REQUIRED for GMail

$mail->Username     = ‘yourname@gmail.com’; // SMTP account username

$mail->Password     = ‘yourpassword’;        // SMTP account password

$mail->AddAddress('receiver@somedomain.com'); // receiver mail address

$mail->SetFrom(‘your name’, ‘yourname@gmail.com’); // from address

$mail->Subject = ‘Test mail’; // mail subject

$mail->MsgHTML(“Message Body”); // HTML message body

if(!$mail->Send())

{

$msg = "Mail could not be sent. <br />Error Description : " . $mail->ErrorInfo; } else

{

$msg = 'Email Message sent successfully!';

}

echo $msg;                        

As a final note please do check the GMail SMTP limitations mentioned here.

You already know how to use your phone. With Gadget Hacks' newsletter, we'll show you how to master it. Each week, we explore features, hidden tools, and advanced settings that give you more control over iOS and Android than most users even know exists.

Sign up for Gadget Hacks Weekly and start unlocking your phone's full potential.

Related Articles

Comments

No Comments Exist

Be the first, drop a comment!