Select to view content in your preferred language

name 'cursor' not defined error

3097
6
Jump to solution
02-22-2019 02:36 PM
by Anonymous User
Not applicable

I'm trying to run this script which searches for approval dates with the current date and sends out emailed alerts to each customer. I keep getting an error when running 'name cursor' is not defined. 

import arcpy, datetime, smtplib
from arcgis import GIS
arcpy.env.overwriteOutput = True

#Location of ArcPro project with AGOL layer added into map content
prjPath = r'C:\Python\UtilityEncroachment_Active\Update\Update.aprx'

#Create layer object of service being updated.
proj = arcpy.mp.ArcGISProject(prjPath)
map = proj.listMaps()[0]
layer = map.listLayers()[0]
field = ['Date_Approved', 'Email', 'Applicant']
today = datetime.datetime.today()
tdy = datetime.datetime.strftime(today, '%Y-%m-%d')
        
with arcpy.da.SearchCursor(layer, field, "Date_Approved = {}".format(tdy)) as email_cursor:
    for row in cursor:
        print ("Match Found!")
        SERVER = "xxxxxxxxxxxxx"
        FROM = "xxxxx@xxxxxx.com"
        TO = "Applicant <{}>".format(row[1])
        CC = "xxxxxxxxxxxxx"
        SUBJECT = "Utility Encroachment Approach Approved"
        MSG = "\n {},\n".format(row[2]) + "        " + "This is to notify you that your utility encroachment has been approved. Please use the following link to view additional details www. .com"
        TEXT = (MSG)   
        MESSAGE = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
        server = smtplib.SMTP(SERVER)
        server.sendmail(FROM, [TO]+[CC], MESSAGE)
        
server.quit()

print ("Email notifications sent.")
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

The error also indicates line 24, but I would expect it to be around 17.  Your other scripts have 31 and 32 lines.  Did something else get added?  You might try the first script you posted with just the change "email_cursor" to "cursor" in line 16.  The only other issue I see at this time could be the where clause in that same line.  The date may need to be in single quotes, but that depends on your database/server.

"Date_Approved = {}".format(tdy)
# or
"Date_Approved = '{}'".format(tdy)

View solution in original post

6 Replies
JoeBorgione
MVP Emeritus

Line 17 is your problem:  you create a search cursor object on line 16 calling it 

email_cursor

Either change line 17 to refer to email_cursor or change line 16 to cursor.....

That should just about do it....
by Anonymous User
Not applicable

I get the following error when I make that change. The script works if I delete line 17 but then I can't have the email populate each customers name and email address.

0 Kudos
by Anonymous User
Not applicable

My current script is below along with the new error I'm receiving

import arcpy, datetime, smtplib
from arcgis import GIS
arcpy.env.overwriteOutput = True

#Location of ArcPro project with AGOL layer added into map content
prjPath = r'C:\Python\UtilityEncroachment_Active\Update\Update.aprx'

#Create layer object of service being updated.
proj = arcpy.mp.ArcGISProject(prjPath)
map = proj.listMaps()[0]
layer = map.listLayers()[0]
field = ['Date_Approved', 'Email', 'Applicant']
today = datetime.datetime.today()
tdy = datetime.datetime.strftime(today, '%Y-%m-%d')

with arcpy.da.SearchCursor(layer, field, "Date_Approved = {}".format(tdy)) as cursor:
        print ("Match Found!")
        SERVER = "xxxxxxxxxxx"
        FROM = "xxxxxxxxxxx"
        TO = "Applicant <{}>".format(cursor[1])
        CC = "xxxxxxxxxxxxxx"
        SUBJECT = "Utility Encroachment Approach Approved"
        MSG = "\n {},\n".format(cursor[2]) + "        " + "This is to notify you that your utility encroachment has been approved. Please use the following link to view additional details www. .com"
        TEXT = (MSG)   
        MESSAGE = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
        server = smtplib.SMTP(SERVER)
        server.sendmail(FROM, [TO]+[CC], MESSAGE)
        
server.quit()

print ("Email notifications sent.")

0 Kudos
RandyBurton
MVP Alum

In your second script you dropped "for row in cursor:" (line 17 in first script).  Then you need to change back lines 20 and 23 to "row[1]" and "row[2]".

with arcpy.da.SearchCursor(layer, field, "Date_Approved = {}".format(tdy)) as cursor:
    for row in cursor:
        print ("Match Found!")
        SERVER = "xxxxxxxxxxx"
        FROM = "xxxxxxxxxxx"
        TO = "Applicant <{}>".format(row[1])
        CC = "xxxxxxxxxxxxxx"
        SUBJECT = "Utility Encroachment Approach Approved"
        MSG = "\n {},\n".format(row[2]) + "        " + "This is to notify you that your utility encroachment has been approved. Please use the following link to view additional details www. .com"
        TEXT = (MSG)   
        MESSAGE = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
        server = smtplib.SMTP(SERVER)
        server.sendmail(FROM, [TO]+[CC], MESSAGE)
‍‍‍‍
by Anonymous User
Not applicable

I made the adjustment and now receive the following error

line 24 is referencing 'for row in cursor:'

0 Kudos
RandyBurton
MVP Alum

The error also indicates line 24, but I would expect it to be around 17.  Your other scripts have 31 and 32 lines.  Did something else get added?  You might try the first script you posted with just the change "email_cursor" to "cursor" in line 16.  The only other issue I see at this time could be the where clause in that same line.  The date may need to be in single quotes, but that depends on your database/server.

"Date_Approved = {}".format(tdy)
# or
"Date_Approved = '{}'".format(tdy)