Arcpy, email, Python

Arcpy: sending a result email

This was actually surprisingly easy to do. The hardest part is logging in to your SMTP server (well, finding all the specific details you need to know to do so); fortunately it’s pretty straightforward for Gmail. If you are bound to another service, search around on the net to see what you can find out about it.

This code has been written to use a Python module/function (third line: def…), which may be unfamiliar to many Arcpy users. A module is a piece of code that is loaded into memory as it is read, which is why you have to put it at the top, and can then be called later. It isn’t necessary for this code, but comes in handy where you are doing the same thing more than once; for example, if there were three slow operations being performed in this code, and you wanted to send an email after each part was complete, having the module would significantly shorten and de-clutter the code (vs. repeating the sending script three times). The main reason I did it here is to make it easier to copy and paste to other scripts.

import smtplib
import arcpy
 
def sendResultEmail(msgContents, success_TF):
	'''sendResultEmail(msgContents, success_TF)
	This is the module that does the sending - you should only need to edit the top four active lines'''
	to = 'receiver@email.com'
	send_username = 'sender@email.com'
	send_password = 'password' # warning - will just be an unencrypted string - so be careful not to leave this file lying around!
	smtpserver = smtplib.SMTP("smtp.gmail.com", 587)

	subject = 'Arcpy testing results: script '
	if success_TF:
		subject += 'SUCCESS.'
	else:
		subject += 'FAILURE.'
		
	smtpserver.ehlo()
	smtpserver.starttls()
	smtpserver.login(send_username, send_password)
	
	header = 'To:' + to + '\n' + 'From: ' + send_username + '\n' + 'Subject:' + subject + '\n'

	msg = header + '\nArcpy results: \n\t' + msgContents + '\n'
	
	smtpserver.sendmail(send_username, to, msg)
	
	arcpy.AddMessage('Results email sent!')
	
	smtpserver.close()


### Here is the geoprocessing code; add (+=) to strResult every time you need to add something to the message (i.e. completion times)
### In a complex script this could be a lot of information, so seperate with '\n' to force new lines in the text...

inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outFC = arcpy.GetParameterAsText(2)
# text file that also contains the output information
outTxt = arcpy.GetParameterAsText(3)

success = False # assume failure (changed to True if script is successful)
strResult = '' # start empty string

try:
	arcpy.FeatureClassToFeatureClass_conversion(inFC, outLoc, outFC)
	strResult += "Converted successfully"
	success = True
except arcpy.ExecuteError:
	strResult += arcpy.GetMessages()

arcpy.AddMessage(strResult)

# Now write results to the text file - if it exists it will be overwritten
with open(outTxt, "w") as txtFile:
	txtFile.write(strResult)

# Now send the email. This line calls the module above, it requires the string and the T/F success variable...
sendResultEmail(strResult, success)
Advertisements

5 thoughts on “Arcpy: sending a result email

  1. This is great, thanks…however, GMAIL gets nervous when an application, such as Python, sends an email. While testing this code, GMAIL sent a few emails warning me of a ‘security violation’. Any thoughts on how to get around this?

    1. Haven’t had this problem myself (just double checked)! I actually set up a new Gmail account that I use exclusively for emailing results, probably a good idea to avoid having your actual account password sitting around in plain text….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s