swiftmailer | Connection Timed Out

12 posts by 3 authors in: Forums > CMS Builder
Last Post: April 5, 2016   (RSS)

Hi,

This September i upgraded the swiftmailer to the latest version in order to make the smtp server work with SSL (forum post #2237408).

From this moment the ssl worked but very frequently i getting Connection Timed Out error but the email usually delivered (99%).

Now I try to make the newsletterBuilder work but with the Connection Timed Out error the procedure is cannot work, the cron job interrupt and the script try to resend the mail.

I talked with my host and after many days ticketing and researching on the problem and checking the server settings generally, the conclusion is that the problem is in the swiftmailer. (The server timout settings is 1day…).

I changed the smtp from ssl to unsecured with a verified login credentials to work through the port 25. I’m getting the same Connection Timed Out error each time where the system I trying to send mail but the mails at the end delivered.

Stackoverflow suggestion on the problem:

http://stackoverflow.com/questions/16528759/smtp-timeout-using-our-own-server-and-swiftmailer-how-can-i-increase-the-timeo

http://stackoverflow.com/questions/13257114/how-to-close-smtp-connection-in-swiftmailer?rq=1

 Please advise,
Karls

By ross - January 8, 2016

Hi Karls

Thanks for posting and sorry for the delay in getting back to you.

My first impression is you are looking for guidance on troubleshooting Swiftmailer.  Does your host have anymore feedback on why their server is timing out? Can they increase the timeout settings to see if more emails get through?

Let me know any questions or feedback.

Thanks.

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By Dave - January 11, 2016

Karls, 

Thanks for the report.  Can you copy and paste the exact error message you are getting?  And how many messages are you sending?  

A couple ideas, I'm wondering if CMSB is sending messages to your mail server faster than it can handle them.  We could test this by checking/logging the delay between attempting to send a message and getting the error.  I could add some code to the specific error returned to show how long it was trying to send the message for.

And if in fact it is a SwiftMailer issue we can work around it or update SwiftMailer, but ideally we'd want to get SwiftMailer updated.

Let me know some more details and we can look into this further.  Thanks!

Dave Edis - Senior Developer
interactivetools.com

By ht1080z - January 25, 2016

Hi Dave,

The error is:

Error: E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out

from the common.php line @trigger_error($e->getMessage(), E_USER_NOTICE);

My email settings:
SMTP server - unsecured connection
Host 127.0.0.1 port 25

Previously i had SSL settings but i cannot make the newsletterBuilder works at all with that, so i changed. Now i get very often this Timed Out error even when the mail is delivered after all.

Thank you,
Karls

By Dave - January 26, 2016

Thanks Karlz, 

Could you try adding this debugging code and then let me know what the new errors are? 

In /lib/common.php search for ### Send Message

Then update the lines in red (or just copy and paste the entire block of code): 

  ### Send Message
  $messagesSent     = 0;
  $failedRecipients = array();
  $errors           = '';
  $sendStartTime    = microtime(true);
  static $totalMessagesSent = 0;
  try {
    $transport->start();  // Catch and report authentication errors early
    $mailer       = Swift_Mailer::newInstance($transport);
    $messagesSent = $mailer->send($message, $failedRecipients);  // $messagesSent adds one for each address on TO: line, CC, BCC, address etc
    $totalMessagesSent += $messagesSent;
  }
  catch (Exception $e) { // catch errors
    $thisMessageTime = sprintf("%0.2f", microtime(true) - $sendStartTime);
    $totalScriptTime = sprintf("%0.2f", microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
    $error           = $e->getMessage() . " (send time: {$thisMessageTime}s, emails sent: {$messagesSent}, total emails sent: {$totalMessagesSent}, total script time: {$totalScriptTime}s)";
    @trigger_error($error, E_USER_NOTICE);   // log error
    $errors .= $e->getMessage() . "\n";      // return error
  }

  // Restore encoding


Let me know what the new errors say.  Thanks!

Dave Edis - Senior Developer
interactivetools.com

By ht1080z - February 19, 2016 - edited: February 19, 2016

Hi Dave,

Thank you for the debug script. I implemented 10days ago and here is some details errors now:

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (send time: 4.01s, emails sent: 0, total emails sent: 3, total script time: 32.76s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (send time: 4.01s, emails sent: 0, total emails sent: 2, total script time: 27.38s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (send time: 4.02s, emails sent: 0, total emails sent: 0, total script time: 14.18s)

E_USER_NOTICE: Expected response code 250 but got code "552", with message "552 Mail rejected by the processing module: Requested action not taken: mailbox unavailable
" (send time: 1.46s, emails sent: 0, total emails sent: 0, total script time: 2.26s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (send time: 4.02s, emails sent: 0, total emails sent: 0, total script time: 9.15s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (send time: 4.01s, emails sent: 0, total emails sent: 7, total script time: 21.52s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (send time: 4.01s, emails sent: 0, total emails sent: 1, total script time: 5.82s)

The errors is always have Filepath /lib/common.php nad line: 1304 (@trigger_error($error, E_USER_NOTICE);)

SMTP settings are attached in screenshot.

Mails are sent in the cases of 99% but errors always triggered.
Unfortunately the newsletterBuilder cannot work with this errors at all.

Thank you,
Karls

Attachments:

SMTP_settings.jpg 78K

By Dave - February 22, 2016

Hi Karlz, 

Do you know what software is used by the SMTP server? 

I've added some more details to the error report message and added the code suggested on stackoverflow.  Can you try this: 

  ### Send Message
  $messagesSent     = 0;
  $failedRecipients = array();
  $errors           = '';
  $sendStartTime    = microtime(true);
  static $totalMessagesSent = 0;
  static $iterations = 0;
  try {
    $iterations++;
    $transport->start();  // Catch and report authentication errors early
    $mailer       = Swift_Mailer::newInstance($transport);
    $messagesSent = $mailer->send($message, $failedRecipients);  // $messagesSent adds one for each address on TO: line, CC, BCC, address etc
    $totalMessagesSent += $messagesSent;
    $mailer->getTransport()->stop(); // http://stackoverflow.com/a/17110512
  }
  catch (Exception $e) { // catch errors
    $mailer->getTransport()->stop(); // http://stackoverflow.com/a/22629213
    $thisMessageTime   = sprintf("%0.2f", microtime(true) - $sendStartTime);
    $totalScriptTime   = sprintf("%0.2f", microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
    $error             = $e->getMessage() . " (total iterations: $iterations, send time: {$thisMessageTime}s, emails sent: {$messagesSent}, total emails sent: {$totalMessagesSent}, total script time: {$totalScriptTime}s)";
    @trigger_error($error, E_USER_NOTICE);   // log error
    $errors .= $e->getMessage() . "\n";      // return error
  }

I still suspect it might be related to the SMTP server, that they're throttling certain connections but we'll keep looking.

Do you know if there's a pattern in the email hostnames that get timeouts?  For example, are they all invalid emails?  Or @aol emails, etc? 

Let me know what you find, thanks!

Dave Edis - Senior Developer
interactivetools.com

Hi Dave,

Thank you for following this up. It is really good to know that i'm not alone with the problem.

I implemented the changes in the common.php and did 2 short test sending mails (test mail from the email templates & password reset request). Strangely i had no new generated error.

Maybe the new $mailer->getTransport()->stop(); function make some difference...

I'll leave the server for a day or two and checking and posting back again later with results.

The server is nginx and AXIGEN Mail-Server 9.0.0 and it is dedicated for only for this project with one cmsBuilder installation.  If you want i can send access to the mail server settings to your mail.

Thank you again,Karls

Hi Dave,

Here is some new records from the error log after  i updated the debug code you sent me.

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (total iterations: 1, send time: 4.16s, emails sent: 0, total emails sent: 0, total script time: 4.26s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (total iterations: 1, send time: 8.03s, emails sent: 0, total emails sent: 0, total script time: 8.06s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (total iterations: 2, send time: 4.15s, emails sent: 0, total emails sent: 1, total script time: 5.99s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (total iterations: 2, send time: 4.94s, emails sent: 0, total emails sent: 1, total script time: 6.56s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (total iterations: 4, send time: 8.01s, emails sent: 0, total emails sent: 2, total script time: 22.06s)

E_USER_NOTICE: Connection to 127.0.0.1:25 Timed Out (total iterations: 2, send time: 5.67s, emails sent: 0, total emails sent: 1, total script time: 12.06s)

Regards,
Karls