View Full Version : Mail: Multiple tos

06-03-2004, 07:57 PM
How could you use the mail function to send to more than one to, for the purpose of this, let's use this example from the PHP Manual:
mail("nobody@example.com", "the subject", $message,
"From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
"X-Mailer: PHP/" . phpversion());

06-03-2004, 08:06 PM
function mailStuff($email) {
mail($email, "the subject", $message,
"From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
"X-Mailer: PHP/" . phpversion());

Then somewhere else...

$addresses = array("john@dot.com","doe@john.com","mary@smith.com");
for($i=0;$i<count($addresses);$i++) {

06-03-2004, 08:28 PM
You don't need to repeatedly invoke the mail command for each recipient. You just need to seperate the addresses with a comma just like it shows in the PHP manual

"To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n";

06-03-2004, 08:41 PM
You don't need to repeatedly invoke the mail command for each recipient. You just need to seperate the addresses with a comma just like it shows in the PHP manual

"To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n";

Hail Spookster ;)

06-03-2004, 08:53 PM
And you can still use some of bcarl's suggestion by defining the addresses elsewhere and storing them in just a string variable instead of an array.

$addresses = "To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n";

mail($addresses, "the subject", $message,
"From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
"X-Mailer: PHP/" . phpversion());

06-03-2004, 09:26 PM
OK, I'm having problems with:
/* recipients */
$db = 'geoffers_newsletter';
$db_user = 'dunnomy';
$db_pass = 'dunnomypass';

@mysql_select_db($db) or die( "Unable to select database");
$query="SELECT email FROM email";



while ($i < $num) {



/* subject */
$subject = 'test';

/* message */
$message = 'test';

/* additional headers */
$headers .= "From: Geoffrey Sneddon <geoffers@geoffers.uni.cc>\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);

06-03-2004, 09:53 PM


... be ...


06-03-2004, 11:26 PM
The reason I would call a function and mail individually is so the recipients do not get all the other email addresses. Some people get freaky about that.

But both methods work just fine.

06-04-2004, 12:10 PM
Id have to agree with bcarl, becuase I wouldnt want many people getting my email address...

However, I also agree that it is not always suitable to make multiple calls to mail(), which would take a long time.

Going more advanced now, you can get hold of SMTP mail scripts... Its much more complex but for large mail sending is much faster. But for sending 2 or 3 emails on a page, shouldnt be required.

06-04-2004, 12:41 PM

I've actually been looking for something like that, can you point me to some resources?


06-04-2004, 12:48 PM
Maybe this will be of some use (I haven't had a look over properly):


06-04-2004, 06:24 PM
How could I put in a , if there was another address?

07-08-2004, 09:35 PM
to send all the mail in 1 go without sharing the email address could you not just send the mail as a BCC because then you cannot see all the other mail addresses it has been sent to

not sure how you would do it with the mail feature though - anyone, does it work?

07-08-2004, 10:24 PM
sending 1 mail with all receivers in bcc is the way to go.

mail() will open a new socket + close it for eacht iterantion if you send them inside a loop, so it will be 'rather slow'. From the crashtests i did a while back, i found that you can easely send out 500 mails before your script times out, but that the time it took to get a chunk of 15 mails out drastically increased with each chunk (i sent out 30 chunks of 15 mails and included the resuls of a timerfunction in the message.)

to send them as bcc's, all you need to do is add them in the header-atribute as shown in example 4 of http://www.php.net/function.mail

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum