WordPress – Use SMTP With And Without Plugins

Author:

Published:

Updated on August 14, 2024

Reading Time: 2 min

Introduction #

When running a WordPress site, ensuring the reliability of your email delivery is crucial. Emails from contact forms, order confirmations, and password resets are pivotal for maintaining effective communication with your users. While WordPress has built-in functionality for sending emails, using Simple Mail Transfer Protocol (SMTP) can significantly improve security and delivery rates.

What is SMTP? #

SMTP stands for Simple Mail Transfer Protocol, the standard protocol used for sending emails across the internet. Most email systems utilize SMTP for sending messages, which are then retrieved by protocols like IMAP or POP3.

Why Use SMTP with WordPress? #

By default, WordPress uses the PHP Mail function (wp_mail) to send emails. However, PHP Mail lacks authentication, which means emails can often end up in the spam folder or be blocked entirely because they can be used to spoof email addresses.

In contrast, SMTP requires authentication (username and password) before sending emails, which significantly reduces the chances of your emails being marked as spam.

How to Implement SMTP in WordPress #

Setting up SMTP in WordPress can be done with or without plugins. Below are the methods to integrate SMTP for better email delivery:

Using a Plugin #

Plugins can simplify the SMTP setup. Two popular SMTP plugins for WordPress are:

  1. WP Mail SMTP
    • This plugin offers a setup wizard that guides you through configuring SMTP.
    • Upon installation, choose Let’s Get Started.
    • Select Other SMTP for services like StackCP or your specific mail host for services like Google, Office365, etc.
    • Enter your SMTP details such as hostname, encryption type (SSL or TLS), username, and password.
    • Set the “From Name” and “From Email” to match the SMTP username.
  2. Easy WP SMTP
    • This plugin focuses on simplicity and ease of setup.
    • Once installed, navigate to its settings in the WP Admin area.
    • Fill in the SMTP Host, Encryption type, SMTP Port, and enable SMTP Authentication.
    • Enter the SMTP Username (your email address) and Password.
    • You can also configure the “From Name” and “Reply-To” settings.

Without a Plugin #

For those comfortable with coding, SMTP settings can be directly integrated into WordPress:

Modify wp-config.php

Add constants to define your SMTP settings such as SMTP_USER, SMTP_PASS, SMTP_HOST, SMTP_PORT, SMTP_SECURE, and SMTP_AUTH.

// Configure your site to use SMTP

define( 'SMTP_USER',   'email@example.com' );
// Username to use for SMTP authentication

define( 'SMTP_PASS',   'MAIL_PASSWORD' );
// Password to use for SMTP authentication

define( 'SMTP_HOST',   'smtp.stackmail.com' );
// The hostname of the mail server

define( 'SMTP_FROM',   'email@example.com' ); 
// SMTP From email address

define( 'SMTP_NAME',   'FROM_NAME' );
// SMTP From name

define( 'SMTP_PORT',   '587' );
// SMTP port number - likely to be 25, 465 or 587

define( 'SMTP_SECURE', 'tls' );
// Encryption system to use - ssl or tls

define( 'SMTP_AUTH',    true ); 
// Use SMTP authentication (true|false)

define( 'SMTP_DEBUG',   0 ); 
// for debugging purposes only set to 1 or 2

Update functions.php

Add a function to initialize the PHPMailer with your SMTP settings.

Use the phpmailer_init action hook to assign your SMTP settings to the PHPMailer object.

 // Configures SMTP authentication for your site

    add_action( 'phpmailer_init', 'send_smtp_email' );

    function send_smtp_email( $phpmailer ) {

        $phpmailer->isSMTP();

        $phpmailer->Host       = SMTP_HOST;

        $phpmailer->SMTPAuth   = SMTP_AUTH;

        $phpmailer->Port       = SMTP_PORT;

        $phpmailer->Username   = SMTP_USER;

        $phpmailer->Password   = SMTP_PASS;

        $phpmailer->SMTPSecure = SMTP_SECURE;

        $phpmailer->From       = SMTP_FROM;

        $phpmailer->FromName   = SMTP_NAME;

    }

SMTP Settings Example #

For a StackCP hosted service, use:

  • Hostname: smtp.stackmail.com
  • Port: 465 (SSL) or 587 (TLS)
  • Security: SSL/TLS
  • Username and Password: Your email credentials

For Office365:

  • Hostname: smtp.office365.com
  • Port: 587
  • Security: STARTTLS
  • Username and Password: Your Office365 email credentials

Conclusion #

Implementing SMTP in WordPress ensures that your emails are not only sent securely but are also more likely to be delivered to your recipients’ inboxes. Whether through a plugin for ease of use or via coding for more control, setting up SMTP is an essential step for any professional WordPress site.