Send smtp message - 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: Send smtp message (/thread-20467.html) |
Send smtp message - barry - Aug-12-2019 I am stumped with trying to use the Python smtplib module. I have followed the instructions via https://pymotw.com/2/smtplib/. I am using the example from the second part "Authentication and Encryption": import smtplib import email.utils from email.mime.text import MIMEText import getpass # Prompt the user for connection info to_email = raw_input('Recipient: ') servername = raw_input('Mail server name: ') username = raw_input('Mail user name: ') password = getpass.getpass("%s's password: " % username) # Create the message msg = MIMEText('Test message from PyMOTW.') msg.set_unixfrom('author') msg['To'] = email.utils.formataddr(('Recipient', to_email)) msg['From'] = email.utils.formataddr(('Author', '[email protected]')) msg['Subject'] = 'Test from PyMOTW' server = smtplib.SMTP(servername) try: server.set_debuglevel(True) # identify ourselves, prompting server for supported features server.ehlo() # If we can encrypt this session, do it if server.has_extn('STARTTLS'): server.starttls() server.ehlo() # re-identify ourselves over TLS connection server.login(username, password) server.sendmail('[email protected]', [to_email], msg.as_string()) finally: server.quit()But, I only get so far: Recipient: [email protected] Mail server name: smtpauth.isp.net Mail user name: [email protected] [email protected]'s password: send: 'ehlo 70WLKG105279.houtx.lcl\r\n' reply: '250-smtp.gmail.com at your service, [50.58.210.12]\r\n' reply: '250-SIZE 35882577\r\n' reply: '250-8BITMIME\r\n' reply: '250-STARTTLS\r\n' reply: '250-ENHANCEDSTATUSCODES\r\n' reply: '250-PIPELINING\r\n' reply: '250-CHUNKING\r\n' reply: '250 SMTPUTF8\r\n' reply: retcode (250); Msg: smtp.gmail.com at your service, [50.58.210.12] SIZE 35882577 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING CHUNKING SMTPUTF8 send: 'STARTTLS\r\n' reply: '220 2.0.0 Ready to start TLS\r\n' reply: retcode (220); Msg: 2.0.0 Ready to start TLSAny suggestions would be much appreciated. Barry Python 2.7.13 on Win32 RE: Send smtp message - barry - Aug-12-2019 Have also tried a slightly modified version (https://pymotw.com/3/smtplib/), using Python 3.5.3: import smtplib import email.utils from email.mime.text import MIMEText import getpass # Prompt the user for connection info to_email = input('Recipient: ') servername = input('Mail server name: ') serverport = input('Server port: ') if serverport: serverport = int(serverport) else: serverport = 25 use_tls = input('Use TLS? (yes/no): ').lower() username = input('Mail username: ') password = getpass.getpass("%s's password: " % username) # Create the message msg = MIMEText('Test message from PyMOTW.') msg.set_unixfrom('author') msg['To'] = email.utils.formataddr(('Recipient', to_email)) msg['From'] = email.utils.formataddr(('Author', '[email protected]')) msg['Subject'] = 'Test from PyMOTW' if use_tls == 'yes': print('starting with a secure connection') server = smtplib.SMTP_SSL(servername, serverport) else: print('starting with an insecure connection') server = smtplib.SMTP(servername, serverport) try: server.set_debuglevel(True) # identify ourselves, prompting server for supported features server.ehlo() # If we can encrypt this session, do it if server.has_extn('STARTTLS'): print('(starting TLS)') server.starttls() server.ehlo() # reidentify ourselves over TLS connection else: print('(no STARTTLS)') if server.has_extn('AUTH'): print('(logging in)') server.login(username, password) else: print('(no AUTH)') server.sendmail('[email protected]', [to_email], msg.as_string()) finally: server.quit()Resulting in: Python 3.5.3 |Continuum Analytics, Inc.| (default, Feb 22 2017, 21:28:42) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ============== RESTART: C:/temp/Barry/Scripts/PyMOTW_example.py ============== Recipient: [email protected] Mail server name: smtp.mailserver.com Server port: 895 Use TLS? (yes/no): yes Mail username: myusername Warning (from warnings module): File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\getpass.py", line 101 return fallback_getpass(prompt, stream) GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed. e166615's password: mypassword starting with a secure connection Traceback (most recent call last): File "C:/temp/Barry/Scripts/PyMOTW_example.py", line 28, in <module> server = smtplib.SMTP_SSL(servername, serverport) File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\smtplib.py", line 1021, in __init__ source_address) File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\smtplib.py", line 251, in __init__ (code, msg) = self.connect(host, port) File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\smtplib.py", line 335, in connect self.sock = self._get_socket(host, port, self.timeout) File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\smtplib.py", line 1027, in _get_socket self.source_address) File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\socket.py", line 712, in create_connection raise err File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\socket.py", line 703, in create_connection sock.connect(sa) 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 RE: Send smtp message - shapeg - Jan-23-2020 Hi Barry, did you get it working? I have code that works fine with the Gmail server, but I can't get it to work with other email servers such as mail.com or outlook.co.nz. regards Shane |