PHP Multiple Recipients πŸ“¨πŸ’¬

beginner
20 min

PHP Multiple Recipients πŸ“¨πŸ’¬

Welcome to this comprehensive guide on sending emails to multiple recipients using PHP! In this tutorial, we'll learn how to send emails with BCC, CC, and To fields. Let's dive in!

Sending an Email with To, Cc, and Bcc Fields 🎯

Before we start, make sure you have a basic understanding of PHP and its mail() function.

php
<?php $to = "recipient1@example.com"; $cc = "cc@example.com"; $bcc = "bcc@example.com"; $subject = "Test Email"; $message = "Hello, this is a test email."; $headers = "From: you@example.com"; mail($to, $subject, $message, $headers . "Cc: $cc" . "Bcc: $bcc"); ?>

In the code above, we have three email fields: $to, $cc, and $bcc. Let's understand what each of them does:

  • $to: The primary recipient of the email.
  • $cc: The Carbon Copy (CC) recipient, who receives the same copy of the email as the primary recipient.
  • $bcc: The Blind Carbon Copy (BCC) recipient, who receives the same copy of the email as the primary recipient, but their email addresses are hidden from other recipients.

Pro Tip:

  • Always use BCC field when sending emails to multiple recipients to respect their privacy.

Sending an Email to Multiple Recipients with To Field πŸ“

If you want to send an email to multiple recipients using the To field, you can separate their email addresses with a comma (,).

php
<?php $to = "recipient1@example.com, recipient2@example.com, recipient3@example.com"; $subject = "Test Email"; $message = "Hello, this is a test email."; $headers = "From: you@example.com"; mail($to, $subject, $message, $headers); ?>

Quiz πŸ’‘

Question: What is the primary recipient of an email when using PHP's mail() function? A: BCC B: CC C: To Correct: C Explanation: The primary recipient of an email is specified using the $to variable.

Wrapping Up βœ…

Now you know how to send emails with multiple recipients using PHP's mail() function. Remember to use BCC to respect recipients' privacy when sending emails to multiple recipients. Happy coding!

Keep learning and stay tuned for more tutorials on CodeYourCraft! πŸŽ‰πŸŽ“πŸ’»