HOW TO

Generate a scheduled email update when a feature is added to a hosted feature service using Python

Last Published: April 25, 2020

Summary

A hosted feature service can be added to a public web app. This provides users with editing capabilities. In some cases, owners may prefer to receive scheduled update notifications when new hosted features are added to the hosted feature service. It is possible to construct a Python script to check for changes to the hosted feature service over a time given interval and send an email to the owner. An automated email can be sent using the smptlib module to access the email SMTP protocol of the desired webmail provider, such as Gmail or Hotmail. 

Procedure

Below are the steps to generate and send an automated email containing newly created ObjectIDs when new features are added to a hosted feature service:
  1. Import the necessary libraries.
import urllib2, json, urllib, datetime, time, smtplib  
from datetime import timedelta
  1. Create an array parameter to store the ObjectID data in the ObjectID field, and specify the required variables such as the URL and the unique ID field. 
username = 'Username'  # ArcGIS Online Username
password = 'password'  # ArcGIS Online Password

URL = 'https://services.arcgis.com/[Folder]/arcgis/rest/services/[Feature Data]/FeatureServer/2/query'       # Feature Service URL
uniqueID = 'OBJECTID'   # Desired field to check 
dateField = 'Date'      # Date field to query
hoursValue = 1          # Number of hours to check when a feature was added

fromEmail = 'Sample@email.com' # Sender's Email
toEmail = 'Sample@email.com'   # Receiver's Email 
#smtpServer = 'smtp.gis.com'   # SMPT Server Name
#portNumber = 25               # SMTP Server port

# Create empty list for uniqueIDs
oidList = []
  1. Generate the ArcGIS Online token for accessing the specified URL.
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 = ''
  1. Create a request to open the network object denoted by a URL for reading.
params = {'f': 'pjson', 'where': "1=1", 'outfields' : '*' , 'returnGeometry' : 'false', 'token' : token}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
  1. Create a loop statement to search for edits or changes, if available, over a time interval.
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("Edited feature")
  1. Create an email template that includes the sender and receiver's email addresses. 
FROM = 'sender@hotmail.com'  
TO = ['receiver@yahoo.com']  
SUBJECT = 'New Feature Added'  
TEXT = "Features with OBJECTIDs " + str(oidList) + " were added."
  1. Retrieve the parameters specified in step 6 as a string data.
message = """\ 
From: %s 
To: %s 
Subject: %s 
 
%s 
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
emailPassword = 'Password' 
  1. Retrieve the email account SMTP protocol using the smtplib module.
Note:
Some email providers, for example, Gmail, require secure access logins from apps. The option to allow less secure app access is required to successfully establish a connection with the smtp server. 
if len(oidList) > 0:
    server = smtplib.SMTP_SSL('smtp.gmail.com')
    server.connect('smtp.gmail.com')
    server.ehlo()
    server.login(FROM, gmailPWD)
    server.sendmail(FROM, TO, message)
    print "Successfully sent email"
    server.close()
else:
 print "No new data found."

The following script shows the final code:  

import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta

# Variables
username = 'Username'    
password = 'password'    

URL = 'https://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/tem_data/FeatureServer/2/query'       # Feature Service URL
uniqueID = 'OBJECTID'          
dateField = 'EditDate'      
hoursValue = 1              

fromEmail = 'test@gmail.com' 
toEmail = 'test@esri.com'   

oidList = []

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 = ''

params = {'f': 'pjson', 'where': "1=1", 'outfields' : '*' , '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("Edited feature")

print(oidList)

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)
gmailPWD = 'password'

if len(oidList) > 0:
    server = smtplib.SMTP_SSL('smtp.gmail.com')
    server.connect('smtp.gmail.com')
    server.ehlo()
    server.login(FROM, gmailPWD)
    server.sendmail(FROM, TO, message)
    print "Successfully sent email"
    server.close()
else:
	print "No new data found."

Article ID:000019310

Software:
  • ArcGIS Online
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic