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

16,613

Solution 1

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

Solution 2

I had the same problem. I just changed from:

EMAIL_USE_SSL = True

To:

EMAIL_USE_TLS = True
Share:
16,613
Roman Luštrik
Author by

Roman Luštrik

I'm an analyst with roots in veterinary medicine, biology/ecology and biostatistics. I work with data from various fields of natural (genetics, ecology, biotechnology...) and social sciences (e.g. official statistics, economy). Having fun with cloud solutions like AWS. My tool of choice is R, but I can also somewhat handle Python, HTML, CSS. Ask me about reproducible research and version control. I feed many, many cats.

Updated on July 18, 2022

Comments

  • Roman Luštrik
    Roman Luštrik almost 2 years

    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))
    
  • NYCeyes
    NYCeyes over 6 years
    Seems like yours was for a Django use-case. Correct?
  • Gregory
    Gregory over 6 years
    Yes, it is for a Django use case
  • Ami
    Ami over 5 years
    This is not useful for the question, because EMAIL_USE_SSL does not appear in the example code.
  • Gregory
    Gregory over 5 years
    vy32 this might be the missing configuration that needs to be added to solve the issue, and that was exactly what I had to add when I had the same issue.
  • Chase
    Chase over 5 years
    For anyone using Flask-Mail or Flask-Security, a variation of this answer fixes your SSL problem - MAIL_USE_TLS = True - The Flask-Security docs say to use MAIL_USE_SSL but you want TLS instead.
  • user4906240
    user4906240 almost 3 years
    i change it to email_use_tls true but still i am getting the same error