I have been trying to utilize Jake's solution located here https://community.esri.com/docs/DOC-10163-send-email-when-a-feature-is-added-to-a-arcgis-online-host... to send an email from outlook when a new feature is added. However, I keep receiving the falling traceback error:
Traceback (most recent call last):
File "C:\Users\twhitley\Documents\check_update2b.py", line 66, in <module>
smtpObj.sendmail(FROM, TO, message)
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 723, in sendmail
self.rset()
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 462, in rset
return self.docmd("rset")
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 387, in docmd
return self.getreply()
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 363, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
My python parameters for outlook are this:
fromEmail = 'awhitley@peachtree-city.org' # Email sender
toEmail = 'awhitley@peachtree-city.org' # Email receiver
smtpServer = 'smtp-mail.outlook.com' # SMPT Server Name
portNumber = 587 # SMTP Server port
Can someone point me in the right direction; the script Generates the token as expected but seems to hang at the SMTP settings. #
The referenced thread is quite long and there are a lot of code snippets. It would be helpful if you paste the specific code you are using here.
Sure; the actual code is here:
import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta
# Variables
username = 'awhitley67' # AGOL Username
password = 'xxxxxxxxxxx' # AGOL Password
URL = 'https://services5.arcgis.com/ixWnd9c3gwqSeniI/arcgis/rest/services/RoadProblemReports/FeatureServer/...' # Feature Service URL
uniqueID = 'OBJECTID' # i.e. OBJECTID
dateField = 'CreationDate' # Date field to query
hoursValue = 1 # Number of hours to check when a feature was added
fromEmail = 'awhitley@peachtree-city.org' # Email sender
toEmail = 'awhitley@peachtree-city.org' # Email receiver
smtpServer = smtp-mail.outlook.com' # SMPT Server Name
portNumber = 587 # SMTP Server port
# Create empty list for uniqueIDs
oidList = []
# Generate AGOL token
try:
print('Generating Token')
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']
except:
token = ''
# Query service and check if created_date time is within the last hour
params = {'f': 'pjson', 'where': "1=1", 'outfields' : '{0}, {1}'.format(uniqueID, dateField), 'returnGeometry' : 'false', 'token' : token}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
createDate = feat['attributes'][dateField]
createDate = int(str(createDate)[0:-3])
t = datetime.datetime.now() - timedelta(hours=hoursValue)
t = time.mktime(t.timetuple())
if createDate > t:
oidList.append(feat['attributes'][uniqueID])
print(oidList)
# Email Info
FROM = fromEmail
TO = [toEmail]
SUBJECT = 'New Features Added'
TEXT = "Features with {0}s {1} were added.".format(uniqueID, oidList)
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# If new features exist, send email
if len(oidList) > 0:
smtpObj = smtplib.SMTP(host=smtpServer, port=portNumber)
smtpObj.sendmail(FROM, TO, message)
print "Successfully sent email"
smtpObj.quit()
I suspect your smtp server is wanting authorization. This page https://www.authsmtp.com/python/index.html has a summary of the steps. See also: POP, IMAP, and SMTP settings for Outlook.com.
EDIT: In the thread you mentioned, Jake Skinner shared a link (How to Send Emails with Gmail using Python) which also describes the authorization process.