import arcpy, os, datetime, ctypes, smtplib, string from datetime import date, timedelta from arcpy import env env.workspace = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports' # Scheduler. Determines the day of the week. Program only checks for new road additions on weekdays. print ("Finding current date and time...") now = datetime.datetime.now() dayofweek = datetime.datetime.now().weekday() print now if dayofweek == 0: print ("Monday") elif dayofweek == 1: print ("Tuesday") elif dayofweek == 2: print ("Wednesday") elif dayofweek == 3: print ("Thursday") elif dayofweek == 4: print ("Friday") else: print ("End: Not programmed to run on weekends.") # Setting up dates and file naming. todaysdate = str(datetime.date.today()) todaysdate = todaysdate.replace('-', '_') print todaysdate yesterday = str(date.today() - timedelta(1)) yesterday = yesterday.replace('-', '_') print yesterday friday = str(date.today() - timedelta(3)) # Actually finds 3 days ago. Only finds Friday on Monday. For example, on Tuesday it would actually find Saturday. friday = friday.replace('-', '_') print friday todaysfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + todaysdate + '.shp' print todaysfile yesterfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + yesterday + '.shp' print yesterfile fridayfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + friday + '.shp' print fridayfile comparefile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\comparefile.txt' # had + 'todaysdate' print comparefile # Export public_works.PW.Streets to shapefile and name according to the date. if dayofweek in [0, 1, 2, 3, 4]: arcpy.CopyFeatures_management(r'G:\Department Projects\Public Works\Public Works.sde\public_works.PW.Streets', r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + todaysdate + '.shp') print ("Copy features completed.") else: print ("End: Not programmed to run on weekends (2).") # Check for existing comparefile and delete if it exists if os.path.exists(comparefile): os.remove(comparefile) # Feature compare. Compare yesterdays file with Todays new file. if arcpy.Exists(yesterfile): result = arcpy.FeatureCompare_management(yesterfile, todaysfile, "FacilityID", "ALL", "", "0.003280833333 Feet", "0", "0", "", "", "CONTINUE_COMPARE", comparefile) print ("Yesterday's file used in feature compare.") else: result = arcpy.FeatureCompare_management(fridayfile, todaysfile, "FacilityID", "ALL", "", "0.003280833333 Feet", "0", "0", "", "", "CONTINUE_COMPARE", comparefile) print ("Friday's file used in feature compare.") # Open (in memory) the resultant output compare file and look for the word 'true' (flag that indicates dissimilar files). Inside the comparefile, true indicates a change is detected and false indicates that features are the same in both files being compared. with open(comparefile) as file: truecount = 0 #linecount = 0 for line in file: #linecount = linecount + 1 for word in line.split(): while word == '"true",': truecount = truecount + 1 break #print linecount print ("Number of trues recorded: " + str(truecount)) file.close() if truecount > 0: print 'Changes detected. Sending email.' SUBJECT = "CHANGE HAS OCCURED IN THE STREETS DATABASE" TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] #TO = (['bblankner@cstx.gov']['dkillian@cstx.gov']['mcline@cstx.gov']) FROM = 'bblankner@cstx.gov' text = 'BREAKING NEWS! The program has detected a change has occurred in the Public Works street spatial database since the last comparison.' BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT , "", text ), "\r\n") server = smtplib.SMTP('00.0.0.0') #removed actual server name for security server.sendmail(FROM, [TO], BODY) server.quit() print ("Email sent!") else: print ("No changes detected. Sending email.") SUBJECT = "No change in the streets datebase" TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] #TO = (['bblankner@cstx.gov']['dkillian@cstx.gov']['mcline@cstx.gov']) FROM = 'bblankner@cstx.gov' text = 'The program has determined that no change has occurred in the Public Works street spatial database since the last comparison.' BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT , "", text ), "\r\n") server = smtplib.SMTP('00.0.0.0') #removed actual server name for security server.sendmail(FROM, [TO], BODY) server.quit() print ("Email sent!") print ('Tasks Complete')
Solved! Go to Solution.
TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] ... server.sendmail(FROM, TO, BODY) # not [TO]
def emailNotify(SUBJECT, text): print 'Changes detected. Sending email.' TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] FROM = 'bblankner@cstx.gov' BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT , "", text ), "\r\n") server = smtplib.SMTP('00.0.0.0') #removed actual server name for security server.sendmail(FROM, [TO], BODY) server.quit() print ("Email sent!") if truecount > 0: print('Changes detected. Sending email.') subject = 'CHANGE HAS OCCURED IN THE STREETS DATABASE' message = 'BREAKING NEWS! The program has detected a change has occurred in the Public Works street spatial database since the last comparison.' else: print('No changes detected. Sending email.') subject = 'No change in the streets datebase' message = 'The program has determined that no change has occurred in the Public Works street spatial database since the last comparison.' emailNotify(subject, message)
TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] ... server.sendmail(FROM, TO, BODY) # not [TO]
def emailNotify(SUBJECT, text): print 'Changes detected. Sending email.' TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] FROM = 'bblankner@cstx.gov' BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT , "", text ), "\r\n") server = smtplib.SMTP('00.0.0.0') #removed actual server name for security server.sendmail(FROM, [TO], BODY) server.quit() print ("Email sent!") if truecount > 0: print('Changes detected. Sending email.') subject = 'CHANGE HAS OCCURED IN THE STREETS DATABASE' message = 'BREAKING NEWS! The program has detected a change has occurred in the Public Works street spatial database since the last comparison.' else: print('No changes detected. Sending email.') subject = 'No change in the streets datebase' message = 'The program has determined that no change has occurred in the Public Works street spatial database since the last comparison.' emailNotify(subject, message)