Skip to content
Advertisement

Dynamic Email Recipient based on ZIP Code Input for Contact Form 7

We have 4 Offices with many assigned ZIP Codes:

  • 21079, 22085,…. = Hamburg
  • 10115, 10178,…. = Berlin
  • 60306, 60312,…. = Frankfurt
  • …..,……,…. = Munich
  • everything else = Head Office

Is it possible to read out the ZIP Code Input from the Form and send the Mail to the assigned Office dynamically?

It has to be dynamically without a dropdown (on Front End) of all ZIP Codes or Cities.

I tried this without success

// 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


   if( $posted_data["plz"] == '21079' ) { 
      $recipient_email = 'office1@xyz.com';
   }
   elseif($posted_data["plz"] == '22085') { 
      $recipient_email = 'office2@xyz.com';
   }
   elseif($posted_data["plz"] == '12345') { 
      $recipient_email = 'office3@xyz.com';
   }
   else { 
      $recipient_email = 'head-office@xyz.com';
   }
   // 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']));
}

EDIT

Thank you for your help. My solution works well, I had problems with my email provider.

Do you know how I can extend the value? I would like to instert more than one value.

For example:

if ( ‘21079’, ‘21080’, ‘21081’ === $posted_data[‘plz’] ) { $recipient_email = ‘office1@xyz.com’;

This gives me an syntax Error.

Also this didnt worked:

if ( ‘21079’ || ‘21080’ || ‘21081’ === $posted_data[‘plz’] ) { $recipient_email = ‘office1@xyz.com’;

Advertisement

Answer

Your question was almost correct. The set_properties() needs to pass the whole array (in your case) $mailProp.

/**
 * Dynamically Change the recipient.
 *
 * @param object $contact_form The contact form 7 contact form object.
 * @return void
 */
function cf7dynamicnotifications( $contact_form ) {
    $submission  = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class.
    $posted_data = $submission->get_posted_data(); // Get all of the submitted form data.
    // Make sure the field is filled in.
    if ( isset( $posted_data['plz'] ) ) {
        if ( '21079' === $posted_data['plz'] ) {
            $recipient_email = 'office1@xyz.com';
        } elseif ( '22085' === $posted_data['plz'] ) {
            $recipient_email = 'office2@xyz.com';
        } elseif ( '12345' === $posted_data['plz'] ) {
            $recipient_email = 'office3@xyz.com';
        } else {
            $recipient_email = 'head-office@xyz.com';
        }
        // 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 ) ); // Pass the whole array.
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement