下面是我的代码,用于在用户提交请求后向审批者发送电子邮件。电子邮件将首次发送给审批者,但是,在将多封电子邮件发送给审批者后,我收到“无法连接到 SMTP 主机”错误。
$resAdd = sqlsrv_query($conn, $queAdd) or die(print_r(sqlsrv_errors()));
if ($resAdd) {
// passing true in constructor enables exceptions in PHPMailer
$mail = new PHPMailer(true);
$emailBody = "This email is for testing only. Please ignore. Please check the link document. <a href = 'http://localhost/einv/v1/viewDet.php?variable=$refNum'><label>Link</label></a>";
// Server settings
$mail->SMTPDebug = 2; // for detailed debug output
$mail->isSMTP();
$mail->Host = 'smtp-mail.outlook.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 25;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Username = '[email protected]';
$mail->Password = 'abc134';
// Sender and recipient settings
$mail->setFrom('[email protected]', 'e-Inv Notification', 0);
$mail->addAddress('[email protected]');
// Setting the email content
$mail->IsHTML(true);
$mail->Subject = "[FOR TESTING, PLEASE IGNORE] Pending Approval : $refNum";
$mail->Body = html_entity_decode($msg_body);
$mail->Body = $emailBody;
$mail->Debugoutput = 'html';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . print_r(error_get_last(), true);
} else {
echo 'Message has been sent';
$mail->smtpClose();
}
echo "<script type=text/javascript>";
echo "alert('Your request submitted successfully.'),location.href='../viewDet.php?variable=$refNum'";
echo "</script>";
}
还有其他问题可能导致此错误吗?
我尝试了各种解决方案,但都不起作用。我尝试过的一些解决方案:
Outlook 具有不同的 SMTP 设置,通常会阻止任何 phpmail 请求。如果您希望 phpmail 连接到 Outlook SMTP,您必须更改 SMTP 设置以允许授权 From: 邮件程序。 (您可以在 Outlook 设置本身中进行点细化)
或者,您可以依靠不使用 phpmailer 的第三方库来实现此目的。