如何从数据库查询(PHP)向多个收件人发送电子邮件
我正在尝试向数据库中的多个电子邮件地址发送电子邮件。这是我目前的代码。它仅在指定单个电子邮件地址时才起作用,但是,我需要让他们查询我的数据库并将电子邮件发送到每个电子邮件地址。我哪里出了问题如何从数据库查询(PHP)向多个收件人发送电子邮件,php,email,Php,Email,我正在尝试向数据库中的多个电子邮件地址发送电子邮件。这是我目前的代码。它仅在指定单个电子邮件地址时才起作用,但是,我需要让他们查询我的数据库并将电子邮件发送到每个电子邮件地址。我哪里出了问题 function sendmail($cat, $user) { require_once "Mail.php"; $elist = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = '
function sendmail($cat, $user) {
require_once "Mail.php";
$elist = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';");
$elist = mysql_fetch_array($elist);
$from = "EMAIL ADDRESS";
$to = $elist;
$subject = "SUBJECT";
$body = "BODY";
$host = "smtp.domain.com";
$username = "USERNAME";
$password = "PASSWORD";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
}
mysql\u fetch\u array
获取一个数组,该数组的条目对应于表中单个行的每一列。换句话说,这里的数组包含一个用户的cEmail
列
在调用邮件函数之前,需要将所有值提取到单个数组中。你可以这样做:
$dest = array();
while ($arr = mysql_fetch_array($elist)) {
$dest[] = $arr['cEmail'];
}
尝试类似的方法,但需要注意的一点是,您应该单独发送电子邮件,而不是将所有电子邮件地址分组在一个“收件人”字段中。其他用户可能不喜欢其他人看到这一点。可能smtp函数会破坏阵列,但不确定:-|
function sendmail($cat, $user)
{
require_once "Mail.php";
$elist = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';");
$from = "EMAIL ADDRESS";
$subject = "SUBJECT";
$body = "BODY";
$host = "smtp.domain.com";
$username = "USERNAME";
$password = "PASSWORD";
if(mysql_num_rows($elist) > 0)
{
while($elist_result = mysql_fetch_array($elist))
{
$headers = array ('From' => $from,
'To' => $elist_result['cEmail'],
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
}
}
}
我是从我正在学习PHP的书中的一个例子中学到这一点的,这本书的第一个例子是PHP和MySQL 您可以通过while循环单独但同时发送每封电子邮件。使用mysqli\u查询选择电子邮件,在我的
while ($row_data = mysqli_fetch_array($your_query_in_a_variable)) {
// Then you will set your variables for the e-mail using the data
// from the array.
$from = 'you@yoursite.com';
$to = $row_data['email']; // The column where your e-mail was stored.
$subject = 'Open Me!';
$msg = 'Hello world!';
mail($to, $msg, $from);
}
我希望这是清楚的。基本上,您只需在查询中调用所需的行,然后使用mysqli_fetch_数组,该数组在每次调用时都会遍历每一行(如果有人纠正我的错误),并在没有其他行被调用时退出。我的建议是使用php邮件()函数并在php.ini文件中设置smtp服务器参数 然后,您所要做的就是查询数据库,将第一条记录作为收件人地址取出,然后循环遍历结果集,并将每个电子邮件地址放入一个数组中。然后只需使用join函数用逗号连接数组,并将其用作头字符串的BCC 如果可能的话,我会用这个。我已经包括了一些注释作为注释
function sendmail($cat, $user) {
$result = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';");
//pull out first email as the to address
$to = mysql_fetch_array($result);
//create array to store all the emails
$elist = array();
//loop through and put each email into an array
// you might be able to skip this altogether if you can just join $result
// but I'm to lazy to investigate what all is being returned
while($row = mysql_fetch_assoc($result)){
$elist[] = $row['cEmail'];
}
//join the emails into a comma separated string
$bcc = join($elist, ",");
$from = "EMAIL ADDRESS";
$subject = "SUBJECT";
$body = "BODY";
$headers = "From: ". $from ."\r\n";
$headers .="BCC: ". $bcc ."\r\n";
mail($to, $subject, $body, $headers);
}
希望这是有用的信息。。您可以随意选择它,并使用您可能需要的任何东西,比如您可以使用part来实现Mail类。您可以而不是使用php
Mail()
函数。它非常慢,因为它会在您发送的每一封邮件中完全连接到smtp服务器。一个好的邮件库(如Zend_Mail)只连接一次,并将一批邮件发送到smtp服务器。
您还应该而不是通过密件抄送长线发送所有邮件。至少如果你希望你的电子邮件不被标记为垃圾邮件(见Jeff的问题和博客条目)
使用此代码解决您的问题
//Connect to database
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$sql = "SELECT email FROM members";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res) )
{
$area .= $row['email']. ", ";
}
// read the list of emails from the file.
$email_list = explode(',', $area);
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++)
{
$email_list[$counter] = trim($email_list[$counter]);
}
$to = $email_list;
echo $to;
//连接到数据库
mysql_connect(“localhost”、“user”、“password”)或die(mysql_error());
mysql_选择_db(“db”)或die(mysql_error());
$sql=“选择来自成员的电子邮件”;
$res=mysql\u query($sql)或die(mysql\u error());
while($row=mysql\u fetch\u assoc($res))
{
$area.=$row['email'].“,”;
}
//从文件中读取电子邮件列表。
$email_list=explode(“,”,$area);
//数一数有多少封电子邮件。
$total_email=count($email_list);
//浏览列表并修剪掉换行符。
对于($counter=0;$counter我在尝试循环遍历收件人的数据库表以向每个收件人发送个性化的PEAR电子邮件时遇到了类似的问题
我遇到的问题是,循环将在一次迭代后停止,直到我修改了$recipients
以只发送数组中的第一项,如此代码段所示。($recipients
是收件人的ID数组):
/*此处包含PEAR邮件设置代码*/
对于($i=0;$isend($recipients[0],$hdrs,$body);
}
@Downvoter:为什么?我认为这是一个很好的解决方案。我简洁准确地指出了给定代码的问题,并提出了一个解决方案。如果您不喜欢它发送一封电子邮件:正确的行为是将$dest
与BCC字段一起使用。这会导致对$smtp->send
函数的一次调用,但是t仍然对其他用户隐藏每个用户的地址。为什么要对您的100个用户中的每一个进行身份验证、建立全新的连接并重新验证SMTP服务器?我同意您的看法。其他示例没有错,但过于臃肿。将地址合并到一个BCC字段中对资源更为温和。它仍然没有发送电子邮件。我是按照上面的方式编写的。由于字符限制,我无法在注释中发布代码,因为此代码仅位于$headers变量位置上方:$to=$elist_result['cEmail'];PHPology-感谢您的帮助。仍然不起作用。我感觉fetch_数组正在捕获所有电子邮件地址并将它们放入“To”字段。但是,对于发送给多个收件人的电子邮件,需要用逗号分隔电子邮件地址。这将如何实现?@BigMike:内爆(',',$array);
将使用逗号加入数组。但是根据数组可以确定目的地。请查看我的答案。fetch\u array
不会在一次呼叫中获取所有电子邮件地址。Borealid-我想尝试您的答案。这将插入到我的代码中的什么位置?
//Connect to database
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$sql = "SELECT email FROM members";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res) )
{
$area .= $row['email']. ", ";
}
// read the list of emails from the file.
$email_list = explode(',', $area);
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++)
{
$email_list[$counter] = trim($email_list[$counter]);
}
$to = $email_list;
echo $to;
/*PEAR mail set-up code included here*/
for ($i = 0; $i < count($recipients); $i++) {
$sql_recipients = "SELECT * FROM $data_table WHERE id = $recipients[$i] ORDER BY id ASC ";
$res_recipients = mysqli_query($mysqli,$sql_recipients)
or die (mysqli_error($mysqli));
$recipients_info = mysqli_fetch_array($res_recipients);
$recip_id = $recipients_info['id'];
$recip_organisation = $recipients_info['organisation'];
$recip_fname = $recipients_info['fname'];
$recip_lname = $recipients_info['lname'];
$recip_email = $recipients_info['email'];
/*PEAR mail code here */
$recipients[0] = $recip_email;
$mail->send($recipients[0], $hdrs, $body);
}