Search code examples
phpphpmailerovh

PHPMailer smtp connect() failed on ovh server


I'm using PHPMailer to send daily emailing (max 100 emails), my script works fine on local, but when i uploaded it to my server (Hosted on OVH) sometimes it blocks and generate this error after sending an average of 20 emails

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

I contacted OVH and they sad that there is no problems with my email adresse so i think it is a bug on my code, here is my code:

$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;
}

The list of emails is stored in a session to loop on with the id, i get the id from url using mvc structure, after executing this code, i render a view wich display list of sent email, and if there is a error i echo out the email adresse with the error, the after 5 seconds i redirect to the same page with next id using jquery. here is a picture of the output: enter image description here


Solution

  • As the docs point out:

    "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).

    So my advice is to set:

    $m->SMTPDebug = 4; 
    

    to get a more detailed message and change back to 0 once you figure out the cause.

    Post the extended debug info here, so we can further look into it.

    Debug info (from comment - removed timestamp):

    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

    So, apparently, ovh is not authenticating you. Perhaps ovh is somehow rate-limiting you?