1

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

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
RaisoMos
  • 159
  • 4
  • 11
  • Alex's answer is spot on - but didn't it even occur to you to follow the link that was in the error message? It's right in front of you! – Synchro Jan 21 '16 at 17:12

1 Answers1

2

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?

Community
  • 1
  • 1
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
  • Would have made this a comment, but it doesn't fit. – Alex Tartan Jan 21 '16 at 10:47
  • how can i get the debug info after page reload ? the error appears sometimes not usually – RaisoMos Jan 21 '16 at 10:59
  • The output should be in `$m->ErrorInfo`. And you're already storing it in the `$_SESSION`; – Alex Tartan Jan 21 '16 at 11:02
  • got it, so if the debug is on, the `$m->ErrorInfo` contains the details of the error? – RaisoMos Jan 21 '16 at 11:05
  • yep! So you don't need to change anything else. – Alex Tartan Jan 21 '16 at 11:18
  • i made a loop on my personal emails adresse to not disturb costumers and i'm sending 37 email, i have no error until now – RaisoMos Jan 21 '16 at 11:20
  • If you have a gmail (don't know if others support it as well), you can add "username+something@gmail.com". The "+something" part is irrelevant in email routing but allows you to test with more email addresses. In your loop you can use `"yourEmail+".$i."@gmail.com";` – Alex Tartan Jan 21 '16 at 11:24
  • i tested 37 emails on 2 gmail, 1 yahoo and 1 outlook and there is no error, i tried on my localhost – RaisoMos Jan 21 '16 at 11:27
  • now i'm trying 97 emails on my hosting server with the same emails – RaisoMos Jan 21 '16 at 11:32
  • I've got the same error after successfully sent 61 emails on my hosting and 37 emails on localhost, here is the output of debug [http://justpaste.it/qqh4] – RaisoMos Jan 21 '16 at 11:45
  • Updated answer. Can you add a `sleep(2)` in your loop? This will add a small delay and maybe you will be able to authenticate to ovh again... – Alex Tartan Jan 21 '16 at 12:25
  • i'm using a setTimeout() jquery function 5s, i've got an automatic email saying that my email adresse was bloqued because i sent 111 emails, strange they sad once that there is no limit for sending emails, thank you for your help, i have to replay on my question to set solved or what ? – RaisoMos Jan 21 '16 at 13:08
  • Well, no need to reply to your own question. If you found this answer useful, you can mark it as correct and/or upvote it. – Alex Tartan Jan 21 '16 at 13:11