Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Zend\Mail doesn't work with Smtp transport via SSL connection .. #130

Closed
BnitoBzh opened this issue Oct 8, 2014 · 13 comments
Closed

Zend\Mail doesn't work with Smtp transport via SSL connection .. #130

BnitoBzh opened this issue Oct 8, 2014 · 13 comments
Labels

Comments

@BnitoBzh
Copy link
Contributor

BnitoBzh commented Oct 8, 2014

I use this module to send my emails in the background via Zend / Mail, but my SMTP server allows only SSL connections.
After a few minutes of inactivity to send email, the tail always returns the same error with a message "SSL broken pipe".
Impossible to miss even creating a Mailer Service on the fly, which returns me an error like this: "SSL operation failed with code 1 OpenSSL Error messages: error: 1409F07F: SSL routines: SSL3_WRITE_PENDING: bad write retry in ..."
I think this is due to the permanent php process, so that the mail function with Zend/Mail can not work with SlmQueue...

@juriansluiman
Copy link

Are you sure this is due to SlmQueue? Does it work when you do not use SlmQueue but directly invoke the email service inside the HTTP dispatch loop?

A possible cause is the difference in configuration for CLI and cgi/fpm/mod-php. They both hold different php.ini files and this can cause a mismatch between a CLI and non-CLI call.

@BnitoBzh
Copy link
Contributor Author

BnitoBzh commented Oct 8, 2014

I use the same Mailer module in my application without using SlmQueue and everything works.
I have not tested in cli mode, in a script that does not use SlmQueue, I'll take the test.
If it comes from the cli configuration you think that may be wrong? which part of the configuration?

@juriansluiman
Copy link

@BnitoBzh you can create a CLI route and dispatch a controller. There you invoke your mail service and test what is happening. You simply start the request the same as with SlmQueue:

php public/index.php [your-route-name]

What can be wrong in CLI mode is something with the ssl module in php. I have no idea since I have never run into this issue, but I have been using Soflomo\Mail (which is a service wrapper around Zend\Mail) with SlmQueue fine.

@BnitoBzh
Copy link
Contributor Author

BnitoBzh commented Oct 8, 2014

I created a test controller for a test on the command line. Emails go well, while in parallel emails always have the SSL problem through SlmQueue Job.
I think a timeout is to be achieved at SSL when passing by a Job after a long time of inactivity, because it is initialized only once at the launch of the queue.

@BnitoBzh
Copy link
Contributor Author

BnitoBzh commented Oct 8, 2014

I did the test with my own Mailer module and with the module Soflomo\Mail

@juriansluiman
Copy link

@BnitoBzh my concern is, SlmQueue does nothing with SSL. I think it's because two jobs are executed directly after each other and something in the HTTP traffic is bugging the handshake. I do not now everything about SSL, but you can perform a "short" handshake for a 2nd connection, so you don't need to negotiate for the complete process. You keep a kind-of session id and that is reused among further connections. I guess in this process the bug is hidden, so somehow in a second connection, php cannot handle your connection well.

  1. Are you injecting a shared instance of the transport? What happens if you do not share the transport?
  2. What if you retry the failed job? Not sure if that solves the problem, but it is always worth trying. Note you have to count the number of retries and quit anyhow when it still fails after (e.g.) 5 times.

@mstrychalski
Copy link

The problem is still not resolved and I have not been able to solve it yet. But we've tried to use swiftmailer instead of zend mail, and everything is working like a charm now. I suggest to switch to it if you have similar problem, and can switch library.

@roelvanduijnhoven
Copy link
Collaborator

Thanks for the update @mstrychalski. I'll close this for now though; because this repo does nothing with SSL or mail and I think the problem lies outside this repo

@MatthiasKuehneEllerhold
Copy link
Contributor

@mstrychalski This could be a problem with the maximum connection lifetime. The SMTP Server will close the connection if nothing is sent over it after a while. Even if you constantly send a ping or NOOP, there is a maximum connection lifetime setting for most servers. Meaning they'll close the connection regardless.

What you have to do with zend-mail is recreate the connection every so often, see my PR here: zendframework/zend-mail#172

We're using zend-mail together with SlmQueue and it works. You just have to guess (?) the connection timelimit of your SMTP server. It wasnt properly document in ours...

@roelvanduijnhoven
Copy link
Collaborator

Mm. What about introducing a section in this repo that gives some general hints about problems with long-connectivity @MatthiasKuehneEllerhold? We know this is also a problem with database connections 🤔.

If you like the idea; would you be willing to add a PR regarding the SSL connection to a section in the docs?

@MatthiasKuehneEllerhold
Copy link
Contributor

This problem is not specific to SSL connections because the same happens with HTTP connections.

The error message is different on a plain HTTP connection:

Notice: fwrite(): send of 6 bytes failed with errno=32 Broken pipe in ./zend-mail/src/Protocol/AbstractProtocol.php on line 255
Fatal error: Uncaught Zend\Mail\Protocol\Exception\RuntimeException: Could not read from 127.0.0.1 in ./zend-mail/src/Protocol/AbstractProtocol.php:301

But I'm not sure if its the same error that @mstrychalski describes. He switched libraries and the error was gone. I dont know if his new library has the auto-reconnect feature that zend-mail misses (until my PR is merged).

Adding some text to the documentation will help for sure. I'll try to whip up something and will need your input (english isnt my main language ;)).

@roelvanduijnhoven
Copy link
Collaborator

No problem @MatthiasKuehneEllerhold, mention me when you have something and I'll gladly give feedback :).

@roelvanduijnhoven
Copy link
Collaborator

Issue #177 is similar. Let's use that to keep track of this effort.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants