Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending emails with bcc and cc with wp_mail

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 );  
like image 625
user892134 Avatar asked May 07 '15 10:05

user892134


People also ask

How to send mail with CC and Bcc in PHP?

= "CC: [email protected]\r\n"; $recipient . = "BCC: [email protected]\r\n"; $subject = "Contact Form"; $mailheader = "From: $email \r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); echo "Thank You!" .

What is wp_mail?

wp_mail() is a PHPMailer function that generates and sends your WordPress emails. By default, almost all WordPress plugins use wp_mail() to send emails. But the wp_mail() function is not very reliable. So when something goes wrong with it, you'll find email delivery issues across your whole site.

What is add CC in email?

Just like the physical carbon copy above, CC is an easy way to send copies of an email to other people. If you've ever received a CCed email, you've probably noticed that it will be addressed to you and a list of other people who have also been CCed.


1 Answers

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 );  
like image 164
Dez Avatar answered Oct 05 '22 00:10

Dez