Skip to main content
Hello,

 

 

I am trying to set my workspace to send an email using a shutdown python script.

 

For doing so, I used the following Python script found on one of the Safe articles:

 

 

 

import smtplib, fmeobjects

 

 

 

to =  FME_MacroValuesa'EmailTo']

 

gmail_user =  FME_MacroValues_'GmailUser']

 

gmail_pwd =  FME_MacroValuesM'Password']

 

Subject =  FME_MacroValues 'SuccessSubject']

 

FailSubject =  FME_MacroValuesÂ'FailSubject']

 

Emailfrom =  FME_MacroValues 'EmailFrom']

 

Jobs =  FME_MacroValuesb'JOBID']

 

 

FeaturesWritten = str(FME_FeaturesWritten)

 

 

status = FME_Status

 

if status == 0:

 

    Subject =  FailSubject

 

    Message = 'Workspace Failed'    

 

else:

 

    Message = 'Workspace Successful. Resulting *.dwg Can Be Located At C:\\Temp'

 

 

smtpserver = smtplib.SMTP("smtp.gmail.com",587)

 

 

smtpserver.ehlo()

 

smtpserver.starttls()

 

smtpserver.ehlo

 

smtpserver.login(gmail_user, gmail_pwd)

 

header = 'To:' + to + '\\n' + 'From: ' + Emailfrom + '\\n' + 'Subject:' + Subject +'\\n'

 

print header

 

msg = header + '\\n' + Message + '\\n\\n'

 

smtpserver.sendmail(gmail_user, to, msg)

 

print 'done!'

 

smtpserver.close()

 

 

 

This code works fine, but what I need to do is set the email destination ("to" variable) to an attribute called _Email_submitter.

 

But when I try to do so, the translation fails.

 

I setting the variable like this:

 

 

feature.getAttribute("_Email_submitter") =  email

 

to =  email

 

 

This is the only part in the entire code that is changed, but for some reason its causing the translation to fail.

 

 

I am not sure with I am having a problem retriving the attribute value (the list have only one value) or if is not possible to set an attribute value as the email destination.

 

 

 

Thanks,

 

Gabriel
I was making some tests with the script and seems that the problem is with the feature.getAttribute("_Email_submitter"), but I couldn't figure out why.
Hi Gabriel,

 

 

feature.getAttribute("_Email_submitter") =  email   1) In the shutdown process, any  features don't exist any longer. You cannot get attribute values from any features. If you need to get a feature attribute value, you should do that in the midway of translation.

 

2) The = operator assigns the right hand side to the left hand side, but the left hand side of this expression is method calling, which cannot be assign any values. This is an invalid syntax.

 

 

Takashi
Hi Gabriel

 

You can make a python variable out of an attribute value midway in a translation using the pythonCaller like this:

 

 

import fmeobjects # Template Function interface: def processFeature(feature):   global MyCounter     Email_submitter = feature.getAttribute('_Email_submitter"')     pass

 

 

You really just need to send one feature to the pythonCaller (use a Sampler - first feature)

 

Then in the shutdown script Email_submitter is already defined and you can get it like this:

 

 

Email_submitter = str(Email_submitter)

 

 

 

 

 


Hello, Ken

 

 

I set my workspace as you told me but my translation keeps failing.

 

I get the following message:

 

 

Python Exception <NameError>: name 'Email_submitter' is not defined

Error executing string `import smtplib, fmeobjects

 

 

I am not sure, but I belive this error is happening in the PythonCaller.

 

I've never used this transformer before, so I was unsure about what should I set on the "Class or Function to Process Features" so I just left the the default value "FeatureProcessor".

 

 

Thanks,

 

 

Gabriel

 

 
Yeah, just changed the "Class or Function to Process Features" to processFeature and still doesn't work
Hi Gabriel,

 

 

Maybe there is a typo in Ken's example script. Change "global MyCounter" to "global Email_submitter" and retry.

 

 

Takashi
Hey Takashi,

 

 

I are right, I guess this was the reason why it was not working.

 

Either way, I managed to solve my problem with the help from your post on this topic:

 

http://fmepedia.safe.com/AnswersQuestionDetail?id=906a0000000cgTZAAY

 

 

Double thank you!
Sorry guys, yes a typo in my sample (I modified it to fit the question and missed something). Should be:

 

 

import fmeobjects # Template Function interface: def processFeature(feature):   global Email_submitter     Email_submitter = feature.getAttribute('_Email_submitter"')     pass

Reply