How To Send SMTP Email from WordPress Without a Plugin

Send SMTP email from WordPress without a plugin

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.com' );
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.

Facebook
Twitter
Pinterest
LinkedIn
Reddit

Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

10 Responses

  1. Hi Tony,

    thanks for the tutorial. Seems great, but I’m having a Gmail alert about preventing a login from unknown device.

    I tried to use zoho and didn’t work either.

    Can you explain how to correct these issues? Thanks.

  2. Hi Tony,

    thanks for the tutorial. i have this message when i’m saving child theme config
    Unable to contact the site again to check for critical errors, so changes were rolled back in PHP. You will need to upload your PHP file changes by some other means, such as using SFTP.

  3. Hi Tony,

    thanks for the tutorial. i have this message
    Unable to contact the site again to check for critical errors, so changes were rolled back in PHP. You will need to upload your PHP file changes by some other means, such as using SFTP.

  4. Great tutorial Tony. Everything is working fine. I have copied the code, added it in the correct files and changed the details and the mail came in within seconds!
    Now I wanted to go a step further and create an admin page with fields where the values can be added (stored in db wp-options table). I want to load the values within the PHP Variables / Constants i.e.:
    define( ‘SMTP_USER’, get_option( ‘smtp_user’ ) );

    But as soon as I try this, the form is not sending anymore. It gets stuck at processing (using Ninja Forms) and results in an error which is logged in the console. When I revert back to add the values manually, everything is working fine. Do you perhaps have any idea what could be going wrong here?

    I really hope you can help me out here.
    Thanks in advance.

Leave a Reply

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