Running SMTP Conditionally

I'm working on developing a support website and I'm using Gravity Forms to handle the support ticket form. I have the form notification configured so that the From shows my client's email address instead of the email address from the SMTP.

I found some code to execute from my functions.php file that will change the default wp_mail() if the From email doesn't match. Exactly what I want..:

add_filter('wp_mail_from', 'doEmailFilter');
add_filter('wp_mail_from_name', 'doEmailNameFilter');

function doEmailFilter($email_address){
if($email_address === [email protected])
    return '[email protected]';
else
    return $email_address;
}
function doEmailNameFilter($email_from){
if($email_from === WordPress)
    return 'Site Admin';
else
    return $email_from;
}

I also found code that will run SMTP from functions.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;
}

Update 6/6/21, Clarifies the question and updates the code: I tried to do this (thanks Paul G.):

add_filter('wp_mail_from', 'doEmailFilter');
function doEmailFilter($email_address){
if($email_address === [email protected])
    return '[email protected]';
else
    return $email_address;
}

$emailfilter = 'doEmailFilter'; 

add_action( 'phpmailer_init', function ( $phpmailer ) use ( $emailfilter ) { 

    if ( $emailfilter === '[email protected]' ) {

        $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;
    }
    else
        return $emailfilter;
    // any other case would not change the sending server
}
);

I have a form with two email notifications that get sent when the form is sent.

The first notification goes to my main inbox as a standard admin notification and uses the [email protected] as the From, which should use the SMTP. This notification represents any email that gets sent from the website that is intended for my customers.

The second goes to another inbox that I own, but it swaps out the From with what's in the form. This will NOT use the SMTP, and I want that. It's more of an internal email that gets sent to an inbox that I have access to. The inbox is only meant to pipe emails into another system.

Both emails send, but the email with [email protected] in the From does not go through the SMTP.

How can it be fixed so only emails with [email protected] in From gets sent through the SMTP?

Topic smtp wp-mail php Wordpress

Category Web

About

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