Sending email from Python throwing SSLError 'unknown protocol'

Question:

I was sending emails from flask-mail but since trying to use the mailservers at namecheap or bluehost, I’m getting the following error:

SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

So now I’m trying to send the email without flask-mail but I’m still getting the same error. Any fix?
My code is as follows:

from smtplib import SMTP
smtp = SMTP()

smtp.set_debuglevel(debuglevel)
smtp.connect('xxxxxx', 26)
smtp.login('[email protected]', 'xxxxxxx')

from_addr = "xxx <[email protected]>"
to_addr = [email protected]

subj = "hello"
date = datetime.datetime.now.strftime( "%d/%m/%Y %H:%M" )

message_text = "HellonThis is a mail from your servernnByen"

msg = "From: %snTo: %snSubject: %snDate: %snn%s" % ( from_addr, to_addr, subj, date, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()

My application is running on Ubuntu 14.04 on Amazon EC2.

Thanks.

Asked By: Alex Paterson

||

Answers:

The reason that this is giving you this error is because your mail server is not a SMTP server. Use Gmail or another smtp mail service to send the mail. Try sending it through a gmail account with the server being smtp.gmail.com and the port being 587. First though you will need to configure your account for it.

Answered By: Jonah Fleming
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.