gpt4 book ai didi

php - 如何通过 phpmailer 在 PHP 和 MySQL 中发送多封电子邮件

转载 作者:行者123 更新时间:2023-11-29 01:17:50 25 4
gpt4 key购买 nike

我有一个问题。当我点击发送按钮时,只有 zxc@hotmail.com 收到邮件。

如何改变呢?我正在使用 Gmail SMTP 发送。

数据库中有2条记录:

这是我的代码:

include "phpmailer/class.phpmailer.php";
$host = "localhost";
$user = "root";
$pass = "root";
$db = "mailTest";

mysql_connect($host, $user, $pass);
mysql_select_db($db);

$query = "SELECT email FROM list";

$recordset = mysql_query($query);
$row_recordset = mysql_fetch_assoc($recordset);
$tota_row_recordset = mysql_num_rows($recordset);
$msg = strip_tags($_POST['msg']);
$mail= new PHPMailer(); //建立新物件
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail->AddAddress($to, "Test Message");
}
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->CharSet = "big5";
$mail->Subject = "Test Message";
$mail->Username = "xxxxxx";
$mail->Password = "xxxxxx";
$mail->Body = "$msg";
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: index.php');
}

最佳答案

在这个循环中,每次迭代都会覆盖收件人,因此只有最后一个人会留下来接收电子邮件:

while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
}
$mail = new PHPMailer();

将您的代码更改为

$mail = new PHPMailer(); // create object FIRST
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail->AddAddress($to, "Test Message"); // add each DB entry to list of recipients
}

关于php - 如何通过 phpmailer 在 PHP 和 MySQL 中发送多封电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com