Skip to content

Instantly share code, notes, and snippets.

@butlerblog
Last active March 28, 2024 02:09
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save butlerblog/7e4dbafcbc61b15505ee8ca90510f1e7 to your computer and use it in GitHub Desktop.
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:
* 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
@mkdizajn
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

@mrminnkhantnaing
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 person.name@example.com',);

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

@joao-vasconcelos
Copy link

This code works everytime.

@conschneider
Copy link

Works for me. Thank you.

@juancts
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