Skip to content
iBlockSpam
Running email day to day

How do you set up WordPress SMTP with SPF and DKIM?

By iBlockSpam Team How we check our guides Updated 10 min read 8 sources
Short answer

Pick one sending service: your mailbox provider, your host's mail server or a transactional email service. Point WordPress at it with an SMTP plugin or a phpmailer_init hook, on port 587 with STARTTLS or 465 with TLS. Use a From address on your own domain, add the service to your one SPF record, and turn on DKIM for your domain.

WordPress sends email for password resets, contact forms, WooCommerce orders and admin alerts. Out of the box it hands those messages to the web server's mail program, and many of them land in spam or never arrive. This guide shows how to switch WordPress to authenticated SMTP, which settings to use for Microsoft 365, Google and typical hosts, and the DNS records that make receivers trust the result.

Why does WordPress email go to spam?

WordPress email goes to spam because, by default, it's sent unauthenticated from the web server. WordPress core calls PHP's mail() through the PHPMailer library, and if nothing sets a sender, the From address is wordpress@ followed by your site's domain. That server usually isn't in your SPF record and doesn't sign with DKIM for your domain, so receivers can't tell the mail is really yours.

Google's sender guidelines require every sender to "set up SPF or DKIM email authentication for your sending domains", and anyone sending 5,000 or more messages a day to Gmail needs SPF, DKIM and DMARC. Our guide on website form emails going to spam explains the mail() problem and form-specific issues in more depth. This guide is the setup.

What do you need before you start?

Have these ready before you change anything:

  1. One sending address on your domain, such as website@example.com. It doesn't need to receive mail, but a real mailbox makes bounces easier to read.
  2. A sending service that will accept authenticated SMTP from your site (see the next section).
  3. The SMTP host, port, encryption type and credentials from that service.
  4. Access to your DNS at your registrar or DNS host, to edit SPF and add DKIM records.
  5. A test mailbox at an outside provider, such as a personal Gmail or Outlook.com account.

Which SMTP service should WordPress use?

Use the service that already sends your domain's mail if its limits fit, or a transactional email service for anything that emails customers. These settings were checked on each provider's own pages at the time of writing (November 2026).

Service Host and port Sign-in Limits and notes
Microsoft 365 client submission (SMTP AUTH) smtp.office365.com, port 587 with STARTTLS (TLS 1.2 or 1.3) A licensed mailbox; OAuth recommended 10,000 recipients a day and 30 messages a minute. SMTP AUTH must be enabled for the mailbox
Google smtp.gmail.com Port 465 (SSL) or 587 (TLS) App password (needs 2-Step Verification) or OAuth 2,000 messages a day
Google Workspace SMTP relay smtp-relay.gmail.com, port 25, 465 or 587 Allowed IP addresses or SMTP authentication, set by an admin 10,000 recipients per user per day
Your host's mail server (cPanel, Plesk) Usually mail.yourdomain on 465 or 587 A mailbox on the host Shared IP reputation; check your host's limits
Transactional email service Provider's SMTP host on 587 or 465, or an API API key or SMTP credentials Built for order and account email; set up custom-domain DKIM

The Microsoft figures come from Microsoft Learn, which also warns: "If your device or application recommends or defaults to TCP port 465, it doesn't support the required versions of TLS" for Microsoft 365. The Google figures come from Google Workspace Admin Help. For the relay's Admin console steps, see our Google Workspace SMTP relay guide.

Microsoft 365: two things to check first

SMTP AUTH is off for many tenants. Microsoft says it's "disabled for organizations created after January 2020", and security defaults also block it. To turn it on for just the sending mailbox, go to the Microsoft 365 admin center > Users > Active users, select the user, then Mail > Manage email apps and tick Authenticated SMTP. In Exchange Online PowerShell:

Set-CASMailbox -Identity website@example.com -SmtpClientAuthenticationDisabled $false

Microsoft is also retiring password (Basic) sign-in for SMTP AUTH. Its current timeline disables it by default for existing tenants at the end of December 2026. Use a plugin that signs in with OAuth, or another sending method. Our SMTP AUTH retirement guide tracks the dates.

How do you configure WordPress to send through SMTP?

You configure WordPress for SMTP either with an SMTP plugin or with a short must-use plugin that hooks phpmailer_init. Both do the same thing: WordPress fires that hook before each send, and the developer reference shows how to switch PHPMailer to SMTP there.

Option 1: an SMTP plugin

Install a well-maintained SMTP plugin from the WordPress plugin directory and enter the host, port, encryption and credentials from the table. Prefer one that supports OAuth for Microsoft 365 and Google, keeps a log of each attempt, and lets you force the From address. Test with the plugin's own "send test email" button first.

Option 2: a must-use plugin, no settings screen

Must-use plugins live in wp-content/mu-plugins, are "automatically enabled on all sites in the installation" and "cannot be disabled from wp-admin" (WordPress documentation). That makes them a stable home for mail settings.

First, add your settings to wp-config.php, above the line that says to stop editing. This example uses a transactional service; swap in your own values:

define( 'SITE_SMTP_HOST', 'smtp.mailservice.example' );
define( 'SITE_SMTP_PORT', 587 );
define( 'SITE_SMTP_SECURE', 'tls' );   // 'tls' = STARTTLS on 587, 'ssl' = TLS on 465
define( 'SITE_SMTP_USER', 'smtp-username' );
define( 'SITE_SMTP_PASS', 'smtp-password-or-api-key' );
define( 'SITE_MAIL_FROM', 'website@example.com' );

Then create wp-content/mu-plugins/site-smtp.php:

<?php
/**
 * Plugin Name: Site SMTP
 * Description: Sends wp_mail() through authenticated SMTP using settings in wp-config.php.
 */
if ( ! defined( 'SITE_SMTP_HOST' ) ) {
    return;
}

add_action( 'phpmailer_init', function ( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host       = SITE_SMTP_HOST;
    $phpmailer->Port       = SITE_SMTP_PORT;
    $phpmailer->SMTPSecure = SITE_SMTP_SECURE;
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Username   = SITE_SMTP_USER;
    $phpmailer->Password   = SITE_SMTP_PASS;
    $phpmailer->Sender     = SITE_MAIL_FROM; // envelope sender (Return-Path)
} );

add_filter( 'wp_mail_from', function () {
    return SITE_MAIL_FROM;
} );

add_filter( 'wp_mail_from_name', function () {
    return 'Example Co website';
} );

add_action( 'wp_mail_failed', function ( $error ) {
    error_log( 'wp_mail failed: ' . $error->get_error_message() );
} );

What each part does:

  • isSMTP() and the connection settings replace PHP mail() with an authenticated SMTP session.
  • Sender sets the envelope sender. PHPMailer uses it as the SMTP MAIL FROM, which is the domain SPF checks. Some services replace it with their own bounce domain; that's normal, and DKIM then provides the alignment.
  • The wp_mail_from filter runs on every message, so forms that put the visitor's address in From are overridden. Make sure your form plugin puts the visitor in Reply-To instead.
  • wp_mail_failed is a core WordPress action that fires when sending fails. Here it writes the error to the PHP error log.

Keep wp-config.php out of version control and readable only by the web server user, since it now holds a mail password.

How do you set up SPF for WordPress email?

Add your sending service to your domain's one SPF record. RFC 7208 allows only one SPF record per name; two records cause a permanent error, so SPF fails for everything.

For example, a domain on Microsoft 365 that adds a transactional service changes this:

example.com.  TXT  "v=spf1 include:spf.protection.outlook.com -all"

to this, using the include your service documents:

example.com.  TXT  "v=spf1 include:spf.protection.outlook.com include:spf.mailservice.example -all"

If WordPress sends through Microsoft 365 or Google itself, your existing Microsoft 365 or Google Workspace include already covers it. If you send through your host's server, add the include or IP address your host gives you. Check the result, including the 10-lookup limit, with the SPF Checker.

How do you set up DKIM for WordPress email?

DKIM is set up at the sending service, not in WordPress: the service signs each message, and you publish its public key in DNS.

  • Microsoft 365: turn on DKIM for your custom domain in the Microsoft Defender portal and publish the two CNAME records. See DKIM setup for Microsoft 365 and Google Workspace.
  • Google Workspace: generate the key in the Admin console, publish the TXT record, then start authentication. The same guide covers it.
  • Transactional services: add your domain in the service's dashboard and publish the CNAME or TXT records it shows. Our DKIM provider pages list the record format for common services such as Amazon SES and SendGrid.
  • cPanel hosting: cPanel's Email Deliverability interface checks a domain's DKIM, SPF, DMARC and reverse DNS (PTR) records, and can repair invalid ones when the server controls the domain's DNS.

Verify the published key with the DKIM Checker. The DKIM d= domain must be your domain, or a subdomain of it, for DMARC to pass with relaxed alignment.

