Start tls - Unable to send email - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Start tls - Unable to send email (/thread-10093.html) |
Start tls - Unable to send email - darunkumar - May-12-2018 Hi Team, Start tls ()is throwing below error
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] raiseThanks A RE: Start tls - Unable to send email - ljmetzger - May-12-2018 print statement in Python 3 needs parentheses. Lewis RE: Start tls - Unable to send email - darunkumar - May-13-2018 (May-12-2018, 04:30 PM)ljmetzger Wrote: print statement in Python 3 needs parentheses. Hi Lewis, Thanks for your reply. The Python Version is 2.7, the troublemaker is starttls command. Thanks A RE: Start tls - Unable to send email - ljmetzger - May-14-2018 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 RE: Start tls - Unable to send email - ljmetzger - May-14-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 RE: Start tls - Unable to send email - darunkumar - May-15-2018 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? ThanksA RE: Start tls - Unable to send email - ljmetzger - May-15-2018 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. 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 RE: Start tls - Unable to send email - darunkumar - May-30-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 |