Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL Error while Sending Email with Python (Raspbian OS)

I just write this code in Python under Raspbian OS:

import smtplib

fromaddr = '*****@hotmail.de'
toaddrs  = '*****@hotmail.de'
msg = 'Testmail'

username = '*****@hotmail.de'
password = '*****'

server = smtplib.SMTP('smtp.live.com',587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

And get following Error-Message:

python ail.py
Traceback (most recent call last):
File "ail.py", line 14, in <module>
  server.login(username, password)
File "/usr/lib/python2.7/smtplib.py", line 601, in login
  AUTH_PLAIN + " " + encode_plain(user, password))
File "/usr/lib/python2.7/smtplib.py", line 385, in docmd
  return self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 358, in getreply
  + str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno 1] _ssl.c:1359: 
error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

What is my fault? Could somebody help me - please?

Regards

like image 975
Kipcak08 Avatar asked Jul 10 '26 18:07

Kipcak08


1 Answers

After I've signed in on http://live.com and validated my account; your code worked as is on Ubuntu python 2.7 and python3.3:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Send email via live.com."""
import smtplib
from email.mime.text      import MIMEText
from email.header         import Header

login, password = ...

msg = MIMEText(u'body…', 'plain', 'utf-8')
msg['Subject'] = Header(u'subject…', 'utf-8')
msg['From'] = login
recipients = [login]
msg['To'] = ", ".join(recipients)

s = smtplib.SMTP('smtp.live.com', 587, timeout=10)  
s.set_debuglevel(1)
try:
    s.starttls() 
    s.login(login, password) 
    s.sendmail(msg['From'], recipients, msg.as_string())
finally:
    s.quit()

Check whether openssl can connect to it (ca-certificates is installed and it is not this bug):

$ openssl s_client -starttls smtp -connect smtp.live.com:587

If it is successful; you could replace smtplib.SMTP.starttls() method (in a subclass) to set appropriate ssl parameters.

like image 86
jfs Avatar answered Jul 13 '26 14:07

jfs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!