Here is a way you can do it; writes the text file and sends an email. The trickiest bit is getting it to work with your specific SMTP email server. If you have gmail it's easy as pie; if not - google around...import smtplib
import arcpy
### This is the module that does the sending - you should only need to edit the top four active lines
def sendResultEmail(msgContents, success_TF):
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!
# This is the tricky bit - here are the settings for gmail (which will only work if your sender is gmail and you have a gmail account).
# If you are using something else, google around smtplib and the email service you use.
# I would suggest gmail is probably a pretty good start if you have an account...
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
if success_TF:
subject = 'Arcpy testing results: script SUCCESS.'
else:
subject = 'Arcpy testing results: script FAILURE.'
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
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 code 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)
success = False # assume failure (changed to True if script is successful)
strResult = '' # start empty string - write to this, for saving to text file and sending to email
try:
arcpy.FeatureClassToFeatureClass_conversion(inFC, outLoc, outFC)
strResult += "Converted successfully \n"
success = True
except arcpy.ExecuteError:
strResult += arcpy.GetMessages()
arcpy.AddMessage(strResult)
# Now write results to the text file
outtable = open(r"C:\temp\results.txt", "w")
outtable.write(strResult)
outtable.close()
# Now send the email. This line calls the module above, passing it the string and the T/F success variable...
sendResultEmail(strResult, success)
Let me know how you get on.