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

Time:03-26

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 = '[email protected]';
   }
   elseif($posted_data["plz"] == '22085') { 
      $recipient_email = '[email protected]';
   }
   elseif($posted_data["plz"] == '12345') { 
      $recipient_email = '[email protected]';
   }
   else { 
      $recipient_email = '[email protected]';
   }
   // 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']));
}

CodePudding user response:

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 = '[email protected]';
        } elseif ( '22085' === $posted_data['plz'] ) {
            $recipient_email = '[email protected]';
        } elseif ( '12345' === $posted_data['plz'] ) {
            $recipient_email = '[email protected]';
        } else {
            $recipient_email = '[email protected]';
        }
        // 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.
    }
}

CodePudding user response:

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 = '[email protected]';

This gives me an syntax Error.

Also this didnt worked:

if ( '21079' || '21080' || '21081' === $posted_data['plz'] ) { $recipient_email = '[email protected]';

  • Related