How to Send WordPress Emails via SMTP: A Complete Guide

WordPress uses email to send various notifications, such as user registration, password resets, comment alerts, and contact form submissions. It uses PHP’s mail() function to send emails by default. While this may work in some cases, it can result in emails being flagged as spam or not delivered. On the other hand, SMTP (Simple Mail Transfer Protocol) is a more reliable protocol used by most email services, offering authentication and encryption to ensure your emails are sent securely and reliably. If you are using a reliable email service provider like Gmail, GoDaddy, or others, you can easily configure WordPress to use their SMTP server for sending emails.

In this post, I will guide you through the steps to set up SMTP for WordPress email delivery. Rather than relying on a plugin, you can configure SMTP directly by adding custom code to your WordPress theme’s functions.php file and your site’s wp-config.php file. This method gives you more control over the setup and avoids the need for additional plugins.

Step 1: Add SMTP Settings to wp-config.php

First, we need to configure SMTP in the wp-config.php file. This file contains important settings for the WordPress installation, and adding your SMTP details here ensures they are securely stored. The wp-config.php file is located in the root directory of the WordPress installation. Open the wp-config.php file in a text editor and add the following SMTP configuration lines:

// SMTP email settings starts
define( 'SMTP_username', 'your-email@yourdomain.com' );  // Your email address
define( 'SMTP_password', 'your-email-password' );   // Your email password
define( 'SMTP_server', 'smtp.your-email-provider.com' );  // SMTP server address
define( 'SMTP_PORT', 587 ); // Port, typically 587 for TLS or 465 for SSL
define( 'SMTP_SECURE', 'tls' ); // Encryption method ('ssl' or 'tls')
define( 'SMTP_AUTH', true );  // Use SMTP authentication (true|false)
define( 'SMTP_FROM', 'your-email@yourdomain.com' ); // From email address
define( 'SMTP_NAME', 'Your Name or Website Name' ); // From name
define( 'SMTP_DEBUG', 0 );  // for debugging purposes only
// SMTP email settings ends

Make sure to replace the placeholders with your actual SMTP server details, username, password, etc.

Step 2: Define the PHP mailer function in functions.php

Now, we will add the necessary code to the functions.php file of the theme. This file is located under “/wp-content/themes/your-active-theme/” in the WordPress directory. Open the functions.php file in a text editor and add the following function to set up SMTP with the details you just defined in wp-config.php.

// to send email using SMTP instead of default wordpress mail - these variables need to be set in wp-config
add_action( 'phpmailer_init', 'my_phpmailer_smtp' );
function my_phpmailer_smtp( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host = SMTP_server;
    $phpmailer->SMTPAuth = SMTP_AUTH;
    $phpmailer->Port = SMTP_PORT;
    $phpmailer->Username = SMTP_username;
    $phpmailer->Password = SMTP_password;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From = SMTP_FROM;
    $phpmailer->FromName = SMTP_NAME;
}

This code ensures that WordPress uses SMTP for email delivery, using the settings you have defined in wp-config.php.

Step 3: Test Your SMTP Configuration

Once you have added the SMTP configuration to both wp-config.php and functions.php, you can send a test email by posting a comment to any of your WordPress posts.

Please let me know if you find any issues with the steps I provided.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.