PHP mail: Multiple recipients?

2019-01-08 22:35发布

问题:

I have this code:

<?php
include("db.php");

$result = mysql_query("SELECT * FROM email");

while($row = mysql_fetch_array($result))
{
$to = $row['address'];
}
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "example@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>

In my table ("email") I have multiple addresses. (They are not comma sepparated.) How could I send my message to all of those addresses?

回答1:

while($row = mysql_fetch_array($result))
{
    $addresses[] = $row['address'];
}
$to = implode(", ", $addresses);

As specified on the mail() manual page, the "to" parameter of the function can take a comma-separated list of addresses.



回答2:

Separate the addresses with commas.

$to=array();
while($row = mysql_fetch_array($result)) {
    array_push($to, $row['address']);
}

...

mail(implode(',', $to), $submit, $message, $headers);


回答3:

I just tested the codes you presented and before using them, people need to know that using this way (multiple addresses in 'to' field), every single person in that email can see all the destinatars.

Also, if you're using Bcc, they'll also know the first person in the list.

Be aware! :)



回答4:

You just need to use GROUP_CONCAT to return the results separated by ','

$result = mysql_query("SELECT GROUP_CONCAT(address) FROM email");


标签: php email