How To Mail To Multiple Recipients In PHP – Multiple TO CC BCC

Welcome to a tutorial on how to send an email to multiple addresses in PHP – That is setting multiple recipients, the CC, and BCC. Yes, while the PHP mail function may seem to only accept one recipient, we can actually address it to many people at the same time.

To send an email to multiple recipients in PHP, we simply separate the emails using a comma. For example, mail("john@doe.com, jane@doe.com", SUBJECT, MESSAGE);

To add CC and BCC in PHP mail, we have to manually set the “Cc” and “Bcc” fields in the mail headers. For example, mail(TO, SUBJECT, MESSAGE, "Cc: joy@doe.com\r\nBcc: joe@doe.com");

That should cover the basics, but if you need more concrete examples – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Multiple Recipients Useful Bits & Links
Tutorial Video The End

 

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

MAILING TO MULTIPLE RECIPIENTS

All right, let us now dive into the various examples on how to set multiple recipients with PHP mail.

 

EXAMPLE 1) BASIC PHP MAIL SEND

1-simple-mail.php
<?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" ;

I know, this is not “multiple recipients”, but a quick recap for the folks who may have forgotten:

  • The PHP Mail function takes in 5 parameters – mail(TO, SUBJECT, MESSAGE, HEADERS, OPTIONS).
  • What we are interested in this tutorial is using TO and HEADERS to send to multiple people… Read up on the rest in your free time (links below).
  • Now, mail() will actually return a boolean true or false, if the mail is successfully sent. Something which some people seem to forget.

 

 

EXAMPLE 2) SENDING MAIL WITH MULTIPLE “TO” RECIPIENTS

2-multiple-to.php
<?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 actually accepts a string that complies with the RFC 2822 standard. Meaning, we only need to feed it with a string of properly formatted multiple recipients:

  • Separate each email with a comma. For example, first@doge.com, second@doge.com.
  • Or to add a custom name, John Doe <john@doe.com>, Jane Doe <jane@doe.com>.

 

EXAMPLE 3) ADDING CC & BCC IN PHP MAIL

3-cc-bcc.php
<?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("\r\n", [
  "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 the CC and BCC recipients to the email is a little bit more tricky, and we will need to define custom email headers.

  • Essentially, we need a line in the email header that says Cc: EMAIL ADDRESSES and another line for Bcc: EMAIL ADDRESSES.
  • Of course, to create a “proper email header”, we also add the MIME-Version and Content-type.

 

 

EXAMPLE 4) PHP SEND MULTIPLE MAIL FUNCTION

4-mail-many.php
<?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("\r\n", $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" ;

Finally, a small convenience for you guys – A simple function to send email to multiple recipients “out-of-the-box”. Feel free to use this in your own project.

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

PHP Mail To Multiple Recipients (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped to solve your email woes, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “How To Mail To Multiple Recipients In PHP – Multiple TO CC BCC”

    1. A random person said “this is not safe” and couldn’t explain why. So it must be true! 😆 Care to share and elaborate more on what is “not safe”? All we are doing here is to add a simple comma to the list of recipients as according to RFC 2822. Commas must be very dangerous. Yikes. I see why your sentence does not even have any punctuation now.

      P.S. If you are referring to firewalls, mail encryption, and secure exchanges – This tutorial is not about security.

Leave a Comment

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