Posts: 4
Threads: 1
Joined: May 2018
Hi Team,
Start tls ()is throwing below error
Error: send: 'STARTTLS\r\n'
reply: '220 2.0.0 SMTP server ready\r\n'
reply: retcode (220); Msg: 2.0.0 SMTP server ready
Unexpected error: <class 'ssl.SSLEOFError'>
Traceback (most recent call last):
File "./kad.py", line 24, in <module>
server.starttls()
File "/usr/lib64/python2.7/smtplib.py", line 648, in starttls
self.sock = ssl.wrap_socket(self.sock, keyfile, certfile)
File "/usr/lib64/python2.7/ssl.py", line 936, in wrap_socket
ciphers=ciphers)
File "/usr/lib64/python2.7/ssl.py", line 611, in __init__
self.do_handshake()
File "/usr/lib64/python2.7/ssl.py", line 833, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:579)
import sys
import smtplib
import email.utils
from email.mime.text import MIMEText
# Create the message
msg = 'This is the body of the message.'
FROMADDR = "[email protected]"
TOADDRS = "[email protected]"
server = smtplib.SMTP(host='<smtp server>',port=25)
server.set_debuglevel(True) # show communication with the server
print msg
try:
server.starttls()
server.ehlo()
server.sendmail(FROMADDR,TOADDRS,msg)
except:
print "Unexpected error:", sys.exc_info()[0]
raise Thanks
A
1. From same server using telnet commands able to send email
2. Python 2.7.5
Posts: 127
Threads: 3
Joined: Mar 2018
print statement in Python 3 needs parentheses.
Lewis
Posts: 4
Threads: 1
Joined: May 2018
(May-12-2018, 04:30 PM)ljmetzger Wrote: print statement in Python 3 needs parentheses.
Lewis
Hi Lewis,
Thanks for your reply.
The Python Version is 2.7, the troublemaker is starttls command.
Thanks
A
Posts: 127
Threads: 3
Joined: Mar 2018
May-14-2018, 01:01 PM
(This post was last modified: May-14-2018, 01:01 PM by ljmetzger.)
I did not have time to port my code to your structure. The following code works for me sending mail from Gmail using Python 2 and Python 3. I will try to port my code to your structure tomorrow.
# Reference: http://rosettacode.org/wiki/Send_email#Python
import smtplib
def sendemail(from_addr, to_addr_list, cc_addr_list,
subject, message,
login, password,
smtpserver='smtp.gmail.com:587'):
header = 'From: %s\n' % from_addr
header += 'To: %s\n' % ','.join(to_addr_list)
header += 'Cc: %s\n' % ','.join(cc_addr_list)
header += 'Subject: %s\n\n' % subject
message = header + message
server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(login,password)
problems = server.sendmail(from_addr, to_addr_list, message)
server.quit()
return problems
sendemail(from_addr = '[email protected]',
to_addr_list = ['[email protected]'],
cc_addr_list = [],
subject = 'Howdy',
message = 'Howdy from a python function',
login = '[email protected]',
password = 'XXXX') I hope this helps.
Please note that the following construction also worked for me. You need to use the host and port for your email provider.
server = smtplib.SMTP(host='smtp.gmail.com',port=587) Lewis
Posts: 127
Threads: 3
Joined: Mar 2018
Your code modified. Works in Python 2 and Python 3:
import platform
import smtplib
import sys
# Create the message
msg = 'This is the body of the message.'
FROMADDR = "[email protected]"
TOADDRS = "[email protected]"
LOGIN = '[email protected]'
PASSWORD = 'xxxx'
#Use host and port provided by your eMail provider
server = smtplib.SMTP(host='smtp.gmail.com',port=587)
server.set_debuglevel(True) # show communication with the server
print("Python Version: {}".format(platform.python_version()))
print( msg)
try:
server.starttls()
server.login(LOGIN, PASSWORD)
server.ehlo()
server.sendmail(FROMADDR,TOADDRS,msg)
except:
print( "Unexpected error:", sys.exc_info()[0]) Lewis
Posts: 4
Threads: 1
Joined: May 2018
May-15-2018, 11:43 AM
(This post was last modified: May-15-2018, 11:43 AM by darunkumar.)
Hey Lewis,
Thanks for amending my code, after including login & password getting the below error.
On a side note did you import/install any certificate?
Error: reply: '220 2.0.0 SMTP server ready\r\n'
reply: retcode (220); Msg: 2.0.0 SMTP server ready
Unexpected error: <class 'ssl.SSLEOFError'>
Traceback (most recent call last):
File "./mymail.py", line 28, in <module>
server.starttls()
File "/usr/lib64/python2.7/smtplib.py", line 648, in starttls
self.sock = ssl.wrap_socket(self.sock, keyfile, certfile)
File "/usr/lib64/python2.7/ssl.py", line 936, in wrap_socket
ciphers=ciphers)
File "/usr/lib64/python2.7/ssl.py", line 611, in __init__
self.do_handshake()
File "/usr/lib64/python2.7/ssl.py", line 833, in do_handshake
self._sslobj.do_handshake()
socket.error: [Errno 104] Connection reset by peer
Thanks
A
Posts: 127
Threads: 3
Joined: Mar 2018
May-15-2018, 07:42 PM
(This post was last modified: May-15-2018, 07:42 PM by ljmetzger.)
Quote: did you import/install any certificate?
No.
I am not an email or communications expert. I just follow the recipes I find. It is possible that the port you are using does not support starttls(). You obviously were successful with the smtplib.SMTP line.
Try commenting out the server.starttls() line.
If you are on a Windows Computer and use Outlook, the following code is simpler and works for me in Python 2 and Python 3, whether Outlook is open or closed. Additionally, there is no need for your Email Address (the Default Outlook Email Address is used) and there is no need for a password.
# Reference: http://rosettacode.org/wiki/Send_email#Python
import platform
import win32com.client
def sendmail(to, title, body):
olMailItem = 0
ol = win32com.client.Dispatch("Outlook.Application")
msg = ol.CreateItem(olMailItem)
msg.To = to
msg.Subject = title
msg.Body = body
msg.Send()
#NOTE: Quit causes problems when 'Outlook' is already open
# Message will not be sent until next time 'Outlook' is opened
#ol.Quit()
print("Python Version: {}".format(platform.python_version()))
print("Sending eMail using Outlook Default Account.")
print("This will work when 'Outlook' is open or closed when Sending the eMail.")
sendmail("[email protected]", "My Title", "Hello") It is possible you may get the following error message, which means there is a missing library.
Error: no module named win32com.client
If you get the error, you have to install library pypiwin32. Go to the Windows cmd.exe (or PowerShell Prompt) and type the following when connected to the Internet:
pip install pypiwin32
The above command DOES NOT WORK if you try it from inside the Python Interpreter.
Lewis
Posts: 4
Threads: 1
Joined: May 2018
Hi Lewis,
Thanks for your help, the issue got resolved, we are able to send email using the code shared initially.
Problem was at SMTP server end, they did upgrade after which things started working charm.
Thanks
A
|