I just want to setup SMTP for a specific instance of wp_mail().

I have tested plugins like "Easy WP SMTP" and also checked how to set up SMTP manually but all these apply to the entire site and then every email from the site is sent through the SMTP account.

I don't want to send any other emails like newsletters or comment approval emails through the same SMTP account.

  • Is it a plugin or custom code that will send that specific mail? – D. Dan Jun 26 '18 at 9:53
  • its from my custom plugin. – Unnikrishnan R Jun 26 '18 at 9:54
  • Then one option could be to look for tutorials how to send email through smtp and code it in the plugin, or see how those plugins do it that you wanted to use. – D. Dan Jun 26 '18 at 10:04
  • @D.Dan I already know how to setup smtp and when i do so it is effecting the entire site. What i have asked is a way to do it for a particular function and not to entire site. And to be honest to get an answer like "look for tutorials" then why you and me are here??? – Unnikrishnan R Jun 26 '18 at 10:18
  • That's why this isn't an Answer but a comment. What you did is change the wp_mail function with a plugin. But as you discovered that wasn't the solution because it effected the whole site, and every mail functionality of it. My suggestion was to don't use the wp_mail function in your plugin, instead mail it through smtp inside your plugin. – D. Dan Jun 26 '18 at 10:29

The following is a way to handle your question. It is in two parts. First, you would create your connection as constants to be used later. The best way to do this is in wp-config.php. (You mentioned that you're doing this in a custom plugin. If that's something that is to be portable, then you may want to change this to saving setting in the db instead.) Second, you'll apply a function hooked to the phpmailer that WP uses. In that function you can define your criteria in which you'd use the SMTP connection instead of the default.

You can set up your SMTP credentials and server connection info in wp-config.php as constants as follows:

 * Set the following constants in wp-config.php
 * These should be added somewhere BEFORE the
 * constant ABSPATH is defined.
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

Once you've added that to your wp-config.php file, you have constants that you can use to connect and send any email via SMTP. You can do this by hooking to the phpmailer_init action and using that to set up connection criteria using the constants you defined above.

In your specific case, you would want to add some conditional logic in the function to identify the condition in which you want to send via SMTP. Set up your conditional so that only your criteria uses the SMTP connection for the phpmailer, and all others would use whatever is already being used.

Since we don't know what that is from your OP, I've represented it here with a generic true === $some_criteria:

add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {

    if ( true === $some_criteria ) {
        if ( ! is_object( $phpmailer ) ) {
            $phpmailer = (object) $phpmailer;

        $phpmailer->Mailer     = 'smtp';
        $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;

    // any other case would not change the sending server

This concept is from the following gist on github: https://gist.github.com/butlerblog/c5c5eae5ace5bdaefb5d

General instructions on it here: http://b.utler.co/Y3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.