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
# 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.
Best answer by esalmagul
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."
# 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."
# 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
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.
# 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.
# 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."
# 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