• Resolved thewaysian

    (@thewaysian)


    I had a bit of code working a while back that would dynamically change who would receive the email based on what city was filled out in the form. But right now it is defaulting to the if, and skipping the else entirely. So now all emails are going to abc@xyz.com regardless of city. Any ideas what I’m doing wrong here? I’m new to PHP.

    // 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["property-City"] == 'denver' or 'Denver' or 'arvada' or 'Arvada' or 'wheat ridge' or 'Wheat Ridge' or 'aurora' or 'Aurora' or 'parker'or 'Parker' or 'cherry creek' or 'Cherry Creek' or 'greenwood village' or 'Greenwood Village' or 'centennial' or 'Centennial' or 'thornton' or 'Thornton' or 'northglenn' or 'Northglenn' or 'commerce city' or 'Commerce City' ) { // If General option is selected
          $recipient_email = 'abc@xyz.com';
       }
       else { // If no dropdown option is selected
          $recipient_email = 'efg@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']));
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Where can we see the website in question?

    Thread Starter thewaysian

    (@thewaysian)

    Website in question is Here

    But I figured out a solution that works. I had to pull the data, implode it, and then compare it against an array using in_array. I don’t know if this was the best way to go about it, but it does work now.

    //Place in wp-content/themes/neve/functions.php
    // 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
       $contact_form = WPCF7_ContactForm::get_current();
       $form_id = $contact_form -> id;
       $property_city = $submission->get_posted_data('property-City');
       $property_city = implode( ', ', (array) $property_city);
       $denverCity = array('denver', 'arvada', 'wheat ridge', 'aurora', 'parker', 'cherry creek', 'greenwood village', 'centennial', 'thornton', 'northglenn', 'commerce city'); // ILC Denver Area
       $south = array('lakewood', 'golden', 'castle rock', 'colorado springs', 'pueblo'); //Property Survey South Area 
       $north = array('longmont', 'lyons', 'estes park', 'berthoud', 'loveland', 'fort collins', 'greeley', 'evans'); //Property Survey North Area
    
    if($form_id == '242'){ //ILC Code
        if (in_array(strtolower($property_city), $denverCity) ) { //Check for Denver Metro Cities
          $recipient_email = 'j@flatironsinc.com';
       }
       else { //Otherwise send to Sterl
          $recipient_email = 's@flatironsinc.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']));
        }
    
    elseif($form_id == '233'){ //Property Survey Code
        if (in_array(strtolower($property_city), $south) ) { //Check for South Region
          $recipient_email = 'j@flatironsinc.com';
       }
       elseif (in_array(strtolower($property_city), $north) ) { //Check for North Region
          $recipient_email = 'j2@flatironsinc.com';
       }
       else { //Otherwise send to Zack
          $recipient_email = 'z@flatironsinc.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']));
        }
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Dynamic Email Recipient Not Working’ is closed to new replies.