18

I want to send the following fields to wp_mail

enter image description here

If i put all the emails in Email to,Copy to and Bcc to in an array $emails and pass it to wp_mail. How do i set the headers for cc and bcc in wp_mail?

$headers = 'From: Test <[email protected]>' . '\r\n';
$headers.= 'Content-type: text/html\r\n'; 
$success = wp_mail( $emails, $subject, $message, $headers );  
4
  • Have you tried by creating an array and defining them into it ? Commented May 7, 2015 at 10:42
  • show the format of $emails
    – jogesh_pi
    Commented May 7, 2015 at 10:51
  • $emails is simply an array of email address. I don't know how to set headers cc and bcc for the emails in the array.
    – user892134
    Commented May 7, 2015 at 10:57
  • @user892134 I want this functionality. Can you please help me to achieve this?
    – NewUser
    Commented Jan 6, 2021 at 10:55

1 Answer 1

47

You can use an array to send all the info you need, thus:

$headers[] = 'From: Test <[email protected]>';
$headers[] = 'Cc: [email protected]';
$headers[] = 'Cc: [email protected]';
...
$headers[] = 'Bcc: [email protected]';
$headers[] = 'Bcc: [email protected]';
$success = wp_mail( $emails, $subject, $message, $headers );  

You can get it programmatically, being $copy_to and $bcc_to arrays of said form fields after splitting them by the comma you state in the inner field text, and having defined array $headers:

$headers[] = 'From: Test <[email protected]>';
foreach($copy_to as $email){
    $headers[] = 'Cc: '.$email;
}
foreach($bcc_to as $email){
    $headers[] = 'Bcc: '.$email;
}
$success = wp_mail( $emails, $subject, $message, $headers );  

These work because wp_mail() doesn't trust you, so normalizes and concatenates multiple Cc: and Bcc: headers correctly. The RFC spec mandates that these headers occur only once, with multiple recipients comma-separated, and "folded" if the lines are too long, so it wouldn't work in plain PHP.

2
  • What about $emails? is it going to be an array of all the users email or what? I am trying with all the users in that $emails array but it is showing all the users in the To list.
    – NewUser
    Commented Jan 5, 2021 at 14:52
  • @NewUser you can leave the $emails as empty string i.e wp_mail('', $subject, $message, $headers)); Commented Feb 20, 2021 at 23:05

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.