Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)?

I am trying to send an email with python, but it keeps saying ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056). Here is my code:

server = smtplib.SMTP_SSL('smtp.mail.com', 587) server.login("[email protected]", "password") server.sendmail( "[email protected]",  "[email protected]",  "email text") server.quit() 

Do you know what is wrong?

like image 570
TheRealTengri Avatar asked Aug 29 '19 17:08

TheRealTengri


1 Answers

The port for SSL is 465 and not 587, however when I used SSL the mail arrived to the junk mail.

For me the thing that worked was to use TLS over regular SMTP instead of SMTP_SSL.

Note that this is a secure method as TLS is also a cryptographic protocol (not unlike SSL).

import smtplib, ssl  port = 587  # For starttls smtp_server = "smtp.gmail.com" sender_email = "[email protected]" receiver_email = "[email protected]" password = input("Type your password and press enter:") message = """\ Subject: Hi there  This message is sent from Python."""  context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server:     server.ehlo()  # Can be omitted     server.starttls(context=context)     server.ehlo()  # Can be omitted     server.login(sender_email, password)     server.sendmail(sender_email, receiver_email, message) 

provided thanks to the real python tutorial

like image 173
moshevi Avatar answered Sep 25 '22 14:09

moshevi