To add BCC
(Blind Carbon Copy) recipients when sending mail in PHP, you can modify the headers to include BCC
recipients. Here’s how you can do it:
Example using PHP mail()
function:
<?php
// Email recipient
$to = "recipient@example.com";
// Subject of the email
$subject = "Test Email with BCC";
// Message body
$message = "This is a test email with BCC using PHP mail() function.";
// Headers
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "Bcc: hidden1@example.com, hidden2@example.com\r\n"; // Add BCC recipients
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
// Send the email
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
?>
Explanation:
$headers
: The$headers
variable is where you define your email headers. In this case, you first add theFrom:
header and then append theBCC:
header.BCC:
: You can include multiple email addresses separated by commas.\r\n
: Ensures proper line breaks in email headers.
This will send the email to the primary recipient and also BCC it to hidden1@example.com
and hidden2@example.com.
Considerations:
- The
mail()
function relies on the server’s mail configuration (sendmail, Postfix, etc.). If it’s not working, consider using a third-party SMTP library like PHPMailer. - Some mail servers may block emails with BCC to prevent spam.
- Would you like an example using PHPMailer (which is more reliable for sending emails)?