The SMTP class also has the capacity to manage authentication and TLS encryption. First, we need to determine whether the server supports TLS encryption. To do this, we can use the ehlo() method to identify our computer to the server and query what extensions are available. Then, we can call has_extn() to check the results. Once TLS is started, you must call ehlo() again to re-identify yourself over TLS connection.
If you want to do SMTP authentication with TLS instead of SSL, you simply have to change the port to 587 and execute smtp.starttls() in the following way:
smtp.connect('smtp.mail.server', 587)
smtp.ehlo()
if smtp.has_extn('STARTTLS'):
smtp.starttls()
smtp.ehlo()
smtp.login('user@domain', 'password')
In this section we have reviewed how we can manage authentication and TLS encryption.