Send WordPress email over SMTP without a plugin
By default WordPress sends email with PHP’s mail() function, which hands the message to the mail program of the server. On many hosts these messages land in spam or never arrive: the server is not allowed to send for your domain (no SPF or DKIM record for it), and receiving servers reject or mark it. The fix is to send through a real SMTP server that is authorised for the domain: the mailbox provider, the host’s SMTP service or a transactional service such as Mailgun or SendGrid.
Problem: Password resets, form notifications and order emails from WordPress go to spam or are not delivered at all.
Solution: Configure PHPMailer, the library behind wp_mail(), on the phpmailer_init action to use SMTP with authentication. Keep the credentials in wp-config.php, and log failures with the wp_mail_failed action.
Credentials in wp-config.php
Above the line /* That's all, stop editing! */:
<?php define( 'HA_SMTP_HOST', 'smtp.example.com' ); define( 'HA_SMTP_PORT', 587 ); define( 'HA_SMTP_SECURE', 'tls' ); // 'tls' = STARTTLS on 587, 'ssl' = port 465 define( 'HA_SMTP_USER', 'noreply@example.com' ); define( 'HA_SMTP_PASS', 'the-smtp-password' );
The PHPMailer settings
In a small plugin or an mu-plugin:
<?php
add_action( 'phpmailer_init', 'ha_smtp_mailer' );
function ha_smtp_mailer( $phpmailer ) {
if ( ! defined( 'HA_SMTP_HOST' ) ) {
return; // not configured: keep the default
}
$phpmailer->isSMTP();
$phpmailer->Host = HA_SMTP_HOST;
$phpmailer->Port = HA_SMTP_PORT;
$phpmailer->SMTPSecure = HA_SMTP_SECURE;
$phpmailer->SMTPAuth = true;
$phpmailer->Username = HA_SMTP_USER;
$phpmailer->Password = HA_SMTP_PASS;
// The sender must be an address the SMTP account may send as
$phpmailer->setFrom( HA_SMTP_USER, get_bloginfo( 'name' ), false );
}
add_action( 'wp_mail_failed', function ( $error ) {
error_log( 'wp_mail failed: ' . $error->get_error_message() );
} );
The action receives the PHPMailer object by reference just before every message is sent, so all email that goes through wp_mail() uses SMTP: core notifications, WooCommerce, Contact Form 7 and other plugins.
The third argument of setFrom() (false) keeps the Sender (the envelope address) unchanged. Many SMTP providers reject messages whose From address belongs to another domain, so do not let forms put the visitor’s address into From; use Reply-To for it.
Testing
wp eval 'var_dump( wp_mail( "you@example.com", "SMTP test", "It works." ) );'
true means the SMTP server accepted the message. If it is false, the error from wp_mail_failed is in the PHP error log (or in wp-content/debug.log with WP_DEBUG_LOG). For a detailed conversation with the server, set $phpmailer->SMTPDebug = 2; temporarily.
Delivery to the inbox also depends on DNS: add the SPF and DKIM records your SMTP provider gives you for the domain.
NOTE: Keep the password out of the theme and out of Git. wp-config.php is usually not in the repository; on hosts with environment variables, read the value with getenv() instead. If you prefer a settings screen, plugins such as WP Mail SMTP do the same configuration through the admin.