向数据库中的所有用户发送电子邮件

Send an email to all users in a database

如何在不被任何垃圾邮件过滤器标记的情况下向数据库中的每个用户发送电子邮件?我假设您将不得不在一个月或更短的时间内全天发送电子邮件。当然,我可以立即通过简单的查询获取所有用户的电子邮件地址 select 'email' from 'users' 但是我似乎迷失了执行此类任务的正确方法......过去我使用过诸如 sendgrid 之类的服务,但是这些公司遵循什么准则才能避免所有问题垃圾邮件过滤器?

获得结果集后,您可以循环并在每个 1 上调用 mail()。希望您的服务器能够处理这个问题。

foreach($result as $user) {
    mail($user->email,"My subject",$msg);
}

我用 PHPMailer 发送电子邮件。这简单。只需迭代来自 DB 的所有电子邮件并使用此库发送。

是的,你可以做到。按照给定的代码。

while($row = mysql_fetch_array($result))
{
    $addresses[] = $row['email'];
}
$to = implode(", ", $addresses);
$subject = 'the subject';
$message = 'hello';
mail($to, "Your Subject", "A message set by you.", "If header information.");

首先,您必须收集所有以逗号分隔的电子邮件地址。之后只需使用邮件功能即可同时向所有用户发送邮件。

如果您想单独发送邮件,请使用:

while($row = mysql_fetch_array($result))
{
    $to = $row['email'];   
    $subject = 'the subject';
    $message = 'hello';
    mail($to, "Your Subject", "A message set by you.", "If header information.");
}

更新: 由于 mysql_* 已弃用,请使用 mysqli_* 而不是所有函数。

while($row = mysqli_fetch_array($result))
{
    $addresses[] = $row['email'];
}
$to = implode(", ", $addresses);
$subject = 'the subject';
$message = 'hello';
mail($to, "Your Subject", "A message set by you.", "If header information.");

while($row = mysqli_fetch_array($result))
{
    $to = $row['email'];   
    $subject = 'the subject';
    $message = 'hello';
    mail($to, "Your Subject", "A message set by you.", "If header information.");
}

请阅读mysqli_*的手册。