Sending E-mails Using PHPMailer and Gmail
=========================================
Ever since Gmail became public back in 2007 it has become a popular e-mail service provider. As such it is not unusual that people would wish to use the Gmail servers to send e-mails from their scripts. Unfortuantely due to the fact that Gmails servers require authentication you cannot use the standard [m]mail[/m] function that's built into PHP. Perhaps the two most commonly use free alternatives are [link=http://pear.php.net/package/Mail]PEAR Mail[/link], the package that comes as part of the PEAR framework and [link=http://phpmailer.worxware.com/]PHP Mailer[/link] by Worx International Inc. It is the latter of these that I will be looking at today.
I first attempted to use PHP Mailer to connect to Gmails SMTP server when I was asked for some help in our IRC channel. I soon came to realise why the user was having trouble, as after a quick search of Google I found many links, but no complete solutions. To make life easier for other people searching for this information, I have decided to create this article/tutorial detailing what I found.
With Gmail there are two types of authentication available, TLS and SSL. I have covered both for the sake of completeness.
SSL
======
If you wish to use SSL you will need to have the php_openssl extension enabled on your server, to check if it is enabled simply create a new script containing the following...
...and run it on your server. You will hopefully find a section entitled OpenSSL which will list OpenSSL support as enabled. If you do not find a section headed OpenSSL then it is not currently enabled on your server. If you do not have access to change your php.ini then you cannot enable the extension and will have to try TLS instead. To enable the extention, simply load up your php.ini file in your favourite webite and remove the semicolon from the start of the following line...
;extension=php_openssl.dll
NB: You must restart your webserver for the change to take effect. Once you have done so you can run the phpinfo script again to make sure it is now enabled.
It is this extension that seems to catch people out most often, most sites I came across did not mention it being a requirement, listing instead just the ports required. Once the extension is enabled we are ready to begin coding.
[php]// include the PHPMailer main class
require("class.phpmailer.php");
// login details
$username = 'example@gmail.com'; // change to your e-mail address
$password = 'password123'; // change to the password of your Gmail account
// a recipient for our test e-mail
$recipient = array('email'=>'example@hotmail.com', 'name'=>'Mr Example');
// details about the sender of the e-mail
$webmaster = array('email'=>'example@gmail.com', 'name'=>'Webmaster');
// create object
$mail = new PHPMailer();
// tell the object we're using SMTP
$mail->IsSMTP();
/********************
* This is the most important section of this script, it sets
* the host for the connection and provides the authentication
* details. You will perhaps also see scripts that will set
* the SMTPSecure, Host and Port seperately, for simplicity
* I set them all together through the Host property.
********************/
$mail->Host = "ssl://smtp.gmail.com:465";
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
// set the basic e-mail settings
$mail->SetFrom($webmaster['email'], $webmaster['name']);
$mail->AddAddress($recipient['email'], $recipient['name']);
$mail->Subject = "This is the subject";
$mail->Body = "This is simply a test, does it work?";
// attempt to send the e-mail
if(!$mail->Send()) {
echo "Oops: An error was encountered... " . $mail->ErrorInfo;
} else {
echo "Yay: Message sent successfully.";
}[/php]
TLS
======
If you cannot enabled SSL on your server, or would prefer to use TLS, the code to be used is very similar. The only section that is actually changed is the section I have commented as 'set required authentication settings', however for completeness sake I have included the whole code snippet again.
[php]// include the PHPMailer main class
require("class.phpmailer.php");
// login details
$username = 'example@gmail.com'; // change to your e-mail address
$password = 'password123'; // change to the password of your Gmail account
// a recipient for our test e-mail
$recipient = array('email'=>'example@hotmail.com', 'name'=>'Mr Example');
// details about the sender of the e-mail
$webmaster = array('email'=>'example@gmail.com', 'name'=>'Webmaster');
// create object
$mail = new PHPMailer();
// tell the object we're using SMTP
$mail->IsSMTP();
/********************
* This is the most important section of this script, it sets
* the host for the connection and provides the authentication
* details. You will perhaps also see scripts that will set
* the SMTPSecure, Host and Port seperately, for simplicity
* I set them all together through the Host property.
********************/
$mail->Host = "smtp.gmail.com:587";
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
// set the basic e-mail settings
$mail->SetFrom($webmaster['email'], $webmaster['name']);
$mail->AddAddress($recipient['email'], $recipient['name']);
$mail->Subject = "This is the subject";
$mail->Body = "This is simply a test, does it work?";
// attempt to send the e-mail
if(!$mail->Send()) {
echo "Oops: An error was encountered... " . $mail->ErrorInfo;
} else {
echo "Yay: Message sent successfully.";
}[/php]
[b]The information contained in this article was based on using PHPMailer v5.1 with PHP v5.3.1, but will likely hold true for other versions.[/b]