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: