php – 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 need it to do this because I have another plugin that pipes emails sent to a specific inbox. That plugin handles the actual help desk. I’m explaining this part to give some background into what I’m trying to accomplish.

Any SMTP plugin that I use will force all emails coming from the website to use the “From” email set up within the plugin. Gravity Forms won’t override the “From” email notification. I want SMTP to work all the time EXCEPCT for when an email is getting sent out with a different “From”.

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 === "")
    return '';
    return $email_address;
function doEmailNameFilter($email_from){
if($email_from === "WordPress")
    return 'Site Admin';
    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->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;

How would I go about combining these two pieces of code so that the SMTP would run and process the the desired “From” email ONLY if the “From” email doesn’t match? Is such a thing possible? Thank you!

Brandon 5 months 2021-06-04T17:11:49-05:00 0 Answers 0 views 0

Leave an answer