I am trying to send an email via python after my script runs...I am referencing this web page
How to send email in Python via SMTPLIB
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 = 'mkyong2002@yahoo.com'
gmail_user = 'mkyong2002@gmail.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()
you might want to compare to the official docs
20.12. smtplib — SMTP protocol client — Python 2.7.12 documentation
there is an example at the bottom.
Thanks for the reply
I tried that and was presented this in the Python 2.7.8 Shell
Code:
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
From: jay.xxx@xxx.gov
To: jay.xxx@xxx.gov
Enter message, end with ^D (Unix) or ^Z (Windows):
Testing ^z
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
raise err
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 are unable to send your email" caused by McAfee | Symantec Connect
ugggggg
well try sending an email from a computer you can control. If it works, then you have your answer... your lack of admin privledges and/or virus scan
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.