Sending WordPress email through an external SMTP server

September 10, 2015
Posted in: Code Snippets, How To, Web Development, WordPress

WordPress sends email when performing many built-in actions like registering a new user, resetting a password, or even notifying the administrator of a new comment or trackback. Contact form plugins like Gravity Forms and Contact Form 7 use wp_mail, so their messages are sent in the same manner.

If you use an external email service like Microsoft Exchange or Google Apps for Work, you can route everything through that service to speed up mail delivery, and help ensure that all of your emails come through.

This can be done by placing a bit of code in your theme’s functions.php file:

function send_via_smtp( $phpmailer ) {
$phpmailer->SetFrom("[email protected]", "From Name");
$phpmailer->AddReplyTo("[email protected]", "Reply-To Name");
$phpmailer->Host = "";
$phpmailer->SMTPAuth = true; // require smtp authentication?
$phpmailer->SMTPSecure = true; // security protocol (true/false or "tls")
$phpmailer->Port = 587; // port depends on security and server setup
$phpmailer->Username = "[email protected]";
$phpmailer->Password = "YOURPASSWORD";
add_action( "phpmailer_init", "send_via_smtp" );

Just get your SMTP settings from your web host or email administrator and replace the values above.  If your server doesn’t require SMTP authentication, you can exclude the username and password lines.

Scott Buckingham

President / Owner
613-801-1350 x101
[email protected]
Scott is a WordPress expert who has worked on hundreds of web design and development projects. He excels at finding creative ways to solve technical problems. View full profile