Setting up 2 SMTP accounts: 1 for wordpress and 1 for woocommerce

I wish to set up 2 smtp accounts on my wordpress site:

1 for woocommerce orders 1 for all other forms

i have the following code, but i am having trouble filtering the woocommmerce order. I am missing somthing. Here is the code:

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

    if ( true === 'WC_Email' ) 
    {
       $phpmailer-Host       = SMTP_WC_HOST;
        $phpmailer-SMTPAuth   = SMTP_WC_AUTH;
        $phpmailer-Port       = SMTP_WC_PORT;
        $phpmailer-Username   = SMTP_WC_USER;
        $phpmailer-Password   = SMTP_WC_PASS;
        $phpmailer-SMTPSecure = SMTP_WC_SECURE;
        $phpmailer-From       = SMTP_WC_FROM;
        $phpmailer-FromName   = SMTP_WC_NAME;
    }

    else{
        $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;
    }
}   

The results, both in woocommerce and regular forms result in the 'else' ; therefore, i'm not filtering the woocommerce orders correctly.

Topic smtp woocommerce-offtopic php email hooks Wordpress

Category Web


Your premise is correct - you can connect to different STMP accounts based on whatever criteria you can build into your action function.

It's a little tricky with WooCommerce because it has two email objects that it uses, one for creating emails and the other for sending. Also, hooking into the WC send process requires some serious grokking of WC to figure out where you need to hook your phpmailer_init action.

A simple method to figure out whether the email being sent is a WC email or not is to setup WC with a different "from" address in the settings (this address won't matter anyway, since you're sending via an SMTP server and you're re-setting the "from" when you setup your SMTP settings). Go to WooCommerce > Settings > Emails and set a unique "from" address that is different from your WP settings. You can then use that in your logic to determine whether the email is being generated via WC or something else.

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

    // Assuming the WC email is set to a different address than all others (i.e. WP General Settings email).
    $woo_from = get_option( 'woocommerce_email_from_address' );

    // If phpMailer is initialized with the WooCommerce from address...
    if ( $phpmailer->From == $woo_from ) {

        // This is a WooCommerce email.

        $phpmailer->Host       = SMTP_WC_HOST;
        $phpmailer->SMTPAuth   = SMTP_WC_AUTH;
        $phpmailer->Port       = SMTP_WC_PORT;
        $phpmailer->Username   = SMTP_WC_USER;
        $phpmailer->Password   = SMTP_WC_PASS;
        $phpmailer->SMTPSecure = SMTP_WC_SECURE;
        $phpmailer->From       = SMTP_WC_FROM;
        $phpmailer->FromName   = SMTP_WC_NAME;

    } else {

        // It's NOT a WooCommerce email.

        $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;

    }
}   

UPDATE: Just re-read your question, so this may be a little off. You said you wanted one for WC orders and one for all other forms. What I have above is for any WC email vs any non-WC email. Depending on what you mean by "other forms" (since there are other WC forms and emails), this may not be exactly what you need. If that's the case, mention it in the comments and I'll edit accordingly. It may be that you just need to check the subject line to see if it's an order.


Right now, it is not possible to add multiple SMTP addresses. However, if you still need to send emails from different accounts that are within the same domain, then you can use domain-based mailers like Sendgrid, Sendinblue, or Mailgun. These mailers usually have the requirement of first verifying the domain instead of authenticating the mailbox account. In that case, you can use it to send emails from the email address of your own domain.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.