Skip to content

Instantly share code, notes, and snippets.

Last active February 6, 2024 15:24
  • Star 48 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save butlerblog/7e4dbafcbc61b15505ee8ca90510f1e7 to your computer and use it in GitHub Desktop.
SMTP using wp-config.php for settings #smtp #wp_mail
<?php // Don't use this line.
* Add the script below to wherever you store custom code snippets
* in your site, whether that's your child theme's functions.php,
* a custom plugin file, or through a code snippet plugin.
* This function will connect wp_mail to your authenticated
* SMTP server. This improves reliability of wp_mail, and
* avoids many potential problems.
* For instructions on the use of this script, see:
* Values for constants are set in wp-config.php
add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
$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;
<?php // Don't use this line.
* Set the following constants in wp-config.php
* These should be added somewhere BEFORE the
* constant ABSPATH is defined.
* Make sure you know from your email host what your specific connection
* parameters are. Common defaults are given below, but if they are not
* what your host requires, you won't be able to connect. Each constant
* is noted with a comment identifying what connection parameter it is,
* but make sure you're using the correct value - check with your
* email host to confirm specific requirements. (Also confirm that your
* host allows remote connections in the first place.)
* For instructions on the use of this script, see:
define( 'SMTP_USER', '' ); // Username to use for SMTP authentication
define( 'SMTP_PASS', 'smtp password' ); // Password to use for SMTP authentication
define( 'SMTP_HOST', '' ); // The hostname of the mail server
define( 'SMTP_FROM', '' ); // SMTP From email address
define( 'SMTP_NAME', 'e.g Website Name' ); // SMTP From name
define( 'SMTP_PORT', '25' ); // SMTP port number - likely to be 25, 465 or 587
define( 'SMTP_SECURE', 'tls' ); // Encryption system to use - ssl or tls
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false)
define( 'SMTP_DEBUG', 0 ); // for debugging purposes only set to 1 or 2
Copy link

This code sometime works

Copy link

Sometimes works? what that does mean?? how can I say to my client: Sometimes the web will send emails?... SOMETIMES!!!

Copy link

The code works! What must the extension be if I want to use a reply address "addReplyTo"?

Copy link

confirmed, this sometimes works.

Copy link

moomean commented Oct 27, 2020

This code DOESN'T work!

Copy link

butlerblog commented Oct 27, 2020

For the people that have commented that this "works sometimes" or that it doesn't work at all... if your site's email is not working, the problem is not the code snippet.

Implementing this is actually quite simple and requires little to no testing IF you actually know the requirements of your email server. So asking your email provider ahead of time for information will save you a lot of headache.

First, make sure you know the issue isn't somewhere upstream. Is the process by which wp_mail() is being triggered actually valid? Is wp_mail() returning true in this instance? You have to know for certain that wp_mail() is actually successfully triggering and handing off a legitimate email without errors to PHPMailer. If you don't know that with 100% certainty, then how can you start to test beyond that?

Next, have you implemented this code correctly? Placement is critical as is understanding "order of operations" in WordPress. If those things don't make sense, you need to make sure you following the listed instructions carefully. Defining the constants MUST go in wp-config.php BEFORE ABSPATH is defined. The action hook MUST be in a location where it can be (and is) loaded before phpmailer_init is triggered. Most often, your theme functions.php or a custom plugin file will suffice here, but there are so many possibilities in WordPress these days, it is important to KNOW your setup so you can determine where it needs to go. Also understand that the more complex your setup, the more difficult it is to test and debug things. Maybe start by working with a basic install so you can eliminate possibilities from a complicated install. Test with basic WordPress, no plugins, and the default theme. If you WON'T do that, then you can't confidently say your problem isn't being introduced from a plugin or theme, or simply because you've set up something incorrectly.

If you know for certain that email is being generated and handed off to the next step, then check the following, some of which requires asking your email service provider what the required settings are for a remote connection (i.e. if you're "guessing" at the values without knowing for certain, then you're just going to beat your head against the wall).

  • Make sure the provider of the email account allows remote connections. If they don't, then you can't connect this way. (I can't believe I have to say that, but I have actually come across that as the issue more than once when doing consulting.)
  • Make sure your credentials are valid AND in the required format of your username (i.e. whether your email system requires "myemail" or "" for a remote connection).
  • Make sure the port number is correct AND that the server allows connections through that port. For example, if your email server uses port 465 and you pasted the code snippet without changing where the port is 25, then the problem is you're trying to connect through the wrong port. Also, there is no rule that the port has to be 25, 465 or 587. ASK your provider what the CORRECT port number is for a remote connection.
  • Make sure you're using the proper encryption scheme - tls or ssl AND if used, that the server has been properly configured. Again, ASK your provider for the proper information.
  • Make sure you know if authentication is required.

Not every mail server is configured the same way. If you're beating your head against the wall to make email work, start by checking with your host's support to find out if you're even using the right parameters for your connection script. And instead of simply copy/pasting code you don't understand, try reading the instructions provided and make sure you know how to determine if the issue is upstream or downstream and where you should be looking for the solution.

Copy link

mkdizajn commented Feb 9, 2021

This will work sometimes, and sometimes will not - confirmed

Copy link

mkdizajn commented Feb 9, 2021

but again I strongly agree with all what you said @butlerblog in previous long comment .. so this is something that I have to debug for my client now - to see where it leaks

Copy link

The code works! What must the extension be if I want to use a reply address "addReplyTo"?

You can simply add this line of code to your wp_mail function for the option of 'Reply to'.
$headers = array('Reply-To: Person Name',);

Copy link

madalinignisca commented Feb 8, 2022

This code always works as long as you use a good smtp service (either yourself managed or some paid 3rd party solution).

This code sometimes works when the smtp is on some shared hosting account (I am really refering to all those s****y Godaddy, Hostgator and all of them). This is not the issue of WordPress or this code snippet. Shared hosting email services are simply a might work thing.

If it's critical for you the email is delivered, than signup to a professional email sending service.

PS: Any service will not work well for SPAM purposes.

PS2: Using this with Oracle Cloud and Sendgrid. 0 email failed on both. I'd say zero failure should be on AWS ses, Mailgun and many others.

Copy link

This code works everytime.

Copy link

Works for me. Thank you.

Copy link

juancts commented May 19, 2023

It works. Thanks for sharing!. Juan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment