How to Configure WordPress SMTP without a plugin?

WordPress world is full of plugins and to be true, I don’t like them much. I want everything to be hard coded because it makes my WordPress admin less dirty, less messy and doesn’t bloat my website. I once used a SMTP plugin and from the next day, I started receiving many spam comments which freaked me out and that lead me to a conclusion that I will use as less plugin as possible.

How to Configure WordPress SMTP without a plugin
How-to-Configure-WordPress-SMTP-without-a-plugin

Anyways, configuring SMTP on WordPress is very easy, and thus you don’t need a plugin for such a small thing. So, let’s see how we can configure SMTP on WordPress to send emails.

Three steps to configure SMTP on WordPress without plugin

Step 1 – Add code to functions.php file

The first step is to add the given below code to your theme’s functions.php file

// Secure SMTP configuration using constants from wp-config.php
function custom_secure_smtp($phpmailer) {
    if (defined('SMTP_HOST') && defined('SMTP_USER') && defined('SMTP_PASS')) {
        $phpmailer->isSMTP();
        $phpmailer->Host = SMTP_HOST;
        $phpmailer->SMTPAuth = true;
        $phpmailer->Port = defined('SMTP_PORT') ? SMTP_PORT : 587;
        $phpmailer->Username = SMTP_USER;
        $phpmailer->Password = SMTP_PASS;
        $phpmailer->SMTPSecure = defined('SMTP_SECURE') ? SMTP_SECURE : 'tls';
        $phpmailer->From = SMTP_USER;
        $phpmailer->FromName = get_bloginfo('name');
    }
}
add_action('phpmailer_init', 'custom_secure_smtp');

Step 2 – Get an app password for Gmail

Since, we are using Gmail, we need to generate an app password because using your standard password won’t work. Here is how you can do it.

  1. Login to https://myaccount.google.com/
  2. Tap on “Security
  3. Enable 2-Step verification because on then you can generate a passkey
  4. Scroll to “App passwords
  5. Enter “app name” (can be anything) and click on “Create
  6. Copy the key shown on the pop-up and paste it in notepad or any text editor as we need to use it in later steps

Step 3 – Add code to wp-config.php

Third and final step is to copy and paste the below code to your wp-config.php file. Make sure to make changes to the below code as per requirement.

// SMTP Configuration
define('SMTP_HOST', 'smtp.your-provider.com');
define('SMTP_USER', '[email protected]');
define('SMTP_PASS', 'your-email-password'); (if using gmail, use the app password that you have generated above)
define('SMTP_PORT', 587);
define('SMTP_SECURE', 'tls');

Save the file and it’s done. SMTP for your WordPress is configured.

Sourabh Verma

Hi, welcome to SwiftGuides! I have over 12 years of experience in Linux, SysAdmin, databases, cloud computing, and other IT-related areas. I publish easy-to-follow, 100% working tutorials. I hope you love my work. For any queries, feel free to drop your questions in the comments. Thanks!

Leave a Comment