Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mail failed; [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:645)

This is a question about sending an email through an authenticated SMTP (not gmail). The below script was put together through various questions and answers on this site but I get an error that has no "googlable" candidates for this particular combination of tools. I'm working in Python 3.5.1 and it produces this error:

mail failed; [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:645)

Is this client side error or server? Am I missing some certificates I'm not aware of? AFAIK server supports SSL authentication. Any thoughts and nudges in the right direction will be appreciated.

import sys
from smtplib import SMTP_SSL as SMTP
from email.mime.text import MIMEText

# credentials masked, obviously
SMTPserver = 'myserver'
sender = 'mymail'
destination = ['recipient']

USERNAME = "myusername"
PASSWORD = "mypass"

# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'

content = """\
Test message
"""

subject = "Sent from Python"

try:
    msg = MIMEText(content, text_subtype)
    msg['Subject'] = subject
    msg['From'] = sender
    conn = SMTP(host=SMTPserver, port=465)
    conn.set_debuglevel(False)
    conn.login(USERNAME, PASSWORD)

    try:
        conn.sendmail(sender, destination, msg.as_string())
    finally:
        conn.quit()

except Exception as exc:
    sys.exit("mail failed; %s" % str(exc))
like image 317
Roman Luštrik Avatar asked Mar 26 '16 21:03

Roman Luštrik


2 Answers

Thanks to both commentators under my question. After navigating around SSL by setting

from smtplib import SMTP as SMTP 

and enabling TLS once the STMP object is created (forgive me if I'm not using the correct terminology)

conn = SMTP(host=SMTPserver, port=587)  # object created
conn.ehlo() 
conn.starttls()  # enable TLS
conn.ehlo()

I was able to send e-mail.

enter image description here

like image 143
Roman Luštrik Avatar answered Oct 07 '22 15:10

Roman Luštrik


I had the same problem. I just changed from:

EMAIL_USE_SSL = True

To:

EMAIL_USE_TLS = True
like image 41
Gregory Avatar answered Oct 07 '22 14:10

Gregory