Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Add CC BCC Multiple Recipients in PHP Mail

INTRODUCTION
THE HIDDEN SETTING

Welcome to a tutorial on how to add CC, BCC, and multiple recipients in Php Mail. While the PHP mail function may seem to only accept one recipient, we can actually define many at the same time. But as the for the CC and BCC, we will need to play with the optional mail headers – That is what we will walk through in this guide, read on!

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

 

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!


 

NAVIGATION
TABLE OF CONTENTS

Section A
PHP Mail Basics

Section B
Multiple Recipients

Extra
Useful Bits

Extra
Source Code Download

Closing
What’s Next?

SECTION A
PHP MAIL BASICS

Before we go into the actual part on multiple recipients, let us run through a quick recap of the basic PHP mail function first… Please feel free to skip this section if you are already an advanced code ninja.

THE BASIC MAIL SEND

1-simple-mail.php

Yep, the PHP mail is a very straightforward function that takes in 5 parameters:

  • Email to – The recipient.
  • The subject of the email.
  • The actual email message.
  • Additional headers, optional.
  • Additional parameters, optional.

REFERENCE

Click here to read the official PHP mail reference manual.

SECTION B
MULTIPLE RECIPIENTS

So just how do we define multiple recipients with the PHP mail function. Well, the answer is actually right there on the official reference page. But let us walk through that a little deeper in this section.

MULTIPLE RECIPIENTS

At first sight, the “email to” parameter seems to only accept one recipient, but that actually accepts a string that complies to the RFC 2822 standard. Meaning, we can throw in multiple recipients:

  • The easiest is to enter multiple emails, separated by commas.
  • We can also add the name of the recipient, followed by the email enclosed in a pair of angle brackets.
2a-many.php
, Doggo ";
 
// The rest is pretty much the same
$subject = "Hey Doge!";
$message = "Wow. Very message. Such test. Much words.";
$sent = mail($to, $subject, $message);
echo $sent ? "Mail send OK" : "Mail send ERROR" ;
?>

ADDING CC

Adding CC recipients to the email is a little bit more tricky, and we will need to define custom email headers:

2b-cc.php

ADDING MULTIPLE CC

As you can probably guess, adding multiple CC recipients is pretty much the same, defining a list of emails separated by commas.

2c-cc-many.php

ADDING BCC

Finally, I don’t think this needs any further explanation, just add another line in the headers to include BCC recipients in the email.

2d-bcc-many.php

EXTRA) FROM AND REPLY-TO

Just in case you have not noticed, we can also set the “email from” and “reply to” fields in the custom headers.

2e-from-reply.php
  'Reply-To: [email protected]',
  'Cc: [email protected], [email protected]',
  'Bcc: [email protected], [email protected]'
];
$headers = implode("\r\n", $headers);

// Send the email
$sent = mail($to, $subject, $message, $headers);
echo $sent ? "Mail send OK" : "Mail send ERROR" ;
?>

EXTRA
USEFUL BITS

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

SUMMARY

    • The PHP mail function takes in 5 parameters.
      • Email To The recipients of the email.
      • Subject Subject of the email.
      • Message The actual message of the email.
      • Headers Optional email headers.
      • Parameters Optional email send parameters.
    • To send to multiple recipients, we add multiple emails to the Email To parameter, separated by commas.
    • We define CC and BCC in the custom Headers instead.
    • Multiple CC and BCC works the same way – We define many emails, separated by commas.

INFOGRAPHIC CHEAT SHEET

How to add multiple recipients in PHP Mail (click to enlarge)

SENDING BULK MAIL

Need to send out a lot of emails? Check out my other guide:

4 Steps to Bulk Send Email with PHP & MySQL (Free Script Download)

EXTRA
DOWNLOAD

Finally, here is the download link as promised.

QUICK NOTES

There are no database nor “special set up” required in this one, but just make sure that you change the recipients to legit email accounts before you run the tests. Also, please make sure that you have a working SMTP server, or the emails will not send out properly – Read my other guide on fixing the email problem if you are having trouble.

SOURCE CODE DOWNLOAD

Click here to download the 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.
 

CLOSING
WHAT’S NEXT?

Thank you for reading, and we have come to the end of this guide. 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!

The post How to Add CC BCC Multiple Recipients in PHP Mail appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

How to Add CC BCC Multiple Recipients in PHP Mail

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×