Python SMTP Server with SSL: handshake error - python

I use lib secure_smtpd for create SMTP server. When I generate certificate and use it for SSL connection I catch exception (for test I use Opera mail client and The Bat!):
SSLError: _ssl.c:489: The handshake operation timed out
When I test use python script everything is ok:
smtpObj = smtplib.SMTP_SSL('localhost',2000)
smtpObj.set_debuglevel(1)
smtpObj.login('testuser', '111111')
msg = MIMEMultipart('alternative')
msg['Subject'] = "my subj SSL"
msg['From'] = sender
msg['To'] = "username#site.com"
msg.attach(MIMEText("Hello world!",'html'))
smtpObj.sendmail(sender, [toemail], msg.as_string())
Can somebody help fix problem with handshake?
I use python 2.7.3

How you configured SMTP in Opera and The Bat! ? There is SSL mode (which you seems to use), where all connection is wrapped in SSL/TLS, and STARTTLS mode, where connection is plain but SSL/TLS excahnge is started after client issues STARTTLS command.

Related

What is smtp_server.ehlo and what is it used for?

I was seeing a smtplib python tutorial, and I see these code lines:
smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtp_server.ehlo()
What is that .ehlo used for?
EHLO ("Extended Hello") is the SMTP command the client uses to tell the server that it is an SMTP client (HELO is the old SMTP protocol, while EHLO is the extended SMTP initialisation command).
EHLO was introduced with RFC 1869 back in 1995, so for any actual use you'll se EHLO used these days.
In effect it's just telling your smtp library to send the "Hello, I'm an STMP client and want to use the extended command set" message.

How do I resolve a connection error when sending an email through outlook with python?

I am trying to send an email with python, because I have to send many emails in bulk and would like to do so with python. The code that I have written is below. When I run this however, I get the following error
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I think this might be a problem with the setup in Outlook, but I am unsure.
import smtplib
with smtplib.SMTP('smtp.outlook365.com',587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('email#address.com','password')
subject = 'This is a test.'
body = 'wonder if this works!'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail('sender#email.com','receiver#email.com',msg)
Check the address of the smtp server, referring to the Microsoft documentation I saw that the address is different.
SMTP server name smtp.office365.com
https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040

Sending email using python through an intermediary host

I am sending emails using smtplib using internal relay.
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = server
s = smtplib.SMTP('SMTP', port)
s.send_message(msg)
s.quit()
Access is controlled with an ACL and I would like to be able to fire my script from my local machine through an intermediary host that is allowed on the SMTP relay. Until now I have been sshing to the intermediary host but that's a bit of a palaver to be honest.
How would I go about tunneling this traffic so SMTP relay would see the request came from intermediary host?
PS. All actions are done within a domain environment. I just want communication to flow from my hostname, through another host that is allowed on our local smtp relay.

Sending email from Python throwing SSLError 'unknown protocol'

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('noreply#xxx.com', 'xxxxxxx')
from_addr = "xxx <noreply#xxx.com>"
to_addr = rec#xxx.com
subj = "hello"
date = datetime.datetime.now.strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%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.
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.

Sending an email via gmail through a python application

as indicated by the title I am having trouble sending an email via my gmail account through a python application.I have searched online for a solution but nothing seems to solve it and I thought I might ask here.
My code is the following:
FROMADDR = "myemail#gmail.com"
LOGIN = FROMADDR
PASSWORD = "mypass"
TOADDRS = "varis81#hotmail.com"
msg = "Test message"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
print "E-mail succesfully sent"
I get the message:
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I tried different ports but it doesn't work also.I also tried hotmail but it causes the same problem.I am using Python 2.7 (don't ask :) ) on a Windows 7 machine developing on Eclipse using PyDev.
Any help would be great!
Thank you in advance.
I'm using the same construct on one of my servers. My code is below.
The only difference is the extra .ehlo() after '.starttls()`. This should not be the issue; from the RFC:
5.2 Result of the STARTTLS Command
The client SHOULD send an EHLO command as the first
command after a successful TLS negotiation.
According to the RFC, the server should not sever a connection if the client does not send ehlo after starttls, but Google could be more restrictive on their SMTP server. I'd check that first. (I've seen providers tighten down on these kinds of conditions to reduce spam, see Mailinator's 2007 writeup for instance.)
It could also be filtered ports - try running the code in the REPL and confirm which line is exceptioning, if it's the connect() you'll know it's network. If it's after, it's likely your usage of smtplib.
Of note, I also experienced occasional unclean shutdowns, resulting in the try/except around .close().
import smtplib
s = smtplib.SMTP()
s.connect("smtp.gmail.com")
s.ehlo()
s.starttls()
s.ehlo()
s.login("from#gmail.com", "frompass")
s.sendmail("fromname#gmail.com", toAddr, bytes)
try:
s.close()
except: pass
Well, since I cant post comments yet I'll have to attempt an answer..
Judging by this: Python SMTP Errno 10060
Perhaps a timeout would help?

Categories