Jump to content
Ultimaker Community of 3D Printing Experts

script to send an email if the printer pauses


aag
 Share

Recommended Posts

Posted (edited) · script to send an email if the printer pauses

you will have to install a mail transport, such as postfix. Then, simply start this script on the printer

 

#!/usr/bin/python3
### BEGIN INIT INFO
# Provides:          pause_alert.py
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

import systemd.journal
import socket
from time import sleep
import smtplib
import email
from smtplib import SMTP
from email.message import EmailMessage
import datetime


def main():
  j = systemd.journal.Reader()
  j.seek_tail()
  j.get_previous()
  while True:
    event = j.wait(1000000)
    if event == systemd.journal.APPEND:
      for entry in j:
         print (entry['MESSAGE'] + " " + str(datetime.datetime.now()))
         alertmail(entry['MESSAGE'])

def alertmail(logEntry):
    # returns first occurrence of Substring
    trigger_text = 'pause'
    result = logEntry.find(trigger_text)
    if (logEntry.find(trigger_text) != -1):
        print ("Contains '" + trigger_text + "' at index:", result)
        msg = EmailMessage()
        msg.set_content(str(datetime.datetime.now()) + " \n " + logEntry)
        msg['Subject'] = ' '.join(logEntry[24:].split())
        msg['From'] = "ultimaker@mail.bellariastrasse.com"
        msg['To'] = "adriano.aguzzi@usz.ch"

        smtp = smtplib.SMTP('mail.bellariastrasse.com', port='587')
        smtp.ehlo()  # send the extended hello to our server
        smtp.starttls()  # tell server we want to communicate with TLS encryption
        smtp.login('ultimaker@mail.bellariastrasse.com', 'scrap10')  # login to our email server

        # send our email message 'msg' to our boss
        smtp.send_message(msg)
        smtp.quit()  # finally, don't forget to close the connection
    else:
        print ("Doesn't contain substring '" + trigger_text +"'")

if __name__ == '__main__':
    main()

 

Edited by aag
updated code
  • Like 1
Link to post
Share on other sites

Posted · script to send an email if the printer pauses

This works on S5?  Very cool.

 

@Smithy maybe you could add a feature that installs postfix and then installs the above script?  Ha ha - your tool is getting more and more complicated all the time.  🙂

 

 

  • Link to post
    Share on other sites

    Posted · script to send an email if the printer pauses

    @aag Great idea, but should work without a local installed mail transport agent as well, or do I miss something? You connect directly to the destination. 
     

    @gr5 yes, should be no problem, if aag agree with it.?

  • Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    • Our picks

      • Ultimaker showcase | April 2022 | 4pm CEST | 10am EDT
        I'm excited because I've personally produced this showcase event, so if you are curious what else I'm up to nowadays, come check it out and say hi in the chat! It would mean a lot! 
          • Thanks
          • Like
        • 4 replies
      • New here? Get ahead with a free onboarding course
        Hi,
         
        Often getting started is the most difficult part of any process. A good start sets you up for success and saves you time and energy that could be spent elsewhere. That is why we have a onboarding course ready for
        Ultimaker S5 Pro Bundle, Ultimaker S5, Ultimaker S3 Ultimaker 2+ Connect.   
        They're ready for you on the Ultimaker Academy platform. All you need to do to gain access is to register your product to gain free access. 
        Ready? Register your product here in just 60 seconds.
          • Like
        • 11 replies
    ×
    ×
    • Create New...