0

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?

4
  • 1
    Usual warning: if you're spoofing From addresses from someone else's domain you'll need to make sure your SMTP route is permitted in their SPF configuration, and that you have DKIM set up for their domain too if they're using that. In general it's easier to send from your own address not your clients'.
    – Rup
    Commented Jun 5, 2021 at 15:06
  • Thanks, @rup for your warning. The email that is being spoofed is going into an inbox that I own. I'm not sending it anywhere else. Notifications that I need to go to my subscribers inboxes I intend to have go through a SMTP because I don't want it to be considered spam.
    – Brandon
    Commented Jun 5, 2021 at 18:37
  • 1
    so you're getting errors because the variable $emailfilter is declared in another scope to the function you've hooked into phpmailer_init. We can solve that by changing how you setup the hook: add_action( 'phpmailer_init', function ( $phpmailer ) use ( $emailfilter ) { your existing code here } );
    – Paul G.
    Commented Jun 6, 2021 at 8:48
  • @PaulG. Thank you for your answer. With some tweaking, I was able to remove the errors with the code you provided. I updated my question above. How do I get it to only use the SMTP if the email is coming From "[email protected]"?
    – Brandon
    Commented Jun 6, 2021 at 13:15

0

Your Answer

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

Browse other questions tagged or ask your own question.