• Resolved robrandell

    (@robrandell)


    Hi,
    I am attempting to add an additional ‘cc’ header to the email being sent via CF7 coupled with ACF custom field. However, the email sends successfully without the ‘cc’ added. Perhaps been staring at it for too long, but I can’t figure it out as yet. Code below in ‘functions.php’.

    function add_jobmanager_recipient( $cf7 ) {
    
        global $post;
        $submission = WPCF7_Submission::get_instance();
    
    	if ( $submission ) {
    		$data = $submission->get_posted_data();
            $cc_email = get_field( 'job_contact_email_address', $post->ID );
    
    		if ( empty( $data ) || empty( $cc_email ) ) {
    			return;
            }
    
    		$mail = $cf7->get_properties();
    		$mail[ 'mail' ][ 'additional_headers' ] .= "\r\nCc: $cc_email";
    		
    		$cf7->set_properties( $mail );
        }
        return $cf7;
    }
    add_action( 'wpcf7_before_send_mail', ' add_jobmanager_recipient', 20, 2 );

    Thanks for any pointers.

    Cheers,
    Rob

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    You have 2 mistakes here:

    add_action( 'wpcf7_before_send_mail', ' add_jobmanager_recipient', 20, 2 );

    Thread Starter robrandell

    (@robrandell)

    Thanks. I knew I was staring at it for too long.
    The other problem was the $post->ID was a null object, so I switched it to get the meta data of the submission, specifically the container_post_id. Final code below if it helps anyone else.

    function add_jobmanager_recipient($cf7)
    {
        $submission = WPCF7_Submission::get_instance();
    
        if ($submission) {
            $data = $submission->get_posted_data();
            $cc_email = get_field(
                'job_contact_email_address',
                $submission->get_meta('container_post_id')
            );
    
            if (empty($data) || empty($cc_email)) {
                return;
            }
    
            $mail = $cf7->get_properties();
            $mail['mail']['additional_headers'] .= "\r\nCc: $cc_email";
    
            $cf7->set_properties($mail);
        }
        return $cf7;
    }
    add_filter('wpcf7_before_send_mail', 'add_jobmanager_recipient', 10, 1);

    Thanks,
    Rob

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Dynamic CC Additional Header’ is closed to new replies.