如何将PHP邮件发送到通过MySQL查询接收的多个电子邮件地址


How to send PHP mailer to multiple email addresses received via MySQL query

我运行MySQL查询,从一个表中获取名字、姓氏和电子邮件,其中"notify"设置为YES,如下所示。在while循环中,我创建了所有信息,然后将其放入一个数组中:

$sql = "SELECT firstname, lastname, email, notify FROM guesses
WHERE poolid = '$poolid'
AND notify = 'yes'";
$getnotify = mysqli_query($connection, $sql);
if (!$getnotify) {
die("Database query failed: " . mysqli_error());
} else {
    while ($row = mysqli_fetch_array($getnotify)) {
        $notifyemailscontent.="'".$row['email'] . "' => '" . $row['firstname'] . " " . $row['lastname']. "',";
    }
}
$notifyemails=array($notifyemailscontent);

然后在PHP邮件代码中,我总是发送到我的地址,如下所示:

$mail->addAddress(myemail@myemail.com, 'Me');

但我也想将我在mysql查询中收到的电子邮件(无论是1还是100)添加为CC或BCC(两者都可以)。根据我在网上找到的东西,我试着在下面这样做,但它的行为并不像我想的那样。

foreach($notifyemails as $email => $name)
    {
       $mail->AddBCC($email, $name);
    }

注意:它会将电子邮件发送给我,但不会将电子邮件发送到BCC人员。当我打印$notifyemails数组时,我会得到以下内容(在这种情况下只有一封BCC电子邮件):

数组([0]=>'bjones@bobscompany.com'=>'Bob Jones',)

再次,我收到了电子邮件,但Bob没有收到。所以我认为上面的for循环或顶部的mysql查询循环中一定有问题???任何见解/指导都将不胜感激。

您的array构造不好,请使用以下内容:

while ($row = mysqli_fetch_array($getnotify)) {
     $notifyemailscontent[$row['email']] =  "{$row['firstname']}  {$row['lastname']}";
}

然后,在phpmailer块内:

foreach($notifyemailscontent as $email => $name)
{
   $mail->AddCC($email, $name);
}

如果有其他答案,我将提交以下内容

正如我在评论中所说,我们从他们的例子中提取了以下内容,并稍作修改,如果你想使用的话,还显示了这个人的名字。

您也可以添加到其中,使用不同的列,同时为它们分配变量。

借款来源https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps

注意:您将需要稍微修改查询/列名,可能还需要修改一些代码,因为您没有发布完整的代码。

以下是我使用的一个工作示例,希望它对您有用。

<?php 
include ('/path/to/database_connection.php');
error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->SMTPSecure = 'tls'; // if required
$mail->Port = 587; // or use the preferred port of your choice
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->addAddress('your_email@example.com', 'John');
$mail->setFrom('email@example.com', 'List manager');
$mail->addReplyTo('email@example.com', 'List manager');
$mail->Subject = "PHPMailer Simple database mailing list test";
//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you're using a templating system),
//set it inside the loop
//msgHTML also sets AltBody, but if you want a custom one, set it afterwards
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$result = mysqli_query($connection, "SELECT user, email FROM table WHERE col = 'x'");
    foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+
        $user = $row['user'];
        $body = "Hello $user, <br><br>This is the HTML message body <b>in bold!</b>";
        $mail->msgHTML($body);
        $mail->addBCC($row['email'], $row['user']);
    /*
        if (!empty($row['photo'])) {
            $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB
        }
    */
        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to:" . $row['user'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
            //Mark it as sent in the DB
    /* UPDATE the table if needed
            mysqli_query(
                $connection,
                "UPDATE mailinglist SET sent = true WHERE email = '" .
                mysqli_real_escape_string($connection, $row['email']) . "'"
            );
    */
        }
        // Clear all addresses and attachments for next loop
        $mail->clearAddresses();
        $mail->clearAttachments();
    }