Python Forum
Start tls - Unable to send email
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Start tls - Unable to send email
#1
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
Reply
#2
print statement in Python 3 needs parentheses.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
(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
Reply
#4
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#5
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#6
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
Reply
#7
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Email, certificate verify failed: unable to get local issuer certificate SpongeB0B 0 1,559 Feb-08-2023, 02:24 PM
Last Post: SpongeB0B
  Send Email with attachment metro17 4 3,201 Apr-14-2020, 04:59 PM
Last Post: CodeItBro
  Send an email Jokoba 0 2,517 Mar-12-2020, 05:30 PM
Last Post: Jokoba
  Help send email by Python using smtplib hangme 6 6,116 Jan-25-2020, 03:31 AM
Last Post: shapeg

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020