Send email with CC and BCC in php

In this tutorial you will learn how to send email with CC and BCC in php. I have already written a tutorial about sending html emails using php in which I have written a custom made function send_email($from, $to, $subject, $message) to send html email. You just have to add the two lines of code in that function and that's it. Let's have a look over how to do so

Send email with CC and BCC in php

function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n"; 
$headers .= "CC: sombodyelse@gmail.com\r\n";
$headers .= "BCC: hiddenemail@gmail.com\r\n";
//set content type to HTML 
$headers .= "Content-type: text/html\r\n"; 

if ( mail($to,$subject,$message,$headers) ) 
{
header("location:yourdesirepage.php?msg=sent");

} 
else 
{
header("location:yourdesirepage.php?msg=notsent");
}
}
//Function ends here
$from_email="fromyou@gmail.com";
$to_email="tosomeone@gmail.com";
$subject="Subject of Email";
$message.="<html><body>";
$message.="<strong>Your message here will be look bold when visible to reader</strong>";
$message.="Some more message";
$message.="</body></html>";
send_email($from_email,$to_email,$subject,$message);



In the send_email() function, i used two more headers CC and BCC to send email with CC and BCC in php.

$headers .= "CC: sombodyelse@gmail.com\r\n";
CC means Carbon Copy. A comma separated list of more recipients that will be seen by all other recipients.
$headers .= "BCC: hiddenemail@gmail.com\r\n";

BCC means Blind Carbon Copy. A comma separated list of more recipients that will not be seen by any other recipients.

Note:-Be remember, Header names are case sensitive. Each header is ended with the Return and Newline characters \r\n. There are no spaces among header name.


So that's it.




Happy coding!!!

0 comments: