PHP 邮件:多个收件人?

2022-08-30 14:13:26

我有这个代码:

<?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);
?>

在我的表格(“电子邮件”)中,我有多个地址。(它们不是逗号分隔的。如何将邮件发送到所有这些地址?


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

mail() 手册页上所指定,函数的 “to” 参数可以采用逗号分隔的地址列表。


答案 2

用逗号分隔地址。

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

...

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

推荐