$row['email']无法从SELECT语句取值至PHPMailer收件人字段
问题修复:数据库查询结果无法传入PHPMailer收件人字段
核心问题分析
- $row变量生命周期失效:你在while循环外赋值
$var_email = $row["email"],但while循环结束后,$row已经是false(fetch_assoc()遍历完结果后返回false),这个赋值根本拿不到有效邮箱地址。而且因为用了LIMIT 1,完全不需要while循环,直接一次fetch就能拿到数据。 - 变量作用域问题:
$var_email是全局变量,但SendMail类的send方法属于局部作用域,默认无法访问全局变量,直接使用会导致变量未定义。
修复方案
步骤1:正确获取并保存邮箱地址
去掉多余的while循环,直接获取单条查询结果,将邮箱地址存入$var_email:
$sql = "SELECT email FROM emails WHERE status = 'new' LIMIT 1"; $result = $conn->query($sql); $var_email = ''; if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $var_email = $row["email"]; echo "获取到邮箱: " . $var_email; } else { echo "0 results"; // 没有找到邮箱时直接终止程序,避免后续报错 exit; }
步骤2:让SendMail类的send方法接收邮箱参数
修改SendMail类的send方法,添加参数接收邮箱地址:
Class SendMail{ // 接收邮箱参数 function send($recipient_email){ $mail = new PHPMailer(true); // ... 其他服务器设置保持不变 ... //Recipients $mail->setFrom('info@####', '####'); // 使用传入的参数作为收件人 $mail->addAddress($recipient_email); // ... 其他内容设置保持不变 ... $mail->send(); echo 'Message has been sent'; } }
步骤3:调用send方法时传入邮箱变量
$sendMail = new SendMail(); $sendMail->send($var_email);
完整修复后代码
<?php $servername = "localhost"; $username = "####"; $password = "####"; $dbname = "####"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully<br>"; $sql = "SELECT email FROM emails WHERE status = 'new' LIMIT 1"; $result = $conn->query($sql); $var_email = ''; if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $var_email = $row["email"]; echo "获取到邮箱: " . $var_email . "<br>"; } else { echo "0 results<br>"; exit; } $sql = "UPDATE emails SET status='done' WHERE status = 'new' LIMIT 1"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully<br>"; } else { echo "Error updating record: " . $conn->error . "<br>"; } use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; //Load Composer's autoloader require 'vendor/autoload.php'; Class SendMail{ function send($recipient_email){ $mail = new PHPMailer(true); //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = '####'; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = '####'; //SMTP username $mail->Password = '####'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption $mail->Port = 465; //Recipients $mail->setFrom('info@####', '####'); $mail->addAddress($recipient_email); //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = '####'; $mail->Body = "####"; $mail->send(); echo 'Message has been sent'; } } $sendMail = new SendMail(); $sendMail->send($var_email); $conn->close(); ?>
额外注意事项
- 建议在没有获取到有效邮箱时直接终止程序(用
exit),避免后续PHPMailer因空收件人报错。 - 尽量避免使用全局变量传递数据,通过方法参数传递是更规范的做法,也能减少作用域问题。
内容的提问来源于stack exchange,提问作者jim
相关产品推荐
相关产品推荐

