views:

48

answers:

2

I am using Symfony + SwiftMailer and getting the following error from SwiftMailer:

500 | Internal Server Error | Swift_TransportException
Expected response code 220 but got code "", with message ""

and can't figure out why. I am using hMailServer on my local machine, and have setup my factories.yml file (as I'm using Symfony) as follows:

  mailer:
    class: sfMailer
    param:
      logging: %SF_LOGGING_ENABLED%
      charset: %SF_CHARSET%
      delivery_strategy: realtime
      transport:
        class: Swift_SmtpTransport
        param:
          host: splodge.loc
          port: 25
          encryption: ~
          username:   ~
          password:   ~

I'm using the following code to send:

$message = $this->getMailer()->compose(
    '[email protected]',
    '[email protected]',
    'Reset Password', 
    'Message'
);
$result = $this->getMailer()->send($message);

Has anyone had an issue like this before?

I have found that sometimes, and very randomly, the send goes through, but every other time, I get the above error message. So constantly refreshing the page that sends the mail, fails unpredictably, and as the code sometimes works, it's kinda hard to figure out what's wrong.

Any help will be highly appreciated!


Further investigation has shown that the SMTP server does send [220 localhost ESMTP], but after taking a look at hMailServer logs, I discovered the following:

"TCPIP" 5232    "2010-06-16 11:40:54.043"   "TCPConnection - Posting AcceptEx on 0.0.0.0:25"
"DEBUG" 5232    "2010-06-16 11:40:54.043"   "Creating session 51"
"SMTPD" 5232    51  "2010-06-16 11:40:54.043"   "127.0.0.1" "SENT: 220 localhost ESMTP"
"DEBUG" 5296    "2010-06-16 11:40:54.199"   "The read operation failed. Bytes transferred: 0 Remote IP: 127.0.0.1, Session: 51, Code: 10054, Message: An existing connection was forcibly closed by the remote host"
"DEBUG" 5296    "2010-06-16 11:40:54.199"   "Ending session 51"

Could it be that SwiftMailer is HELO/EHLOing too early, and that this is simply a time issue? Can I delay the HELO transmission a bit?

The good logs, when the thing actually sends looks as follows:

"TCPIP" 5232    "2010-06-16 11:42:21.278"   "TCPConnection - Posting AcceptEx on 0.0.0.0:25"
"DEBUG" 5232    "2010-06-16 11:42:21.294"   "Creating session 54"
"SMTPD" 5232    54  "2010-06-16 11:42:21.294"   "127.0.0.1" "SENT: 220 localhost ESMTP"
"SMTPD" 5224    54  "2010-06-16 11:42:21.294"   "127.0.0.1" "RECEIVED: EHLO splodge.loc"

So it has to be to do with the HELO/EHLO acknowledgement. Please advise! :)

+1  A: 

Sounds like an SMTP issue.

What happens if you try the following from the command-line:

telnet splodge.loc 25

Can you connect? Does the reply line start with 220?

If not, then you've got a problem with your SMTP server (or perhaps firewall) config.

timdev
When using telnet I get: [220 localhost ESMTP] each and every time. The code sometimes sends, sometimes, it doesn't. This is really weird.
Joe Zephyr
A: 

Managed to get this working by creating a loop that retries the mail sending...

$sendResult = false;
do
{
    try
    {
        $message = Swift_Message::newInstance()
            ->setFrom('[email protected]', 'Mr.Postman')
            ->setTo($to)
            ->setSubject('Password reminder...')
            ->setBody($this->getPartial('resetPasswordEmail', array(
                'newPassword' => $newPassword)), 'text/html');
        $sendResult = $this->getMailer()->send($message);
    }
    catch(Exception $e){/*Do nothing*/}
}
while($sendResult !== 1);

Next I'll add some config settings to limit the number of retries.

Joe Zephyr