← Tous les articles

WordPress emails not arriving? Set up proper SMTP without a plugin

Silent contact forms, password resets landing in spam: why WordPress emails go missing, and how to route them through SMTP cleanly with phpmailer_init.

WordPress emails not arriving? Set up proper SMTP without a plugin

Key points

  • Out of the box, WordPress sends email through the server’s mail() function, which many mailbox providers reject.
  • A few lines in a must-use plugin route everything through authenticated SMTP, with credentials kept in wp-config.php.
  • The sender address must live on your own domain, backed by SPF, DKIM and DMARC records.

“We’re not getting contact form enquiries anymore.” We usually hear this after a hosting move, sometimes weeks later, when someone realises a lead never got a reply. The form still says “Message sent”. The form plugin is rarely to blame: the real issue is how WordPress sends mail in the first place.

Why the emails disappear

wp_mail() is built on PHPMailer, which defaults to PHP’s mail(): your web server sends the message itself. That server is almost never declared as a legitimate sender for your domain, so Gmail or Outlook see mail from wordpress@your-domain.com coming from an unknown machine and quietly junk or drop it.

The other common case on our projects: the host blocks direct sending altogether. wp_mail() then returns false, but plenty of form plugins ignore that and show a success message anyway.

Route mail through SMTP with phpmailer_init

The fix is to send through an authenticated SMTP server, either your business mailbox or, better, a transactional email service. An SMTP plugin works, but we prefer a few lines of code: no settings screen to keep updated and no password sitting in plain text in wp_options. Credentials go in wp-config.php:

// wp-config.php (kept out of Git)
define( 'WTS_SMTP_HOST', 'smtp.your-provider.com' );
define( 'WTS_SMTP_PORT', 587 );
define( 'WTS_SMTP_USER', 'notifications@your-domain.com' );
define( 'WTS_SMTP_PASS', 'app-specific-password' );

Then a must-use plugin, which loads automatically and can’t be switched off by accident from the dashboard:

<?php
// wp-content/mu-plugins/smtp.php
use PHPMailerPHPMailerPHPMailer;

add_action( 'phpmailer_init', function ( PHPMailer $mailer ) {
    if ( ! defined( 'WTS_SMTP_HOST' ) ) {
        return; // not configured: leave WordPress alone
    }

    $mailer->isSMTP();
    $mailer->Host       = WTS_SMTP_HOST;
    $mailer->Port       = WTS_SMTP_PORT;
    $mailer->SMTPAuth   = true;
    $mailer->Username   = WTS_SMTP_USER;
    $mailer->Password   = WTS_SMTP_PASS;
    $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
} );

add_filter( 'wp_mail_from', fn () => 'notifications@your-domain.com' );
add_filter( 'wp_mail_from_name', fn () => 'Your Company' );

Checking for the constant lets the same code run on a local or staging copy without sending real emails. If your provider requires port 465, swap ENCRYPTION_STARTTLS for ENCRYPTION_SMTPS.

DNS does half the job

SMTP alone won’t help if your domain doesn’t vouch for the sender. Check three DNS records:

  • SPF: which servers may send for your domain, including your provider’s;
  • DKIM: the public key used to verify message signatures, supplied by the provider;
  • DMARC: what receivers should do when the other two fail. Start with p=none to monitor, then tighten it.

A trap we see regularly: a domain already has an SPF record for its mailbox, and someone adds a second one for the new service. There must be exactly one SPF record that includes both. And the address in wp_mail_from has to be on that same domain, not a Gmail address.

Log failures and test

WordPress fires wp_mail_failed whenever sending fails. Without a listener, an SMTP authentication error goes completely unnoticed:

add_action( 'wp_mail_failed', function ( WP_Error $error ) {
    $data = $error->get_error_data();
    error_log( sprintf(
        'wp_mail failed to %s: %s',
        implode( ', ', (array) ( $data['to'] ?? [] ) ),
        $error->get_error_message()
    ) );
} );

To test without filling in a form, use WP-CLI:

wp eval 'var_dump( wp_mail( "you@example.com", "SMTP test", "If you can read this, it works." ) );'

bool(true) means the SMTP server accepted the message, not that it reached the inbox. Check actual delivery and look at the received message’s headers: spf=pass and dkim=pass confirm your DNS is right.

FAQ

Do I still need an SMTP plugin?

No. A plugin mainly adds a settings screen and sometimes an email log. If a technical team looks after the site, the code above is easier to maintain and keep in version control.

The form says “sent” but nothing arrives. Where do I start?

Add the wp_mail_failed logger, send a WP-CLI test and read the PHP error log. If nothing shows up there, the message left your server, so look at DNS or the recipient’s spam filtering next.

Emails from your website going missing or landing in spam? We set up sending, DNS and monitoring so every enquiry actually reaches you.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →