Suggestion: Support 'TLS optional' usage
The current API requires users to specify whether they want TLS on connect, or STARTTLS, or no TLS. In the first 2 cases, attempting to use TLS with a destination server lacking TLS support, the library crashes out with an exception, leaving an unreturned collection lock.
This is a suggestion to add a keyword parameter to support a behaviour of "please try to connect by TLS, but if that fails, just connect in plaintext" instead.
With the current library, I first have to attempt connection via TLS, and if that crashes out, I have to instantiate a new SMTP object with TLS disabled, and connect with that. This works, but it slows down the cycle a bit and feels messy. It would be nicer to be able to set the aiosmtplib package to act with leniency.
FYI for just STARTTLS you can use something like this to avoid reconnecting:
async with SMTP(hostip, source_address=local_fqdn) as client:
try:
await client.starttls(validate_certs=False) # opportunistic TLS
except SMTPException as exc:
if 'starttls extension not supported' not in exc.message.lower():
raise
return await client.send_message(message)
For SMTP over TLS, I don't think there's any alternative but to try and catch (even if it were implemented by aiosmtplib, internally it would have to do the same thing).
The newest beta (v2.0.0b0) improves this situation slightly by checking for STARTTLS support and upgrading the connection automatically if it's present by default.
The TLS on connect situation isn't really improved though. The lower level connect APIs don't really make it easy to handle TLS connections transparently, unfortunately.
Closing as partially fixed