How to stop an email sending based on submitted values

Created by Phil Kurth, Modified on Fri, 2 Sep, 2022 at 8:33 AM by Phil Kurth

If you need to stop email notifications based on a submitted field value, you may use the af/form/email/recipient filter to override the recipient list based on the form's configuration args. A return value of FALSE will disable the notification email.


<?php

function conditionally_send_email( $recipient, $email, $form, $fields ) {
  // Check for correct form.
  if ( $form['key'] !== 'FORM_KEY' ) {
    return $recipient;
  }

  // If submitted field value doesn't match the expected value, return FALSE to stop email sending.
  if ( af_get_field( 'my_field_name' ) !== 'some value' ) {
    return FALSE;
  }

  return $recipient;
}

add_filter( 'af/form/email/recipient', 'conditionally_send_email', 10, 4 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article