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 = 'firstname.lastname@example.org' send_username = 'email@example.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)
If you are doing Python development, you may be interested in my Windows Dev Stack, which describes my development environment from high level technologies down to specific apps, and how they all work together.