Increasing the WP-Mail-SMTP Timeout

WP-Mail-SMTP is a popular WordPress plugin that lets you configure your site to send email through an external SMTP host rather than directly from WordPress. It does this by sending special parameters to WordPress’s built-in version of PHPMailer.

The problem was, I was only getting mail part of the time. Using WP-Mail-SMTP’s helpful test function, I was able to duplicate the problem. Mail sending failed after 10 seconds or so. The following message appeared at the bottom of the debug output:

SMTP -> ERROR: Failed to connect to server: Connection timed out (110)

The plugin’s home page has some troubleshooting tips but they do not mention timeout errors. So I started digging into the debug output (which is generated by PHPMailer, not the WP-Mail-SMTP plugin) and found two time-related parameters:

timeout – set to 10 seconds – apparently the connection timeout

timelimit – set to 30 seconds – apparently the total time to send the mail

Apparently the DreamHost servers are sometimes not able to complete a connection to our external SMTP server within 10 seconds, so the plugin is timing out.

Where Do the Values Come From?

Further digging revealed that the plugin does not set these time values when calling PHPMailer, so PHPMailer uses its default values. This version of WordPress includes PHPMailer 5.2.7. In that version, timeout is set in /wp-includes/class-phpmailer.php to 10 seconds, and timelimit is set in /wp-includes/class-smtp.php to 30 seconds. It looks like PHPMailer version 5.2.8, to fix issue 104, will increase both to 300 seconds per RFC2821 section 4.5.3.2.

Hacking the WP-Mail-SMTP Plugin

Fortunately, it’s pretty easy to set both of the time values when calling PHP mailer. In fact, WP-Mail-SMTP provides a way to set values outside the plugin. I found that I could add the following lines to the end of /wp-config.php and the plugin would pick up the new values (30 and 60 seconds respectively):

// Add a custom hook for the WP-Mail-SMTP plugin to extend the SMTP timeout.
// Adapted from 5/29 comment on http://www.callum-macdonald.com/2008/12/12/wp-mail-smtp-v08/comment-page-9/
add_filter('wp_mail_smtp_custom_options', 'smtp_timeout');
function smtp_timeout($phpmailer){
  $phpmailer->Timeout = 30;
  $phpmailer->Timelimit = 60;
  return $phpmailer;
}

Updated WP-Mail-SMTP Plugin

However, I thought it would be nice if the plugin actually offered UI elements for setting the time values, so I updated the base plugin. Here it is, adapted from version 0.9.5 and using a default of 300 seconds for both values as in the newer PHPMailer:

If you replace your plugin with that code, you should see this near the bottom of the plugin’s Settings page:

WP-Mail-SMTP timeouts

Please note that I have only tested this briefly and not at all with WPMU. Use at your own risk!

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.