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.

You can reply to this post by tweeting to me