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
    Commented Jun 26, 2018 at 9:53
  • its from my custom plugin. Commented Jun 26, 2018 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
    Commented Jun 26, 2018 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??? Commented Jun 26, 2018 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
    Commented Jun 26, 2018 at 10:29

2 Answers 2


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',   '[email protected]' );    // 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',   '[email protected]' ); // 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


As I cant yet comment but would like to thank @butlerblog for pointing me in the right direction and because I am really frustrated that that this is not implemented in none of the bigger SMTP plugins, like WP Mail SMTP or Post SMTP Mailer or whatever, I created an account here to post my version of the solution.

My use case seems so standard that I am really baffled that this is not build in into wordpress or that there is no plugin for it out there.

My problem is that I have the normal admin email for system notifications, like new comments, or new user registration or things like that but I wanted a different email to contact the users, in my case after ordering something (via woocommerce). (Order confirmation, invoice, shipping confirmed, ...)

The problem is, that my email server does not allow sending emails in the name of other users. So I needed a way to store two different user names and passwords for the smtp configuration. But all the aforementioned plugins permit only ONE single email to be configured via SMTP (at least when using a custom SMTP server, I did not test anything like sendrid or mailgun). I am using Namecheap Privateemail...

So I am now using the WP Mail SMTP plugin and I configured it for the admin email, lets say [email protected]. After testing that the config works, via the plugins testing functionality I added the following code to functions.php and configured woocommerce to send emails via [email protected]

// Use 20 as priority so that this action is called after the wp mail smtp plugin action
add_action( 'phpmailer_init', 'send_smtp_email', 20 );
function send_smtp_email( $phpmailer ) {
    if ( $phpmailer->From === '[email protected]' ) {
        $phpmailer->Username = '[email protected]';
        $phpmailer->Password = 'your_password_here';
    // Debugging (send complete $phpmailer object to your email):
    // $phpmailer->Body = 'phpmailer: '.print_r($phpmailer, true);

The password could also be saved in wp-config.php as mentioned in the other response and that way no other admin would have access. (I assume)

Note: I am no wordpress dev, so there might be problems with this approach with I am not aware of (yet).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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