I am trying to send an email via python after my script runs...I am referencing this web page
I put in my Gmail user and password, not the one below...and run it...that's all I changed.
I get this error...wondering if anyone has done this before or have any other examples on how to do this.
import smtplib to = 'firstname.lastname@example.org' gmail_user = 'email@example.com' gmail_pwd = 'yourpassword' smtpserver = smtplib.SMTP("smtp.gmail.com",587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(gmail_user, gmail_pwd) header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n' print header msg = header + '\n this is test msg from mkyong.com \n\n' smtpserver.sendmail(gmail_user, to, msg) print 'done!' smtpserver.close()
Thanks for the reply
I tried that and was presented this in the Python 2.7.8 Shell
import smtplib def prompt(prompt): return raw_input(prompt).strip() fromaddr = prompt("From: ") toaddrs = prompt("To: ").split() print "Enter message, end with ^D (Unix) or ^Z (Windows):" # Add the From: and To: headers at the start! msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, ", ".join(toaddrs))) while 1: try: line = raw_input() except EOFError: break if not line: break msg = msg + line print "Message length is " + repr(len(msg)) server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()
Result in the Python 2.7.8 Shell
Message length is 94
Traceback (most recent call last):
File "C:/Users/tjv36463/Desktop/BearCollar/Python Files/ImportBearDataFiles_TestForExcel.py", line 83, in <module>
server = smtplib.SMTP('localhost')
File "C:\Python27\ArcGISx6410.3\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\ArcGISx6410.3\lib\smtplib.py", line 311, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\ArcGISx6410.3\lib\smtplib.py", line 286, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\ArcGISx6410.3\lib\socket.py", line 571, in create_connection
error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
So Dr Googled the actual error message links and ruled them out...
with things like virus protector, you aren't running as administrator...
I logged in with an Admin account and same error....hmmmm
Our computers are pretty locked down as its a govt computer...McAfee is all over this machine...I tried to change the settings but no go they are disabled.
We use SMTP as well and as a government agency can say we have had this same error occur several times before. So I have seen 2 scenarios for us that triggered this:
1) Network spikes/lag issues - we have created a centralized IT agency here in Ohio and rather than each agency managing their own exchange servers, etc. it's all handled by the new IT agency. Being that we are not co-located and due to some extensive networking that had to be done so the central IT could sit between the web and all agencies under it we have seen some minor oddities at times with things that have to be routed between agencies (essentially, having to define agency to agency traffic as external despite the fact that everything appears to be internal since all agencies are tucked behind the IT firewall).
2) Permissions at the SMTP end - The centralized IT office of course doesn't allow open ended admin rights to the exchange servers, etc. So they had to build in permissions to explicitly allow the service accounts (running the .py scripts) and sender accounts (FROM field of e-mail) [had them do both to cover our bases and alleviate further wait times on 2 separate tickets] in order to allow the e-mails to be sent.