Send WordPress email over SMTP: wp_mail with phpmailer_init, or a plugin

Two ways to make wp_mail use authenticated SMTP: a 15-line mu-plugin using the phpmailer_init hook (no plugin needed) or WP Mail SMTP with "Other SMTP". Where to store credentials in wp-config.php and how to test.

Install

shell
# option A: no plugin — create wp-content/mu-plugins/smtp.php

Complete example

WordPress — STARTTLS on 587, credentials from the environment
<?php
// wp-content/mu-plugins/smtp.php  — loaded automatically, survives theme changes
add_action('phpmailer_init', function ($phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host       = SMTP_HOST;
    $phpmailer->Port       = SMTP_PORT;
    $phpmailer->SMTPSecure = SMTP_PORT == 465 ? 'ssl' : 'tls';
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Username   = SMTP_USER;
    $phpmailer->Password   = SMTP_PASS;
    $phpmailer->From       = SMTP_FROM;
    $phpmailer->FromName   = SMTP_FROM_NAME;
});

// wp-config.php (above "That's all, stop editing!")
define('SMTP_HOST', 'smtp.queensmtp.com');
define('SMTP_PORT', 587);
define('SMTP_USER', 'your-smtp-username');
define('SMTP_PASS', 'your-smtp-password');
define('SMTP_FROM', 'hello@yourdomain.com');
define('SMTP_FROM_NAME', 'Your Site');

// test from WP-CLI
// wp eval 'var_dump(wp_mail("you@yourdomain.com", "SMTP test", "It works"));'

What to know

  • Option B: install WP Mail SMTP (or the QueenSMTP plugin), choose "Other SMTP", enter host smtp.queensmtp.com, port 587, encryption TLS, authentication on, and the same credentials. The plugin adds a log and a test button.
  • WooCommerce order mail, password resets and contact forms all go through wp_mail, so this one change fixes every plugin.
  • The From address must be on a domain you verified (SPF + DKIM) at the relay; a From of @gmail.com through another server fails DMARC.
  • Hosts with hourly caps (cPanel "max emails per hour") do not apply when you relay externally — that is the usual reason stores switch.

Same code, different provider

Only the host, port and credentials change. The rules per provider are on the SMTP settings hub.

ProviderHostPortUsernamePasswordNote
QueenSMTP smtp.queensmtp.com587 (STARTTLS) / 465 (SSL) / 2525SMTP username from the dashboardSMTP password$5/year + $0.10 per 1,000; 100 free/day
Gmail smtp.gmail.com587 / 465you@gmail.comApp Password500/day, From must be the account
Amazon SES email-smtp.<region>.amazonaws.com587 / 465 / 2587SES SMTP usernameSES SMTP passwordsandbox 200/day until approved
Office 365 smtp.office365.com587user@yourdomain.commailbox passwordSMTP AUTH must be enabled; 30/min
SendGrid smtp.sendgrid.net587 / 465 / 2525apikeyAPI keyno free plan since 2025

Credentials for the example above

Create a free account, verify your sending domain (SPF + DKIM shown in the dashboard), add an SMTP credential, and paste it into the code. 100 emails a day free; $5/year plus $0.10 per 1,000 after that. Prefer HTTPS? The same account has a REST email API.

Get SMTP credentials Test your settings first

Other languages and frameworks

Examples tested 2026-08-30. If a library changed its API, tell us.

WordPress SMTP — questions

Yes — hook phpmailer_init in a must-use plugin (15 lines above) and define the SMTP constants in wp-config.php. Plugins add logging and a test UI on top of the same mechanism.

WP Mail SMTP with the "Other SMTP" option works with any provider; the QueenSMTP plugin pre-fills our host and ports. Both use PHPMailer underneath.