Change Email Recipient Based On Subject in Contact Form 7

I have been using Contact Form 7 on a lot of my personal sites, as well as client sites for over a decade now and it’s easy to see why it’s the top plugin in the WordPress repository for ‘contact form’ with over 5 million active installs. It’s easy to customise and comes with a lot of hooks to make developers happy. One such hook I recently used was wpcf7_before_send_mail.

A client had a request to send a contact form’s email notifications to specific email addresses, depending on the ‘enquiry’ dropdown menu. For example – If the enquiry was for Accounts, the email would send the form data to accounts@example.com and so on.

Create a form in Contact Form 7

Start by creating your form and include a dropdown menu with a few options. I have create my enquiry-type dropdown menu with three options:

  1. General
  2. Media
  3. Jobs
<p class="form-field">
   <label> Your Name</label>
   [text* your-name]
</p>

<p class="form-field">
   <label>Your Email</label>
   [email* your-email]
</p>

<p class="form-field">
    <label>Enquiry Type</label>
   [select enquiry-type include_blank "General" "Media" "Jobs"]
</p>

<p class="form-field">
   [textarea* message]
</p>

[submit "Send"]

If we submitted this form at the moment, the form would be sent to the email address set in the Mail tab in CF7, regardless of which dropdown option is selected. Let’s change that.

Dynamically change recipient email

Let’s hook into wpcf7_before_send_mail, get the submitted form data, find the enquiry-type dropdown option and then use an if/else statement to send the notifications to specific email addresses.

// hook into wpcf7_before_send_mail
add_action( 'wpcf7_before_send_mail', 'cf7dynamicnotifications'); // Hooking into wpcf7_before_send_mail

function cf7dynamicnotifications($contact_form) // Create our function to be used in the above hook
{
   $submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class
   $posted_data = $submission->get_posted_data(); // Get all of the submitted form data

   /* 
   * Here are our 'enquiry-type' options and associated email address for reference
   * 
   1 General – general@frontendhero.dev
   2 Media - media@frontendhero.dev
   3 jobs - jobs@frontendhero.dev
   4 default - hello@frontendhero.dev
   */

   if( $posted_data["enquiry-type"] == 'General' ) { // If General option is selected
      $recipient_email = 'general@frontendhero.dev';
   }
   elseif($posted_data["enquiry-type"] == 'Media') { // else if Media option is selected
      $recipient_email = 'media@frontendhero.dev';
   }
   elseif($posted_data["enquiry-type"] == 'Jobs') { // else if Jobs option is selected
      $recipient_email = 'jobs@frontendhero.dev';
   }
   else { // If no dropdown option is selected
      $recipient_email = 'hello@frontendhero.dev';
   }
   // set the email address to recipient
   $mailProp = $contact_form->get_properties('mail');
   $mailProp['mail']['recipient'] = $recipient_email;

   // update the form properties
   $contact_form->set_properties(array('mail' => $mailProp['mail']));
}

And there you have it, with a few lines of code we can dynamically send email notifications to specific email addresses – Very professional.

If this tutorial has helped you, I wouldn't say no to a coffee as a tip ☕️

Buy Me a Coffee at ko-fi.com

9 thoughts on "Change Email Recipient Based On Subject in Contact Form 7"

  1. Hi, I’ve tried your script and somehow can’t get it working. Is the functions.php of my child theme the right place for the code or do I have to put it somewhere else?

    1. Hi Paul,

      Yep, functions.php is the correct place.

      Let me check over this shortly to see if anything has been updated in the CF7 plugin and get back to you.

  2. Hey Keith, thank you for the quick reply, much appreciated. Please let me know that you find.

    1. Hey Patrick,

      It appears that the plugin was updated, as the code is indeed broken. (I will revise shortly).

      In the meantime, here is a new solution to help you out.

      https://www.youtube.com/watch?v=CXFRzqS6Zms

      1. Hi Keith, did you find a solution?

        1. Hola Andrés,

          Sí – la solución es en YouTube.

          Necesito revisar este código después de las vacaciones.

  3. Did you revised the code? is it working now?

  4. I did copy and paste, just changing the names of my values and it worked! thanks a lot!!!

  5. Hello,

    I need to update the recipient based on a postal code. Did you revised the code? is it working now?

    thank you very much,

Leave a Reply

Your email address will not be published. Required fields are marked *