Using Gmail for SMTP with WordPress
We frequently experience minor issues when configuring PHP applications that require email, like WordPress. Periodically, the client site is on a Windows server, which requires the use of SMTP to send email. On a Linux/Apache server, the PHP mail function is very simple and almost always works with the application out of the box.
In one particular case, for a WordPress installation we wanted to use Gmail as the SMTP server so mail would go directly to a Gmail account. We knew smtp.gmail.com could be used in an email client such as Outlook, but didn’t know about configuring it in WordPress. A quick search led us to this post in the WordPress forums, which describes a pretty simple method for setting smtp.gmail.com as the outgoing server. To get it to work, we had to omit one step, which we describe below.
Apparently there are WordPress plugins to accomplish it, but this method is very quick and we’d rather not use plugins unless absolutely necessary.
As detailed in the above forum post, we replaced the PHPMailer files (wp-includes/class-[phpmailer|smtp|pop].php) with the newer (2.2.1) files because smtp.gmail.com uses SSL and the WordPress class files don’t support it (as of v. 2.6.2).
Then we edited the smtp parameters in wp-include/class.phpmailer.php:
public $Host = 'smtp.gmail.com';
public $Port = 465;
public $SMTPSecure = 'ssl';
public $SMTPAuth = true;
public $Username = '[username]';
public $Password = '[password]';
Next we edited wp-include/pluggable.php so the updated PHPMailer file names match (the original files used hyphens - e.g. ‘/class-phpmailer.php’).
require_once ABSPATH . WPINC . '/class.phpmailer.php';
require_once ABSPATH . WPINC . '/class.smtp.php';
This last step is where we had trouble. The WordPress post says to make this change:
$phpmailer->IsSMTP(); // Use to be $phpmailer->IsMail();
This resulted in a PHP error (blank page). When we changed the line back to the original, Gmail smtp worked like charm.
$phpmailer->IsMail();
It would be nice if WordPress had built-in mail server configuration, but for now it’s not too difficult to edit the class files.
As with any WordPress hacks, take care to update the files when you upgrade WordPress.
Props to WordPress member achampion for his post.