Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SMTP using wp-config.php for settings #smtp #wp_mail
<?php // Don't use this line.
* 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 are constants 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.
* 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

This comment has been minimized.

Copy link

@couponrim couponrim commented Jan 31, 2019

This code sometime works


This comment has been minimized.

Copy link

@josuevalrob josuevalrob commented Feb 12, 2019

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


This comment has been minimized.

Copy link

@severendis severendis commented Oct 23, 2019

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


This comment has been minimized.

Copy link

@DDC-Devs DDC-Devs commented Jul 20, 2020

confirmed, this sometimes works.


This comment has been minimized.

Copy link

@moomean moomean commented Oct 27, 2020

This code DOESN'T work!


This comment has been minimized.

Copy link
Owner Author

@butlerblog 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.