Send Emails to Multiple Recipients in PHP

Welcome to a tutorial on how to send an email to multiple addresses in PHP – specifically setting multiple recipients, CC, and BCC. Although the PHP mail function might appear to only accept one recipient, it’s actually possible to address it to multiple people simultaneously. Let’s dive into the details and examples!

TLDR – Quick Overview

Table of Contents

Example 1) Basic PHP Mail Send

<?php
$to = "goodboy@doge.com";
$subject = "Hey Doge!";
$message = "Wow. Very message. Such test. Much words.";
echo mail($to, $subject, $message) ? "Mail send OK" : "Mail send ERROR";

To start off, let’s quickly recap the basics for those who may have forgotten:

  • The PHP mail function takes in 5 parameters: mail(TO, SUBJECT, MESSAGE, HEADERS, OPTIONS).
  • In this tutorial, we’ll focus on using TO and HEADERS to send emails to multiple recipients.
  • It’s worth noting that mail() returns a boolean value (true or false) indicating whether the email was successfully sent.

Example 2) Sending Mail with Multiple “TO” Recipients

<?php
// (A) Separate each email with a comma
$to = "first@doge.com, second@doge.com";

// (B) Trick - Implode an array
$to = implode(", ", [
  "first@doge.com",
  "second@doge.com"
]);

// (C) Subject + Message
$subject = "Hey Doges!";
$message = "Wow. Very message. Such test. Much words.";

// (D) Send!
echo mail($to, $subject, $message) ? "Mail send OK" : "Mail send ERROR";

The email to parameter of the mail() function accepts a string that complies with the RFC 2822 standard. In other words:

  • Separate each email with a comma (e.g., first@doge.com, second@doge.com).
  • To add custom names, use the format: John Doe <john@doe.com>, Jane Doe <jane@doe.com>.

Example 3) Adding CC & BCC in PHP Mail

<?php
// (A) TO, CC, BCC
$to = implode(", ", [
  "first@doge.com",
  "second@doge.com"
]);
$cc = implode(", ", [
  "third@doge.com",
  "forth@doge.com"
]);
$bcc = implode(", ", [
  "fifth@doge.com",
  "sixth@doge.com"
]);

// (B) Subject + Message
$subject = "Hey Doges!";
$message = "Wow. Very message. Such test. Much words!";

// (C) Email Header
$head = implode("rn", [
  "MIME-Version: 1.0",
  "Content-type: text/plain; charset=utf-8",
  "Cc: $cc",
  "Bcc: $bcc"
]);

// (D) Send!
echo mail($to, $subject, $message, $head) ? "Mail send OK" : "Mail send ERROR";

Adding CC and BCC recipients to the email requires defining custom email headers:

  • Include a line in the email header with Cc: EMAIL ADDRESSES for CC recipients.
  • Similarly, add another line with Bcc: EMAIL ADDRESSES for BCC recipients.
  • Additionally, the email header must contain the MIME-Version and Content-type to create a proper email header.
See also  Develop Your Own Project Management System with PHP and MySQL

Example 4) PHP Send Multiple Mail Function

<?php
// (A) Mail to Many Function
// Subject & Message are strings, as usual.
// To, CC, BCC should be arrays
function mmail($subject, $message, $to, $cc = null, $bcc = null) {
  // (A1) Build Header
  $head = [
    "MIME-Version: 1.0",
    "Content-type: text/plain; charset=utf-8"
  ];

  if ($cc !== null) {
    $head[] = "Cc: " . implode(", ", $cc);
  }

  if ($bcc !== null) {
    $head[] = "Bcc: " . implode(", ", $bcc);
  }

  $head = implode("rn", $head);

  // (A2) Send!
  return mail(implode(", ", $to), $subject, $message, $head);
}

// (B) Test
echo mmail(
  // (B1) Subject + Message
  "Hello World!",
  "This is a test email",
  // (B2) TO
  [
    "jo@doe.com",
    "joe@doe.com"
  ],
  // (B3) CC
  [
    "jon@doe.com",
    "joy@doe.com"
  ],
  // (B4) BCC
  [
    "john@doe.com",
    "jane@doe.com"
  ]
) ? "OK" : "ERROR";

Lastly, here’s a convenient function that allows you to send emails to multiple recipients out-of-the-box. Feel free to use it in your own projects.

Sorry for the Ads…

But someone has to pay the bills, and sponsors support this site. I insist on not turning ProgramMatek into a “paid scripts” business, and I don’t “block people with Adblock”. Every bit of support counts.

Become a Supporter

Example Code Download

Click here to access the source code on GitHub Gist. You can either download the zip or clone the repository. The code is released under the MIT license, allowing you to build on top of it or use it in your own projects.

Links & References

See also  ProgramMatek: The Perfect Solution for Building Your Classified Website

Tutorial Video

[Video Placeholder]

Infographic Cheat Sheet

PHP Mail To Multiple Recipients