0

I have a multi-domain wordpress site, set up so that https://example1.com and https://example2.com both work and show the same content (but with the address bar showing whichever domain was used). I would like admin mails coming from the site to use different SMTP settings depending on the domain (I have completely separate mail accounts in each of the domains that I would like to use to send the respective domain's mail). what is the best way to set something like this up so that it works site-wide depending on the domain?

I can easily setup any of the various SMTP plugins that work with ONE domain...how could I make it work with different settings for EACH?

NOTE: this is not a multisite install. This is ONE site that serves the same content for either domain entered.

1 Answer 1

1

I believe what you need is the phpmailer_init action hook.

Here is an something you can do.

add_action( 'phpmailer_init', 'custom_smtp_settings' );

function custom_smtp_settings( $phpmailer ) {

    $phpmailer->isSMTP();

    switch ($_SERVER['HTTP_HOST']) {

        case 'example1.com': //name only, no http
            $phpmailer->Host = 'smtp.example1.com';
            $phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
            $phpmailer->Port = 25;
            $phpmailer->Username = 'example1';
            $phpmailer->Password = 'example1pass';

            // Additional settings…
            $phpmailer->SMTPSecure = "tls"; // Choose SSL or TLS, if necessary for your server
            $phpmailer->From = "[email protected]";
            $phpmailer->FromName = "Example One";
            break;

        case 'example2.com':
            $phpmailer->Host = 'smtp.example2.com';
            $phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
            $phpmailer->Port = 25;
            $phpmailer->Username = 'example2';
            $phpmailer->Password = 'example2pass';

            // Additional settings…
            $phpmailer->SMTPSecure = "tls"; // Choose SSL or TLS, if necessary for your server
            $phpmailer->From = "[email protected]";
            $phpmailer->FromName = "Example Two";
            break;
    }

}
5
  • Thanks, this looks great, but when I try it I get this error and no sending: stream_socket_enable_crypto(): SSL operation failed with code 1
    – Stephen
    Commented Sep 26, 2017 at 9:47
  • and this is super weird, because my cert is valid and my website serves securely
    – Stephen
    Commented Sep 26, 2017 at 12:19
  • I'm not sure why, can you try without SSL to see if that works. That is use $phpmailer->SMTPsecure = '' Commented Sep 26, 2017 at 13:12
  • I can't actually, my mail server requires a secure connection. But the error is on the wordpress server, not my mail server...
    – Stephen
    Commented Sep 26, 2017 at 13:35
  • ugh I figured out what was causing the error! $_SERVER['HTTP_HOST'] does NOT include the http(s). Leaving that off got it working! I credited you with the solution, but you might want to edit that above. Thanks again.
    – Stephen
    Commented Sep 26, 2017 at 15:06

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.