Sending emails with bcc and cc with wp_mail
I want to send the following fields to wp_mail
If I put all emails into Email to
, Copy to
and Bcc to
into an array $emails
and pass it to wp_mail. How to set headers for cc and bcc in wp_mail?
$headers = 'From: Test <info@test.co.uk>' . '\r\n';
$headers.= 'Content-type: text/html\r\n';
$success = wp_mail( $emails, $subject, $message, $headers );
source to share
You can use an array to send all the information you need, like this:
$headers[] = 'From: Test <info@test.co.uk>';
$headers[] = 'Cc: copy_to_1@email.com';
$headers[] = 'Cc: copy_to_2@email.com';
...
$headers[] = 'Bcc: bcc_to_1@email.com';
$headers[] = 'Bcc: bcc_to_2@email.com';
$success = wp_mail( $emails, $subject, $message, $headers );
You can get it programmatically, being $copy_to
both $bcc_to
arrays of the specified form fields after separating them on a comma that you specify in the inner textbox and defining an array $headers
:
$headers[] = 'From: Test <info@test.co.uk>';
foreach($copy_to as $email){
$headers[] = 'Cc: '.$email;
}
foreach($bcc_to as $email){
$headers[] = 'Bcc: '.$email;
}
$success = wp_mail( $emails, $subject, $message, $headers );
source to share