我在提交表单后使用 PHPMailer 发送两封不同的电子邮件时遇到问题。问题是脚本回显“已创建”,但两封电子邮件均未发送。我正在使用 PHPMailer 库来处理电子邮件发送过程。这是我的代码:
require("connect.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
if (isset($_POST["full-name"])) {
require "PHPMailer/src/PHPMailer.php";
require "PHPMailer/src/SMTP.php";
require "PHPMailer/src/Exception.php";
$userFullName = $_POST["full-name"];
$userEmail = $_POST["email"];
$userPassword = $_POST["password"];
$userAddress = $_POST["address"];
$userType = $_POST["type"];
$userPhone = $_POST["phone"];
$date2 = date("j F Y h:i A");
$check_user = "SELECT * FROM `user_accounts` WHERE `EMAIL` = '$userEmail'";
$check_result = $conn->query($check_user);
$token = bin2hex(random_bytes(64));
$verificationLink = "**********************?token=$token"; // hidden for security purpose
if ($check_result->num_rows == 0) {
sendEmailToOwner($userFullName, $userEmail, $userType, $userAddress, $userPhone, $date2);
sendVerificationEmail($userFullName, $userEmail, $verificationLink, $conn, $userPassword, $userAddress, $userType, $userPhone, $token);
} else {
echo "User is already registered";
}
}
function sendEmailToOwner($userFullName, $userEmail, $userType, $userAddress, $userPhone, $date2)
{
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'valeexecutive.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '**********'; //webmail password
$mail->setFrom('[email protected]', 'Vale Executive Cars');
$mail->addAddress('[email protected]');
$mail->isHTML(true);
$mail->Subject = 'New Account Created';
$mail->Body = ''; // I removed the body for posting on stackoverflow for some purpose
$mail->send();
} catch (Exception $e) {
echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
function sendVerificationEmail($userFullName, $userEmail, $verificationLink, $conn, $userPassword, $userAddress, $userType, $userPhone, $token)
{
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'valeexecutive.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '********'; // webmail password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->isHTML(true);
$mail->setFrom('[email protected]', 'Vale Executive Cars');
$mail->addAddress($userEmail);
$mail->AddEmbeddedImage('images/logos/logo.png', 'logo', 'logo.png');
$mail->Subject = 'Account Verification';
$mail->Body = ''; // I removed the body for posting on stackoverflow for some purpose
if ($mail->send()) {
$date = date("Y-m-d H:i:s");
$query = "INSERT INTO `user_accounts`
(`FULL_NAME`, `EMAIL`, `PASSWORD`, `ADDRESS`,
`TYPE`, `PHONE`, `TOKEN`, `VERIFIED`,
`ACCOUNT_CREATED_AT`)
VALUES ('$userFullName', '$userEmail', '" .
md5($userPassword) . "', '$userAddress',
'$userType', '$userPhone', '$token', 0,
'$date')";
$result = mysqli_query($conn, $query);
if ($result === TRUE) {
echo "created";
} else {
echo "Error: " . mysqli_error($conn);
}
}
} catch (Exception $e) {
echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
问题: 提交表单后,我尝试发送两封不同的电子邮件:一封发送给所有者,另一封用于帐户验证。该脚本似乎有效,因为它回显“已创建”,表明 sendVerificationEmail 函数已成功执行。但是,这两封电子邮件都没有发送给各自的收件人。
我尝试过的: 我已确保正确包含必要的 PHPMailer 依赖项,并确认脚本成功到达 sendEmailToOwner 和 sendVerificationEmail 函数。我还通过使用 try-catch 块处理异常和打印错误消息来检查任何潜在的错误。
我的问题:
您是否阅读过这里关于 SO 的许多“PHP 邮件无法正常工作”问题?发送和接收电子邮件之间有很多步骤。
首先要检查的是电子邮件服务器上的日志。
服务器真的使用SMTPS吗?现在在端口 465 上已弃用此方法,其中 STARTTLS 是正确的方法。
您还可以考虑在 PHPmailer 中启用调试(您需要启用调试并注入 PSR-3 记录器)。