phpphpmailer

SMTP connect() failed error after 70 successful sends


A number of people had shown similar question, using phpmailer with g-mail. This problem is slightly different and may need a different answer.

The phpmailer code is fairly standard (below). I have a loop that goes through some data in the table and builds customised messages for recipients, sending a message for each row in the table. When the message is sent, it successfully completes about 75 out of some 100 recipients, and for the last 25, it reports the SMTP connect() failed error. This doesn't happen every time, and occasionally, the script goes through the loop without errors.

<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 0; // enables SMTP debug information (for testing)
$mail->SMTPsecure = "ssl";
$mail->SMTPAuth   = true; // enable SMTP authentication
$mail->Host       = "smtp.gmail.com"; // sets the SMTP server
$mail->Port       = 587;  // set the SMTP port for the GMAIL server
$mail->Username   = "xx"; // SMTP account username
$mail->Password   = "xx"; // SMTP account password 
$mail->SetFrom("xx@gmail.com");
$mail->isHTML(true);
$mail->CharSet = "utf-8";
$mail->Subject = $msgsubject;
$mail->AltBody = "Invitation";
  do {
    $membername = $row_musicians['name'];
    $membernumber = $row_musicians['number'];
    $emailaddress = $row_musicians['email'];
    $rhdate = $row_musicians['rhdate'];

$mail->clearAttachments();
$mail->Body = 'Dear '.$membername.',
  <p>'.$maintext.'</p>
  ';
// some more text using custom variables from database
// closing part of the message
$mail->Body .= '<p>'.nl2br($closing, false).'</p>
</body>
</html>';
 $mail->AddAddress($emailaddress, $membername); // sending each message
  sleep(1); // wait one second between sends, to avoid spam filters
 echo '<br />
 Recipient: '.$membername.' ('.$emailaddress.') -- ';   
 if(!$mail->Send()) {
        echo "Mailer Error: <div style='color:#009999'>" . $mail->ErrorInfo."        </div>";
        } else {
              echo "Message sent!";
}
  $mail->ClearAddresses();
    } while ($row_musicians = mysql_fetch_assoc($musicians));
mysql_free_result($musicians);
?>

Solution

  • I recommend basing your code on the mailing list example provided with PHPMailer.

    You're doing most things right - initialising PHPMailer first and setting properties that are common to all messages, but one key thing is missing:

    $mail->SMTPKeepAlive = true;
    

    Without this it means that it closes and reopens a connection for every message, and you're mostly likely running into connection rate limits.

    you're also using the combination of Port= 587 and SMTPSecure = 'ssl' which will generally not work; switch to Port = 465.

    I'd also recommend against the do/while loop - it will fail if your database query fails to find anything because it's bottom-tested and thus will always run once, even if there is no data. Use a while or foreach loop instead.

    The mailing list example does all of these things already.

    Final thing - if this is all your code, it looks like you're running an old version of PHPMailer, so get the latest.