如何从数据库查询(PHP)向多个收件人发送电子邮件

时间:2010-07-12 05:50:11

标签: php email

我正在尝试向我的数据库中的多个电子邮件地址发送电子邮件。这是我目前的代码。它仅在我指定单个电子邮件地址时才起作用,但是,我需要让他们查询我的数据库并将电子邮件发送到每个电子邮件地址。我在哪里错了?

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

7 个答案:

答案 0 :(得分:6)

尝试类似这样的事情,但需要注意的一点是,您应该单独发送电子邮件,而不是将所有电子邮件地址分组到一个“收件人”字段中。其他用户可能不喜欢其他人看到这一点。也许你的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); 
            }
        }
 } 

答案 1 :(得分:1)

mysql_fetch_array获取一个数组,其中的条目对应于表格的单个行的每一列。换句话说,这是一个包含一个用户的cEmail列的数组。

在调用邮件函数之前,您需要将所有值提取到单个数组中。你可以这样做:

$dest = array();
while ($arr = mysql_fetch_array($elist)) {
   $dest[] = $arr['cEmail'];
}

答案 2 :(得分:1)

我尝试循环访问收件人的数据库表时遇到了类似的问题,目的是向每个收件人发送个性化的PEAR电子邮件。

我遇到的问题是循环会在一次迭代后停止,直到我将$recipients修改为只发送数组中的第一项,如此代码片段所示。 ($recipients是收件人的ID数组):

/*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);

}

答案 3 :(得分:0)

我从本书的一个例子中学到了这一点,我正在学习PHP, Head First PHP&amp; MySQL的

您可以做的是单独发送每封电子邮件,但同时通过while循环发送。使用 mysqli_query 选择电子邮件,制作一个通过我的

的循环
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_array ,每次调用时都会遍历每一行(有人纠正我,如果错误),并且在没有更多行时将退出离开那里被叫了。

答案 4 :(得分:0)

我的建议是使用php mail()函数并在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类。

答案 5 :(得分:0)

使用php mail()函数。它很慢,因为它会在您发送的每封邮件中完全连接到smtp服务器 一个好的邮件库,如Zend_Mail只连接一次,并将一批邮件发送到smtp服务器。
您还应通过长BCC-Line发送所有邮件。至少如果您希望自己的电子邮件不被标记为垃圾邮件(请参阅this问题和杰夫的this博客条目)。

<?php
function sendmail($cat, $user) {
    // Setup mailer
    require_once "Mail.php";

    $smtp = Mail::factory('smtp', array(
        'host'     => "smtp.domain.com";
        'auth'     => true,
        'username' => "USERNAME";
        'password' => "PASSWORD";
    ));

    // ALWAYS escape your variables!
    $query = sprintf("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%s'",
            mysql_real_escape_string($cat),

    // submit the query
    $result = mysql_query($query);

    // iterate over each row
    while ($row = mysql_fetch_assoc($result)) {
        $from    = "EMAIL ADDRESS";
        $to      = $row['cEmail'];
        $subject = "SUBJECT";
        $body    = "BODY";

        $headers = array(
            'From'    => $from,
            'To'      => $to,
            'Subject' => $subject
        );

        // Send mail with individual to-address via smtp 
        $mail = $smtp->send($to, $headers, $body);
    }
}

答案 6 :(得分:0)