openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.88k stars 3.59k forks source link

[mail] SMTP thing status not reliable #17337

Open clinique opened 2 months ago

clinique commented 2 months ago

From the tests I made, it appears that SMTP thing status is ONLINE while credentials are tested only upon expedition of a mail.

lsiepel commented 2 months ago

AFAICT there is no way to validate user/pass before actually sending a mail. So the best thing that can be done is set the thing to UNKOWN at startup. If that is all you require i can make a PR, but it is hardly changing anything to the binding behaviour.

clinique commented 2 months ago

Yes, I imagined that it was the pitfall. I think it would be better to leave it UNKNOWN until the connection is definitively known as working.

J-N-K commented 2 months ago

I know that this is currently not implemented, but IMO a thing that is not ONLINE should not receive commands from the framework (similar to UNINITIALIZED). This would also greatly simplify binding development, because connection state and similar would not need to be checked when processing a command. So I would vote against changing it.

Instead we could add code like https://stackoverflow.com/questions/49867860/java-mail-api-credentials-verification to initialize() and set the thing to OFFLINE.

lsiepel commented 2 months ago

Even if that would be implemented I would expect that an UNKNOWN state would allow you to send commands.

However, the link you provided may be a good solution. Busy with some other PR’s but might come back to this.

jimtng commented 2 months ago

a thing that is not ONLINE should not receive commands from the framework (similar to UNINITIALIZED)

+1

It would be weird to try to send an email when the thing is not online.

I would say implement an optional (!!) credential check at the beginning if you need such feature. Beware that repeated authentications, especially incorrect ones, could get you banned from the mail server if it has an overzealous security, e.g. fail2ban or something. And this could happen if one were to restart openhab too often. So please make this an optional config.

lsiepel commented 2 months ago

a thing that is not ONLINE should not receive commands from the framework (similar to UNINITIALIZED)

+1

It would be weird to try to send an email when the thing is not online.

I would say implement an optional (!!) credential check at the beginning if you need such feature. Beware that repeated authentications, especially incorrect ones, could get you banned from the mail server if it has an overzealous security, e.g. fail2ban or something. And this could happen if one were to restart openhab too often. So please make this an optional config.

Why is it weird to try to send a mail while the thing status is UNKOWN?

Checking the credentials on binding start and set the thing state accordingly would be good great. Don't see why this should be optional, as under what circumstances would one restart the binding that often to get a ban?

While coding a rule that sends mails, a mistake is easily made, never seen a user complain about a ban. This mechanism would even protect you for those mistakes. Or what am i missing?

J-N-K commented 2 months ago

Unfortunately it's not that easy. Imagine you have a power outage. openHAB might be faster in restarting than your internet router, so the credentials check will fail because there is no connection to the server. if you never try to re-connect, you'll always stay in OFFLINE mode.

geert-claes commented 1 month ago

@lsiepel Isn't it possible to capture the response of the SMTP server ? In case of a wrong username/password it would be 535 5.7.8 Authentication credentials invalid. during the handschake with the SMPT Server ? The response could be logged. @J-N-K If there is no internet access, a connection to the SMTP server will not open. Therefore the test loops (every x seconds) until it can. Until succesfull opening a connection the status is offline.