我正在尝试制作一个简单的应用程序,将电子邮件发送到我数据库中的电子邮件地址。
我正在使用PHPMailer。此行无法使用:$mail->AddAddress($to, "Test Message");
仅当我键入电子邮件地址而不是$to
时,它才有效。但我需要向数据库中的每封电子邮件发送电子邮件。
if ((!empty($subject)) && (!empty($text))) {
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$mail->AddAddress($to, "Test Message");
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $to . '<br />';
}
mysqli_close($dbc);
}
答案 0 :(得分:0)
所以,只有PHPMailer
。不要混用PHPMailer
和PHP mail()
函数。
if ((!empty($subject)) && (!empty($text))) {
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query) or die('Error querying database.');
$mail = new PHPMailer();
$mail->Subject = $_POST['subject'];
while ($row = mysqli_fetch_array($result)) {
$mail->AddAddress($row["email"]);
$msg = "Dear " . $row['first_name'] . " "
. $row['last_name'] . " "
. "\n" . $_POST['elvismail'];
$mail->Body = $msg;
$mail->Send();
$mailer->ClearAllRecipients();
echo 'Email sent to: ' . $row["email"] . '<br />';
}
mysqli_close($dbc);
}