-
-
Save butlerblog/7e4dbafcbc61b15505ee8ca90510f1e7 to your computer and use it in GitHub Desktop.
<?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: | |
* https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/ | |
* | |
* Values for constants are set in wp-config.php | |
*/ | |
add_action( 'phpmailer_init', 'send_smtp_email' ); | |
function send_smtp_email( $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; | |
} |
<?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: | |
* https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/ | |
*/ | |
define( 'SMTP_USER', 'user@example.com' ); // Username to use for SMTP authentication | |
define( 'SMTP_PASS', 'smtp password' ); // Password to use for SMTP authentication | |
define( 'SMTP_HOST', 'smtp.example.com' ); // The hostname of the mail server | |
define( 'SMTP_FROM', 'website@example.com' ); // 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 will work sometimes, and sometimes will not - confirmed
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
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 person.name@example.com',);
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.
This code works everytime.
Works for me. Thank you.
It works. Thanks for sharing!. Juan.
Works perfectly. Thank You!
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? Iswp_mail()
returning true in this instance? You have to know for certain thatwp_mail()
is actually successfully triggering and handing off a legitimate email without errors toPHPMailer
. 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 beforephpmailer_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).
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.