Support » Plugin: Contact Form 7 » How to use post meta to set only ‘mail1’ recipient dynamically?

  • Resolved shinchaya


    I saw the support forum in the past.

    “How to use post meta to set mail recipient dynamically?”

    but in the code introduced here, when using Mail 2, the mail recipient of Mail 2 will also be replaced.
    What kind of code should I write in order not to replace the mail recipient of Mail 2?


    function set_question_form_recipient($components, $form, $object) {
      if ($form->id() == 12) :
        // Get the ID of the post the form was sent from
        $post_id = wpcf7_special_mail_tag('', '_post_id');
        // Get the recipient e-mail address from an Advanced Custom Field
        $recipient_email = get_field( 'name_of_acf_field', $post_id );
        // If you use a regular (non-ACF) custom field, use:
        // $recipient_email = get_post_meta( $post_id, 'name_of_custom_field', true );
        if ($recipient_email) :
          // Set the recipient
          $components['recipient'] = $recipient_email;
      // Return the modified array (not sure if needed)
      return $components;
    add_filter('wpcf7_mail_components', 'set_question_form_recipient');
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi


    A callback function bound to the wpcf7_mail_components filter takes a WPCF7_Mail object as its third argument. WPCF7_Mail’s name() method returns ‘mail’ when it’s the primary Mail, and returns ‘mail_2’ when it’s Mail (2).

    So in your case, put this at the head of the function:

    if ( 'mail' !== $object->name() ) {
    	return $components;

    Mr. Miyoshi
    I got an output result as expected.
    Thank you for your response regardless of your busy situation.


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use post meta to set only ‘mail1’ recipient dynamically?’ is closed to new replies.