PHPMailer smtp connect() 在 ovh 服务器上失败

PHPMailer smtp connect() failed on ovh server

我正在使用 PHPMailer 每天发送电子邮件(最多 100 封电子邮件),我的脚本在本地运行良好,但是当我将它上传到我的服务器(托管在 OVH 上)时,有时它会阻止并在发送邮件后生成此错误平均 20 封电子邮件

SMTP connect() failed https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我联系了 OVH,他们很遗憾我的电子邮件地址没有问题,所以我认为这是我的代码中的错误,这是我的代码:

$m = new PHPMailer;
$mail = new Mailing();
$admin = new Administrateur();

$m->isSMTP();
$m->IsHTML(true); 
$m->SMTPAuth = true;
$m->SMTPDebug = 0;
$m->SMTPKeepAlive = true;
$m->Host = 'ssl0.ovh.net';
$m->Username = 'exemple@exemple.com';
$m->Password = 'xxxxxxxxxxx';
$m->SMTPSecure = 'ssl';
$m->Port = "465";
$m->CharSet = 'UTF-8';
$m->From = 'exemple@exemple.com';
$m->FromName = 'Chantier TN';
$m->addReplyTo('exemple@exemple.com', 'Reply adress');
$m->Subject = 'Alerte quotidienne';

$error = false;
$alertes = Session::get('alertes');
$date = date('Y-m-d h:i:s');
$token = $alertes[$id]['id_cli'].'-'.strtotime($date);

$m->addAddress($alertes[$id]['email'], strtoupper($alertes[$id]['nom']).' '.$alertes[$id]['prenom']);

Session::delete('send');
$m->Body = $mail->generateBodyForAlerte(strtoupper($alertes[$id]['nom']).' '.$alertes[$id]['prenom'], $alertes[$id]['leads'], $token);
if ($m->send()) {
    $mail->set('type_mail', 1);
    $mail->set('date_send', $date);
    $mail->set('id_cli', $alertes[$id]['id_cli']);
    $mail->set('id_op', $admin->getId());
    $mail->set('content', Session::get('send'));
    $mail->save();
    $s = Session::exists('sent') ? Session::get('sent') : array();
    $s[] = $alertes[$id]['email'];
    Session::put('sent', $s);
}
else{
    $error = $m->ErrorInfo;
    $s = Session::exists('notsend') ? Session::get('notsend') : array();
    $s[] = $alertes[$id]['email'].' error: '.$error;
    Session::put('notsend', $s);
}
$m->clearAddresses();

if (!isset($_SESSION['alertes'][$id+1])) {
    $next = false;
}
else{
    $next = $id+1;
}

电子邮件列表存储在会话中以使用 id 循环,我使用 mvc 结构从 url 获取 id,执行此代码后,我呈现一个显示已发送电子邮件列表的视图,如果出现错误,我会回显错误的电子邮件地址,5 秒后,我会使用 jquery 重定向到具有下一个 ID 的同一页面。 这是输出的图片:

正如 docs 指出的那样:

"SMTP Error: Could not connect to SMTP host."

This may also appear as SMTP connect() failed or Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).

所以我的建议是设置:

$m->SMTPDebug = 4; 

获取更详细的消息,并在找出原因后改回 0

Post这里是扩展的调试信息,所以我们可以进一步查看它。

调试信息(来自评论 - 删除时间戳):

SERVER -> CLIENT: 250-ns0.ovh.net You connect to mail751 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-8BITMIME 250 SIZE 109000000
Auth method requested: UNKNOWN
Auth methods available on the server: LOGIN,PLAIN
Auth method selected: LOGIN
CLIENT -> SERVER: AUTH LOGIN
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "334 VXNlcm5hbWU6 "
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: bm9yZXBseUBjaGFudGllci50bg==
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "334 UGFzc3dvcmQ6 "
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: Y2luMjM0NjQ4ODQ=
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "535 authorization failed (#5.7.0) "
SERVER -> CLIENT: 535 authorization failed (#5.7.0)
SMTP ERROR: Password command failed: 535 authorization failed (#5.7.0)
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "221 ns0.ovh.net You connect to mail751 "
SERVER -> CLIENT: 221 ns0.ovh.net You connect to mail751
Connection: closed

所以,显然,ovh 没有对您进行身份验证。也许 ovh 不知何故 rate-limiting 你?