• Resolved wpcork

    (@wpcork)


    Hello,

    Is it possible to access the form ID before the submission is sent, or would this be handled by Zapier.

    I want to have 2 “send to” emails and each one will be used alternatively. I think that the easiest way to do this would be to access the form ID and then have an if/else function that would set the email address.

    Something like this…

    add_filter( 'ctz_get_data_from_contact_form', 'set_email' );

    function set_email( $data ) {
    // SET THE EMAIL ADDRESS
    if ($data['id']%2 = 0){
    $data['send_to']= '[email protected];
    } else {
    $data['send_to']= '[email protected];
    }
    return $data;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mário Valney

    (@mariovalney)

    Hi!

    If every form should have a specific sent_to, why not use a hidden field with this value?

    Anyway, you can receive form in the second param:

    /**
    * @param $data Array 'field => data'
    * @param $contact_form ContactForm obj from 'wpcf7_mail_sent' action
    */
    apply_filters( 'ctz_get_data_from_contact_form', $data, $contact_form );
    Thread Starter wpcork

    (@wpcork)

    Hi Mário,

    Thanks for your response and the complexity is caused by the user wanting to alternate between two different email addresses, where each address gets every second email.

    It looks like I’m going to need to install Flamingo, which will give me access to an auto-incrementing submission ID. I can use that along with the “wpcf7_before_send_mail” hook to check if the ID is even or odd. Then the even numbers can be sent to one email and the odd numbers can be sent to the other email.

    It would look something like this…

    add_action( 'wpcf7_before_send_mail', 'wpcf7_change_recipient' );
    function wpcf7_change_recipient($contact_form){
    $submission = WPCF7_Submission::get_instance();

    $recipient = "[email protected]" //set email address

    if ($mail[_serial_number] % 2 == 0) {
    $recipient = "[email protected]" //change email address if ID is even
    }

    if($recipient) {
    $mail = $contact_form->prop( 'mail' );
    $mail['recipient'] = $recipient;
    $contact_form->set_properties(array('mail'=>$mail));
    }
    }

    If there’s a better way to do this, please let me know.

    Plugin Author Mário Valney

    (@mariovalney)

    user wanting to alternate between two different email addresses

    You mean each submission should go to other mail? To1, To2, To1, To2… ?

    In your place I would try using the Options API. And I don’t like to save the mail prop everytime.

    Maybe using wpcf7_mail_components filter?

    <?php

    add_action( 'wpcf7_mail_components', 'example_wpcf7_mail_components', 10, 2 );
    function example_wpcf7_mail_components( $components, $contact_form ) {
    if ( $contact_form->get_something() !== 'check-something' ) {
    return $components;
    }

    $sending = get_option( 'example_form_serial', 0 );
    $mail = ($sending % 2) ? '[email protected]' : '[email protected]';

    // I'm not sure what is "$components['recipient']" so check before
    $components['recipient'] = $mail;
    update_option( 'example_form_serial', $sending++ );

    return $components;
    }

    P.S.: Marking as “not a support question” because it’s not related to my plugin.

    • This reply was modified 3 months, 3 weeks ago by Mário Valney.
    Thread Starter wpcork

    (@wpcork)

    Hi Mario,

    Thanks again for your help and I’m sorry that this is taking up your time without getting the benefit of getting a new customer.

    I’ll try creating a new option which will be called by the get_option() and incremented by the updated_option().

    I can also use the form title to make sure it’s the correct one as you included in your code…

    $contact_form->get_something() !== 'check-something'
    $contact_form->title !== 'The Correct Form'

    Thanks again,
    – Donal

    Plugin Author Mário Valney

    (@mariovalney)

    No problem! I have no customers… everything is opensource haha

    Amazing you got it. Let me know if you need some help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.