简体   繁体   中英

PHP mail: Multiple recipients?

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?

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.

Separate the addresses with commas.

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

...

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

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! :)

您只需要使用GROUP_CONCAT返回以','分隔的结果

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

According to Alex's answer above, recipients will know the first person in the list even if you're using BCC. A solution for this would be to send individual copies to each address one by one.

$emails = ['a@example.com', 'b@example.com', 'c@example.com'];
foreach ($emails as $email){ // or $result as $row
  mail(
    $email, // or $row['address']
    'Same Subject',
    'Same Content',
    'From: same_sender@example.com',
    '-f same_sender@example.com'
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM