How to add a BCC to PHP mail() command.

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
$to = "recipient@example.com"; // Primary recipient
$subject = "Subject of the email";
$message = "This is the body of the email.";
$headers = "From: sender@example.com\r\n";
$headers .= "BCC: bcc1@example.com, bcc2@example.com\r\n"; // Add BCC recipients

// Send the email
mail($to, $subject, $message, $headers);
?>

Explanation:

  • $headers: The $headers variable is where you define your email headers. In this case, you first add the From: header and then append the BCC: 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 bcc1@example.com and bcc2@example.com.

Considerations:

  • This method works with PHP’s mail() function but has limitations for more complex use cases (e.g., large BCC lists, attachments). In those cases, it’s better to use a dedicated library like PHPMailer or SwiftMailer.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *