Miksus/red-mail

ENH: Support non-TLS connections

Miksus opened this issue · 1 comments

Is your feature request related to a problem? Please describe.
The EmailSender could also support other than just TLS connection. Connecting to the SMTP server could be more customizable.

Describe the solution you'd like

Init could also take use_tls as an argument and the connection could be handled in a method to make subclassing convenient:

class EmailSender:
    ...
    def __init__(self, host:str, port:int, user_name:str=None, password:str=None, use_tls=True):
        self.host = host
        self.port = port
        self.use_tls = use_tls

...
    def send_message(self, msg):
        "Send the created message"
        server = self.connect()
        server.send_message(msg)
        
        server.quit() 

    def connect(self):
        "Connect to the SMTP server"
        user = self.user_name
        password = self.password
        
        server = self._cls_smtp_server(self.host, self.port)
        if self.use_tls:
            server.starttls()
        if user is not None or password is not None:
            server.login(user, password)
        return server

Also, the _cls_smtp_server could be public variable and meant to be changed.

Describe alternatives you've considered
There is no convenient alternative at the moment.

It seems starttls does not exactly mean TLS is used. To respect the terminology, the argument is probably better to be named as use_starttls (https://en.wikipedia.org/wiki/Opportunistic_TLS).