Search code examples
wordpresscontact-form-7

Sent different email templates based on dropdown selections in CF7


I'm trying to make a form that asks for user data first and then sends an email with a link to download our catalog. The particularity of the case is that the customer have to select his business type from a dropdown and the email template should be different for each one since the catalogs are different. Can this be achieved with a CF7 form?


Solution

  • There are 3 ways to achieve this,

    1. If you are not much of a programmer, then create 1 form for each business type, so you have 1 email template for each type. You can then proceed to filter the business type prior to the form display, by actually having all the different forms on the same page and only showing the one that corresponds to a dropdown selection at the top of the page. At page load only the dropdown is shown and all forms are hidden, and when a dropdown value changes (using js) you show the appropriate form dynamically. The downside of this solution is that you have to maintain multiple forms, but makes it simpler to understand.
    2. You can programmatically achieve this with the general mail tag filter functionality offered on a plugin extension such as the Smart Grid-layout. The plugin creates a general mail_tag filter such as [cf7sg-form-<your-form-name>] which you can insert into the body of your mail, and it allows you to programmatically compose your mail body message using all the submitted fields of your form, so you can check the value of the business type and compose the mail accordingly, (see this example tutorial for more examples)
    add_filter( 'cf7sg_mailtag_cf7sg-my-custom-form', 'filter_cf7_mailtag_cf7sg_form_my_custom_form', 10, 3);
    function filter_cf7_mailtag_cf7sg_form_my_custom_form($tag_replace, $submitted, $cf7_key){
      /*the $tag_replace string to change*/
      /*the $submitted an array containing all submitted fields*/
      /*the $cf7_key is a unique string key to identify your form, which you can find in your form table in the dashboard.*/
      if('my-custom-form'==$cf7_key ){
      switch($submitted['business-type']){
        case 'type1':
          $body = ... //the message body for type1 businesses.
          break;
        case 'type2':
          $body = ... //the message body for typee businesses.
          break;
        default:
          $body = "Unknown business".
          break;
      }
      return $body;
    }
    
    1. For more flexibility, you can also achieve this programmatically with the CF7 plugin's mail composition hook wpcf7_mail_components which allows you to filter all the components used by the plugin to create and send the notification mail,
    add_filter('wpcf7_mail_components', 'filter_cf7_mail_components',10,3);
    function filter_cf7_mail_components($components, $form_obj, $wpcf7_mail_object){
      $components['subject'];
      $components['sender'];
      $components['body']; //this is the one you want to change.
      $components['additional_headers'];
      $components['attachments'];
      
      return $components;
    }
    

    There are quite a few questions/answers on the CF7 wpcf7_mail_components filter on this forum.