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

时间:2016-02-10 17:31:23

标签: php sendmail

如何在不被任何垃圾邮件过滤器标记的情况下向数据库中的每个用户发送电子邮件?我假设您必须在一个月或更短的时间内全天发送电子邮件。当然,我可以通过简单的查询获得所有用户的电子邮件地址 select 'email' from 'users'但是实施正确的方法来完成这样的任务就是我似乎迷失了......过去我曾经使用过诸如sendgrid之类的服务,但这些公司遵循的指导原则是为了避免所有这些垃圾过滤?

3 个答案:

答案 0 :(得分:7)

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

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_*手册。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我使用PHPMailer发送电子邮件。这很容易。只需迭代来自数据库的所有电子邮件并使用此库发送。

相关问题