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.

Table of Contents
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
function custom_smtp_mailer($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = true;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = 'tls'; // or 'ssl'
$phpmailer->Port = SMTP_PORT;
$phpmailer->From = SMTP_FROM_EMAIL;
$phpmailer->FromName = SMTP_FROM_NAME;
}
add_action('phpmailer_init', 'custom_smtp_mailer');
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.
- Login to https://myaccount.google.com/
- Tap on “Security”
- Enable 2-Step verification because on then you can generate a passkey
- Scroll to “App passwords”
- Enter “app name” (can be anything) and click on “Create”
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 email settings
define('SMTP_HOST', 'smtp.gmail.com'); // Gmail SMTP server address
define('SMTP_PORT', 587); // Port for TLS, use 465 for SSL
define('SMTP_USER', '[email protected]'); // Your Gmail email address
define('SMTP_PASS', ‘App password'); // Your Gmail app password
define('SMTP_FROM_EMAIL', '[email protected]'); // The "From" email address
define('SMTP_FROM_NAME', 'Your Name'); // The "From" name that will appear in the email
Save the file and it’s done. SMTP for your WordPress is configured.
Leave a Reply