Custom Python in Omniscope to send emails

Posted over 5 years ago by Deborah

Post a topic
Un Answered
D
Deborah

Hello there, just posting a Python script that I wrote and embedded in Omniscope to send email when I find a certain value in my dataset (i.e. a failure condition)


 

 

import pandas as pd
import smtplib

mail_user = 'youruser@yourserver.com'  
mail_password = 'yourpassword'

sent_from = mail_user  
to = ['recipient1@yourserver.com', 'recipient2@yourserver.com]  
subject = 'Important Message'  
body = ''

#Check a special condition
for index, row in input_data.iterrows(): 
  if (row[0] == "FAILURE"): 
    #Construct the message to send
    body = 'Failure found - ' + row[1] 
    #Do not touch these lines below until the break keyword!
    email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % (sent_from, ", ".join(to), subject, body)
    break

#If the condition was met,  send the email
if body:
	#Email Server config, and connect
	server = smtplib.SMTP_SSL('smtp.yourserver.com', 465)
	server.ehlo()
	#Next, log in to the server
	server.login(mail_user, mail_password)
    #Send the mail
	server.sendmail(sent_from, to, email_text)
    #Quit the connection
	server.quit()
	#Output the message sent to Python block output
	output_data = pd.DataFrame(["Sent "+ body], columns=["Result"])
else:
	#do nothing, output no message was sent
	output_data = pd.DataFrame(["Nothing to send"], columns=["Result"])

 

 



1 Votes


1 Comments

Antonio Poggi

Antonio Poggi posted over 5 years ago Admin

Thank you Deborah, 

For more complex email you can use the Python modules email.MIMEMultipart and email.MIMEText,  found in the basic Python libraries.

N.B. we will be adding the email block in Omniscope 2019 (in Q1). In the meantime your script is a super valid and useful alternative!



2 Votes

Login or Sign up to post a comment