How do you test WordPress email after setup?

  1. Send a password reset to your outside test mailbox from the WordPress login page.
  2. Send a test to the Email Deliverability Test to see SPF, DKIM, DMARC, reverse DNS and spam results for one real message.
  3. Open the headers of the delivered copy in the Email Header Analyzer. You want spf=pass or dkim=pass for your domain, and dmarc=pass.
  4. Submit each form on the site and confirm the From is your address and Reply-To is the visitor.
  5. Check the error log for wp_mail failed lines, or your plugin's mail log.
  6. Publish DMARC if you haven't. Start at p=none with a reporting address, and use the DMARC Checker to confirm it.

What do common WordPress SMTP errors mean?

Error or symptom Likely cause Fix
535 5.7.8 authentication failed Wrong username or password, or the provider needs an app password or OAuth Re-enter credentials; create an app password or switch to OAuth
530 5.7.0 authentication required The connection didn't start TLS or sign in Set encryption to STARTTLS on 587 and enable authentication
5.7.60 Client doesn't have permissions to send as this sender (Microsoft 365) From address differs from the signed-in mailbox Use the mailbox's own address, or grant Send As
Connection timed out The host blocks outbound SMTP ports Ask your host which ports are open, or use the service's API
Delivered, but dmarc=fail SPF and DKIM pass only for the service's own domain Turn on custom-domain DKIM at the service
550 5.7.26 from Gmail Message failed authentication Fix SPF and DKIM for the From domain

For port choices, see SMTP ports 25, 587 and 465.

Next steps

  1. Pick one sending service, set one From address on your domain, and switch WordPress to SMTP with a plugin or the code above.
  2. Add the service to your SPF record, turn on DKIM for your domain, and publish DMARC.
  3. Run the free Email Deliverability Test after every change.
  4. If your site and other systems send business notifications, iBlockSpam's outbound relay works as a smart host with DKIM signing and outbound filtering, with 500 messages a month included on both plans.

Browse more guides on running email day to day.

Frequently asked questions

What SMTP port should WordPress use?

Use 587 with STARTTLS, or 465 with implicit TLS if your provider supports it. Microsoft 365's client submission uses smtp.office365.com on port 587 and says a device that defaults to 465 doesn't support its required TLS. Google's smtp.gmail.com accepts 465 or 587. Avoid port 25 from web hosts, which is often blocked.

Do I need an SMTP plugin to send WordPress email through SMTP?

No. WordPress fires the phpmailer_init hook before it sends each message, so a few lines in a must-use plugin can switch PHPMailer to SMTP with settings kept in wp-config.php. A plugin is easier if you want a settings screen, OAuth sign-in or a mail log. Either way, SPF and DKIM still have to be set up in DNS.

Why does WordPress email still fail DMARC after I set up SMTP?

Usually because nothing aligns with your From domain. The SMTP service may use its own bounce domain, which passes SPF for that domain but not yours, and sign DKIM with its own domain too. Turn on custom-domain DKIM at the service so the d= domain matches your From address, then check the headers for dmarc=pass.

Can WordPress send email through my Gmail or Google Workspace account?

Yes, through smtp.gmail.com on port 465 or 587, which Google limits to 2,000 messages a day. Google says that since May 1, 2025, Workspace accounts no longer support apps that sign in with just a username and password, so you need an app password, which requires 2-Step Verification, or a plugin that signs in with OAuth. Workspace admins can use the SMTP relay service instead.

What From address should WordPress use?

Use one address on your own domain, such as website@ or noreply@, and send every message from it. WordPress defaults to wordpress@ plus your site's domain, and some forms put the visitor's address in From, which fails DMARC for their domain. Put the visitor's address in Reply-To so you can still answer them with one click.

Sources

  1. WordPress Developer Resources: wp_mail()
  2. WordPress Developer Resources: phpmailer_init hook
  3. WordPress Advanced Administration: Must-use plugins
  4. Microsoft Learn: Set up a multifunction device or application to send email using Microsoft 365
  5. Microsoft Learn: Enable or disable SMTP AUTH in Exchange Online
  6. Google Workspace Admin Help: Send email from a printer, scanner, or app
  7. Gmail Help: Email sender guidelines
  8. RFC 7208: Sender Policy Framework (SPF)

See how your domain measures up

Run a free Email Health Check: MX, SPF, DKIM, DMARC and transport security in one scan, with the raw records shown. No sign-up.

Keep reading