How To Send SMTP Email from WordPress Without a Plugin

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

Rather than installing yet another plugin on your website (the popular on being WP Mail SMTP), you can configure your email settings directly in WordPress. This will reduced the overhead that you get when you install additional plugins, and help you understand how WordPress email actually works behind the scenes.

There are two files that you must edit:

  1. wp-config.php: This file exists in the root of your WordPress installation. You will need FTP or SSH access to modify it.
  2. functions.php: This file is accessible thorough your WordPress admin dashboard under Appearance > Theme Editor.

Let’s start with the wp-config.php file first.

Define Email Setting in wp-config.php

Add the following PHP variables to your wp-config.php file. You will want to update the values for each variable to reflect your own email credential and SMTP settings.

// SMTP email settings
define( 'SMTP_USER', 'youremail@example.com' );
define( 'SMTP_PASS', 'yourpassword' );
define( 'SMTP_HOST', 'smtp.gmail.com' );
define( 'SMTP_FROM', 'youremail@example.guru' );
define( 'SMTP_NAME', 'Tony Florida' );
define( 'SMTP_PORT', '587' );
define( 'SMTP_SECURE', 'tls' );
define( 'SMTP_AUTH', true );

When finished, save the file.

Override the phpmailer_init function in functions.php

While you can technically edit your theme’s functions.php file directly, I recommend you edit your child’s theme functions.php file. Don’t have a child theme or aren’t sure? Check out this blog post on WordPress child themes to help you wrap your head around child themes in WordPress.

Add the following function definition to your functions.php file.

// Send email via SMTP
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $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;
}

This block of code creates a function called my_phpmailer_example which overrides the functionality of the phpmailer_init function. It uses the PHP variables that we defined in our wp-config file. This way, when another plugin on your website (like Contact Form 7) tries to send an email, your SMTP settings will be used rather than the defaults.

Save the file and test it out! Try to send an email from your contact form or whatever email functionality you have on your website. It should work, but if not, let me know in the comments below and I’ll do my best to try and help you out.


About The Author

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment