Solved

Shutdown Python Scripts in FME


Does anyone has a good example for this script? I copied the script from here and filled out some of the parameters. Not a coder. Hope someone can help me out

 

https://docs.safe.com/fme/html/FME_Desktop_Documentation/FME_Desktop/Workbench/Startup_and_Shutdown_Python_Scripts.htm

 

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import fme
# FME variables.
status = fme.status
errorMsg = fme.failureMessage
logFile = fme.logFileName
 
# E-mail message values.
subject = "FME Translation FAILURE"
to = "receiver@domain.com"
sender = "Your FME script <sender@domain.com>"
text = "FME translation failed with error message: " + errorMsg + "\r\n\r\nSee attached logfile for details."
 
# Credentials.
AUTHREQUIRED = 0
username = "smtp.user@domain.com"
password = "smtppassword"
smtpServer = "smtp.server.com"
 
# Create and return a message with a logfile attachment.
def createMessage():
    # Set up the e-mail.
    message = MIMEMultipart()
    message["Subject"] = subject
    message["To"] = to
    message["From"] = sender
    message["Date"] = formatdate(localtime=True)
    message.attach(MIMEText(text))
    # Attach the logfile.
    attachment = MIMEBase("application", "octet-stream")
    attachment.set_payload(open(logFile, "rb").read())
    Encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", 'attachment; filename="%s"' % os.path.basename(logFile))
    message.attach(attachment)
    return message
 
# Send the passed in message.
def sendMessage(message):
    server = smtplib.SMTP(smtpServer)
    if AUTHREQUIRED:
        server.login(username, password)
    server.sendmail(sender, to, message.as_string())
    server.quit()
 
# E-mails the translation results on failure.
def mailResults():
    if status == 0:
        message = createMessage()
        sendMessage(message)
 
# Call function for FME to execute.
mailResults()

 

It gives me the following error.

 

Another issues after fixing the indentions, it tells me to install fme python packages. However install pip doesn't wrk either because it wants me to upgrade to version 3.8

 

The goal of this is just to get notifications of some form in case FME workbench fails to run and get a log of it. I heard the emailer still fails if the workbench fails.

 

icon

Best answer by esalmagul 6 July 2022, 03:12

View original

4 replies

Userlevel 4

Can you please repost the code with the correct indentations.

Can you please repost the code with the correct indentations.

Updated :)

Userlevel 4

The code is for Python 2.7, it's not compatible with Python 3.x, unfortunately.

Try with the following:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders
import os
import fme
# FME variables.
status = fme.status
errorMsg = fme.failureMessage
logFile = fme.logFileName
 
# E-mail message values.
subject = "FME Translation FAILURE"
to = "receiver@domain.com"
sender = "Your FME script <sender@domain.com>"
text = "FME translation failed with error message: " + errorMsg + "\r\n\r\nSee attached logfile for details."
 
# Credentials.
AUTHREQUIRED = 0
username = "smtp.user@domain.com"
password = "smtppassword"
smtpServer = "smtp.server.com"
 
# Create and return a message with a logfile attachment.
def createMessage():
    # Set up the e-mail.
    message = MIMEMultipart()
    message["Subject"] = subject
    message["To"] = to
    message["From"] = sender
    message["Date"] = formatdate(localtime=True)
    message.attach(MIMEText(text))
    # Attach the logfile.
    attachment = MIMEBase("application", "octet-stream")
    attachment.set_payload(open(logFile, "rb").read())
    encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", 'attachment; filename="%s"' % os.path.basename(logFile))
    message.attach(attachment)
    return message
 
# Send the passed in message.
def sendMessage(message):
    server = smtplib.SMTP(smtpServer)
    if AUTHREQUIRED:
        server.login(username, password)
    server.sendmail(sender, to, message.as_string())
    server.quit()
 
# E-mails the translation results on failure.
def mailResults():
    if status == 0:
        message = createMessage()
        sendMessage(message)
 
# Call function for FME to execute.
mailResults()

Note, however, that the code does not seem to support e.g. TLS/SSL encryption, if your SMTP server requires it.

The code is for Python 2.7, it's not compatible with Python 3.x, unfortunately.

Try with the following:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders
import os
import fme
# FME variables.
status = fme.status
errorMsg = fme.failureMessage
logFile = fme.logFileName
 
# E-mail message values.
subject = "FME Translation FAILURE"
to = "receiver@domain.com"
sender = "Your FME script <sender@domain.com>"
text = "FME translation failed with error message: " + errorMsg + "\r\n\r\nSee attached logfile for details."
 
# Credentials.
AUTHREQUIRED = 0
username = "smtp.user@domain.com"
password = "smtppassword"
smtpServer = "smtp.server.com"
 
# Create and return a message with a logfile attachment.
def createMessage():
    # Set up the e-mail.
    message = MIMEMultipart()
    message["Subject"] = subject
    message["To"] = to
    message["From"] = sender
    message["Date"] = formatdate(localtime=True)
    message.attach(MIMEText(text))
    # Attach the logfile.
    attachment = MIMEBase("application", "octet-stream")
    attachment.set_payload(open(logFile, "rb").read())
    encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", 'attachment; filename="%s"' % os.path.basename(logFile))
    message.attach(attachment)
    return message
 
# Send the passed in message.
def sendMessage(message):
    server = smtplib.SMTP(smtpServer)
    if AUTHREQUIRED:
        server.login(username, password)
    server.sendmail(sender, to, message.as_string())
    server.quit()
 
# E-mails the translation results on failure.
def mailResults():
    if status == 0:
        message = createMessage()
        sendMessage(message)
 
# Call function for FME to execute.
mailResults()

Note, however, that the code does not seem to support e.g. TLS/SSL encryption, if your SMTP server requires it.

from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
import email
import os
import fme
# FME variables.
status = fme.status
errorMsg = fme.failureMessage
logFile = fme.logFileName
 
# E-mail message values.
subject = "FME Translation FAILURE"
to = "xxx@xxx.xxx"
sender = "Your FME script <xxx@xxx.xxx>"
text = "FME translation failed with error message: " + errorMsg + "\r\n\r\nSee attached logfile for details."
 
# Credentials.
AUTHREQUIRED = 0
username = "smtp.user@domain.com"
password = "smtppassword"
smtpServer = "xxx.xxx.xx"
 
# Create and return a message with a logfile attachment.
def createMessage():
    # Set up the e-mail.
    message = MIMEMultipart()
    message["Subject"] = subject
    message["To"] = to
    message["From"] = sender
    message["Date"] = formatdate(localtime=True)
    message.attach(MIMEText(text))
    # Attach the logfile.
    attachment = MIMEBase("application", "octet-stream")
    attachment.set_payload(open(logFile, "rb").read())
    email.encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", 'attachment; filename="%s"' % os.path.basename(logFile))
    message.attach(attachment)
    return message
 
# Send the passed in message.
def sendMessage(message):
    server = smtplib.SMTP(smtpServer)
    if AUTHREQUIRED:
        server.login(username, password)
    server.sendmail(sender, to, message.as_string())
    server.quit()
 
# E-mails the translation results on failure.
def mailResults():
    if status == 1:
        message = createMessage()
        sendMessage(message)
 
# Call function for FME to execute.
mailResults()

So this is what worked. I discovered I had the following issues

 

1) I needed to install all python packages first. There was a trouble accessing email and utils packages, so I had to go and download those first

2) The code was not updated to the newest Python version. So I had to research what they changed to. email.mime.multipart is the new way to the commands vs email.MIMEMultipart and so on. So this is another important thing to keep in mind

 

Reply