Reputation: 1361
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?
Upvotes: 89
Views: 320292
Reputation: 111
This worked for me.
with smtplib.SMTP(os.getenv("EMAIL_HOST"), int(os.getenv("EMAIL_PORT"))) as server:
server.ehlo()
server.starttls(context=ssl.create_default_context())
server.ehlo()
server.login(from_addr, os.getenv("SENDER_MAILID_PASSWORD"))
server.sendmail(from_addr, to_addrs + bcc_addrs + cc_addrs, mail.as_string())
Replaced smtplib.SMTP_SSL with smtplib.SMTP worked for me
Upvotes: 0
Reputation: 174
I had a similar error when I was using SSL on port 587 because the service provider is not enabling port port 465 on SSL. If I used TLS on port 587, I was getting another error 550, b'Administrative prohibition - envelope blocked. Then I realized in my code I didn't change the default from email to be the correct one. After doing this, I was able to send an email on port 587 and using TLS.
Upvotes: 0
Reputation: 1295
this is how i solved same problem
import ssl
sender = "[email protected]"
password = "password123"
where_to_email = "[email protected]"
theme = "this is subject"
message = "this is your message, say hi to reciever"
sender_password = password
session = smtplib.SMTP_SSL('smtp.yandex.ru', 465)
session.login(sender, sender_password)
msg = f'From: {sender}\r\nTo: {where_to_email}\r\nContent-Type: text/plain; charset="utf-8"\r\nSubject: {theme}\r\n\r\n'
msg += message
session.sendmail(sender, where_to_email, msg.encode('utf8'))
session.quit()
also if you want to use yandex
mail you must
to turn on "protal code" in settings.
Upvotes: 3
Reputation: 9
google no longer lets you turn this feature off, meaning it just wont work no matter what you do, yahoo appears to be the same way
Upvotes: 0
Reputation: 6023
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 (like 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.
Upvotes: 93
Reputation: 17
Code to send email via python:
import smtplib , ssl
import getpass
server = smtplib.SMTP_SSL("smtp.gmail.com",465)
server.ehlo()
server.starttls
password = getpass.getpass() # to hide your password while typing (feels cool)
server.login("[email protected]", password)
server.sendmail("[email protected]" , "[email protected]" , "I am trying out python email through coding")
server.quit()
#turn off LESS SECURE APPS to make this work on your gmail
Upvotes: 0