Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 28, 2023 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/ebf330983f37eb9689988fa5036ce947 to your computer and use it in GitHub Desktop.
Save code-boxx/ebf330983f37eb9689988fa5036ce947 to your computer and use it in GitHub Desktop.
PHP Email Multiple Recipients

PHP EMAIL TO MULTIPLE RECIPIENTS

https://code-boxx.com/php-mail-multiple-recipients-cc-bcc/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?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" ;
<?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" ;
<?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" ;
<?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" ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment