WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add Bcc: to Contact Form 7 through extension (3 posts)

  1. tliebig
    Member
    Posted 1 year ago #

    Hi,

    I'm trying to add a Bcc: header to a contact form that gets it address through a shortcode. I'd like to to set when sending the mail without ever publicicly exposing the address. I feel I'm close, but it's just not adding the Bcc. The code:

    add_action( 'wpcf7_before_send_mail', 'add_affiliate_bcc' );
    function add_affiliate_bcc( $wpcf7 ) {
      $affiliate_recipient = do_shortcode('[affiliate_recipient]');
    
      if ( $affiliate_recipient AND stristr( $wpcf7->title, 'order') ) :
    
            $wpcf7->setup_mail_template( array('additional_headers' => 'Bcc: ' . affiliate_recipient, 'subject' => 'modified header successfully!' ) );
    
      endif;
    }

    But alas, no header ever get's modified, and the mail goes out just the same way as without the action. Anyone more knowledgeable in here who could help me out?

  2. tliebig
    Member
    Posted 1 year ago #

    Wohoo, now that was fun - after finding a way to inspect the $wpcf7 object, I was able to pinpoint every data I need. The solution below just takes the data after the mail was sent and sends yet another mail through wp_mail to the recipient of your choice:

    add_action( 'wpcf7_mail_sent', 'add_bcc' );
    function add_bcc( $wpcf7 ) {
    
      /* we're not actually adding a bcc but instead send yet another mail */
        $affiliate_recipient = 'some_recipient@domain.com';
        $subject = 'Your custom mail subject';
    
        /* get mail body from wpcf7 object (gets returned with your [tag-name] form tags ) */
        $mailbody = $wpcf7->mail["body"];
    
        /* inject posted data into in the form tags */
        foreach ( $wpcf7->posted_data as $form_name => $value ) :
            $mailbody = str_replace( "[".$form_name."]", $value, $mailbody );
        endforeach;
    
        /* Bonus: If you'd like to apply your custom shortcodes in the mail body, just uncomment the next line line */
        // $mailbody = do_shortcode($mailbody);
    
        /* all done, so let's send the mail! */
        @wp_mail( $affiliate_recipient, $subject, $mailbody );
    
    }
  3. jasiiboss
    Member
    Posted 1 year ago #

    This is fantastic. Just what the doc ordered.

    Thank you for sharing

Topic Closed

This topic has been closed to new replies.

About this Topic