我运行一个MySQL查询,从表格中获取名字,姓氏和电子邮件,其中包含'通知'设置为YES,如下所示。在while循环中,我创建了所有信息,然后我将其放入数组中:
180 180
然后在PHP邮件代码中,我总是发送到我的地址,如下:
$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);
但是我还想将我在mysql查询中获得的电子邮件(无论是1还是100)添加为CC或BCC(两者都可以)。我尝试在下面这样做,基于我在网上找到的东西,但它没有按照我的意愿行事。
$mail->addAddress(myemail@myemail.com, 'Me');
注意:它正在向ME发送电子邮件,但它没有向BCC人员发送电子邮件。当我打印$ notifyemails数组时,我得到以下内容(在这种情况下只有一封BCC电子邮件):
数组([0] =>' bjones@bobscompany.com' =>' Bob Jones',)
再一次,我收到了电子邮件,但鲍勃并不是BCC' d。所以我认为在上面的for循环中或者可能在顶部的mysql查询循环中有些错误?任何见解/方向都将不胜感激。
答案 0 :(得分:2)
您的array
构造不正确,请使用以下内容:
while ($row = mysqli_fetch_array($getnotify)) {
$notifyemailscontent[$row['email']] = "{$row['firstname']} {$row['lastname']}";
}
然后,在phpmailer
区块内:
foreach($notifyemailscontent as $email => $name)
{
$mail->AddCC($email, $name);
}
答案 1 :(得分:2)
鉴于其他答案,我提交以下内容。
正如我在评论中所述,已经从他们的示例中提取以下内容并进行了略微修改,以便在您想要使用该名称的同时显示此人的姓名。
您也可以在为其分配变量时使用不同的列添加它。
借鉴https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps
N.B。:您需要稍微修改一下查询/列名称,可能还需要修改一些代码,因为您没有发布完整的代码。
以下是我所使用的工作示例,希望它能为您提供良好的服务。
<?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("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
} else {
echo "Message sent to:" . $row['user'] . ' (' . str_replace("@", "@", $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();
}