dmetzler

E mail NameError Error Python

Discussion created by dmetzler on Jan 24, 2012
Latest reply on Jan 24, 2012 by stacyrendall
Hello,
I am sort of new to python and I am encountering an error I just cant get past. This script copies a shapefile to a new location. due to locks I have to run it automatically when services are stopped. I want the script to email me the specific error if there is one or email me a message saying it completed successfully. here is the code:


import os
import arcpy
import smtplib
import string

arcpy.env.overwriteOutput = True
try:
arcpy.CopyFeatures_management ('\\\\petrots\\data\\PAM\\PFS-Survey\\Data\\xxxxx.shp', '\\\\PETROTS\\arcgisproject\\PAM\\Clients\\PFSSurvey_v1\\Data\\xxx.shp')
except Exception as e:
print e.message
errormessage = e.message

try:
to = 'me@email.com'
gmail_user = 'username'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + errormessage
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()
except NameError:
to = 'me@email.com'
gmail_user = 'username'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + 'all is good'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

When I run it everything is smooth if there is an error however if all is good I get this:

Traceback (most recent call last):
File "P:\Scripts\copy to Y test.py", line 14, in <module>
errormessage = e.message
AttributeError: 'str' object has no attribute 'message'



I thought the except clause would take care of this however it just isn't getting the job done.

Any help would be appreciated.

Cheers,
Dave Metzler

Outcomes