Sending E-mails via SMTP with PHPMailer and GMail
Why using GMail for sending mail messages?
First of all it’s FREE! Sure most website owners can use their own SMTP server for sending email messages from their website, but it makes sense even than to use GMail for sending mail. The chance is big that your websites IP address is on a blacklist if your site is on hosted by a shared web hosting provider. If not or you host your site on your own server, there is always a risk that your IP address get blacklisted. Because of some limitations, the SMTP server from Google is a good choice applications with less than 500 recipients a day, check this information from the Google help pages.
Requirements
You need for this PHPMailer code example a PHP5 enabled web host (I did tests only on Linux), the port 465 need to be open and of course you need a GMail or Google Apps account.
Trouble sending e-mails with your “normal” GMail account? If you own a domain name, you can register for a Google Apps account or check below the tutorial update about an alternative SMTP service!
PHPMailer tutorial for GMail and Google Apps
- If you don’t have one, register a GMail account or setup your domain for Google applications.
- Download a recent version of PHPMailer (I’m using the version 5.02)
- Check that your web hosting provider has opened the port 465 (TCP out), if not ask him to open that port
- Include the PHPMailer class file:
require_once('phpmailer/class.phpmailer.php');
- Create those two constant variables to store your GMail login and password. Use the login for your Google Apps mail account if you have one.
12define(‘GUSER’, ‘you@gmail.com’); // GMail usernamedefine(‘GPWD’, ‘password’); // GMail password
- Use the following function to send the e-mail messages (add the function in one of your included files):
1234567891011121314151617181920212223function smtpmailer($to, $from, $from_name, $subject, $body) {global $error;$mail = new PHPMailer(); // create a new object$mail->IsSMTP(); // enable SMTP$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only$mail->SMTPAuth = true; // authentication enabled$mail->SMTPSecure = ‘ssl’; // secure transfer enabled REQUIRED for GMail$mail->Host = ‘smtp.gmail.com’;$mail->Port = 465;$mail->Username = GUSER;$mail->Password = GPWD;$mail->SetFrom($from, $from_name);$mail->Subject = $subject;$mail->Body = $body;$mail->AddAddress($to);if(!$mail->Send()) {$error = ‘Mail error: ‘.$mail->ErrorInfo;return false;} else {$error = ‘Message sent!’;return true;}}
Most of the settings inside the function are required by GMail. While searching for PHPmailer tutorials I found articles with different settings for the port and security. My advice is to use the settings from this tutorial.
- Call the function within your code:
1smtpmailer(‘to@mail.com’, ‘from@mail.com’, ‘yourName’, ‘test mail message’, ‘Hello World!’);
Use this more “advanced” usage inside your application:
1234if (smtpmailer(‘to@mail.com’, ‘from@mail.com’, ‘yourName’, ‘test mail message’, ‘Hello World!’)) {// do something}if (!empty($error)) echo $error;
Advanced setup with fall-back SMTP server
Because of the e-mail message limit it might be useful to use a secondary SMTP server if the GMail option is unable to send the message. For this functionality you need to replace the part with the SMTP settings. First create login/server variables for the second SMTP server:
1
2
3
|
define(‘SMTPUSER’, ‘you@yoursmtp.com’); // sec. smtp username
define(‘SMTPPWD’, ‘password’); // sec. password
define(‘SMTPSERVER’, ‘smtp.yoursmtp.com’); // sec. smtp server
|
Next we need to create an if/else statement using the variables for the second server (replace).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if ($is_gmail) {
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘smtp.gmail.com’;
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
} else {
$mail->Host = SMTPSERVER;
$mail->Username = SMTPUSER;
$mail->Password = SMTPPWD;
}
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = ‘Mail error: ‘.$mail->ErrorInfo;
return false;
} else {
$error = ‘Message sent!’;
return true;
}
}
|
And next use the modified PHPMailer function as followed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$msg = ‘Hello World’;
$subj = ‘test mail message’;
$to = ‘to@mail.com’;
$from = ‘from@mail.com’;
$name = ‘yourName’;
if (smtpmailer($to, $from, $name, $subj, $msg)) {
echo ‘Yippie, message send via Gmail’;
} else {
if (!smtpmailer($to, $from, $name, $subj, $msg, false)) {
if (!empty($error)) echo $error;
} else {
echo ‘Yep, the message is send (after doing some hard work)’;
}
}
|
Both examples are very simple and demonstrate only how-to send e-mail messages using PHPmailer with the SMTP server provided by GMail. Of course you can extend the code to handle HTML messages and attachments etc. If you have any issues with these examples, just let me know.
UPDATE: Alternate SMTP service
Since this tutorial was published, I got several messages that the free GMail account doesn’t allow you to send a lot of e-mail messages from your own websites. Your can use this PHPmailer example code for any other SMTP server or service. I suggest to use ElasticEmail, a premium e-mail service provider in the cloud. Signup for a free account which is good for the first 25.000 mail messages and 10.000 contacts every month.
If you use the tutorial code with ElasticEmail you need to replace this part of code
1
2
3
4
5
6
7
8
9
10
11
|
if ($is_gmail) {
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘smtp.gmail.com’;
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
} else {
$mail->Host = SMTPSERVER;
$mail->Username = SMTPUSER;
$mail->Password = SMTPPWD;
}
|
with this code
1
2
3
4
|
$mail->Host = ‘smtp.elasticemail.com’;
$mail->Port = 2525;
$mail->Username = ‘your username’;
$mail->Password = ‘your password’;
|
Optional: Remove the “$is_gmail” attribute from the function.
Leave a Reply