6

I have this code part that's responsible for getting the variables from the contact form - and and sending an email to me, and my friends from work.

add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form'); 
// Send information from the contact form
function submit_contact_form(){

    // If there is a $_POST['email']...
    if( isset($_POST['email']) && ($_POST['validation'] == true ) ) {

        $email = $_POST['email'];       
        $email_to = "[email protected]";
        $fullname = $_POST['fullname'];
        $headers = 'From: '. $fullname .' <'. $email .'>' . "\r\n";
        $group_emails = array(
            '[email protected]', 
            '[email protected]', 
            '[email protected]', 
            '[email protected]', 
            '[email protected]' 
            );
        $email_subject = "example intro: $email";
        $message = $_POST['text']; 

        if(wp_mail($group_emails,$email_subject,$message,$headers)) {
            echo json_encode(array("result"=>"complete"));
        } else {
            echo json_encode(array("result"=>"mail_error"));
            var_dump($GLOBALS['phpmailer']->ErrorInfo);
    }
        wp_die();
    }
}

I want to add to the headers 4 emails as BCC.

How do I do this right? I tried a few variations of writing it without any success.

1 Answer 1

8

$headers can be a string or an array, but it may be easiest to use in the array form. To use it, push a string onto the array, starting with "From:", "Bcc:" or "Cc:" (note the use of the ":"), followed by a valid email address.

https://codex.wordpress.org/Function_Reference/wp_mail#Using_.24headers_To_Set_.22From:.22.2C_.22Cc:.22_and_.22Bcc:.22_Parameters

In other words:

$headers = array(
    'From: [email protected]', 
    'CC: [email protected]', 
    'CC: [email protected]', 
    'BCC: [email protected]', 
    'BCC: [email protected]' 
);

You can see where the Core parses the string by splitting it on that ":":

296  list( $name, $content ) = explode( ':', trim( $header ), 2 );
297 
298                                 // Cleanup crew
299                                 $name    = trim( $name    );
300                                 $content = trim( $content );
301 
302                                 switch ( strtolower( $name ) ) {
303                                         // Mainly for legacy -- process a From: header if it's there
304                                         case 'from':

Note: This is untested but I am fairly confident. I did not want to start sending email to addresses without warning (if those are even active addresses).

3
  • The only thing I changed for it to work is using $header and not $group_emails. TNX!
    – Kar19
    Commented Jun 7, 2015 at 15:27
  • 1
    Can you post your final code? Help will be appreciated.
    – WordCent
    Commented Aug 8, 2019 at 10:55
  • I note that wp_mail's processing of additional headers doesn't work exactly the same as the PHP mail() command. WP_MAIL uses additional headers in a one-dimensional array ("from:[email protected]"). The PHP mail() function allows for a two-dimensional array, as in $header = array("from" => "[email protected]"). If you supply a two-dimensional additional headers array, wp_mail will not process the additional headers properly. Commented Aug 1, 2022 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.