Topic: How-To: Send Email from a Nion

We've recently received a customer request for information on how to send event notification emails from a Nion. Here's a quick and dirty solution for those with similar needs. This is a rudimentary implementation of an SMTP client in a Python script.

The intent of the script is to allow you to send simple notifications to your email from a Nion. The advantage of this script is that it is very compact and needs only the built-in 'socket' Python library. Thus, you can easily copy/paste this into your projects with no external dependencies.

Please note that this script has many limitations and is in no way intended to be a general solution for sending Email. In particular there are no provisions here for multiple recipients, attachments, or non-ascii character sets. This source code is supplied without warranty or support from Peavey Electronics. We are, however, always happy to answer any questions you may have regarding the Nion.

The necessary inputs to the script are:
    server - IP Address of an accessible SMTP server. This server must NOT require authentication.
    sender - The email address of the sender.
    recipient - The email address of the recipient.
    msg - The email message. This MUST include the headers of the message as well.

The format for an email message is:
Header line\r\n
Another header line\r\n
Message body

The script example below includes a simple example of building a message from component parts.


import socket

def read_smtp_response(sock):
  response = "";
  for i in range(10):
    response += sock.recv(1024);
    lines = response.split('\n');
    for line in reversed(lines):
      code = line.split(' ')[0];
      if code.isdigit():
        return code;
  return "error parsing server response";

def send_email(server, sender, recipient, msg):
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
  sock.connect((server, 25));
  code = read_smtp_response(sock);
  if (code != '220'):
    return "Server not accepting connections: " + code;
  #helo phase
  sock.send("helo nion\r\n");
  code = read_smtp_response(sock);
  if (code != '250'):
    return "Server rejected connection: " + code;

  #mail from phase:
  sock.send("mail from:<" + sender + ">\r\n");
  code = read_smtp_response(sock);
  if (code != '250'):
    return "Server rejected sender: " + code;

  #rcpt to phase:
  sock.send("rcpt to:<" + recipient + ">\r\n");
  code = read_smtp_response(sock);
  if (code != '250'):
    return "Server rejected recipient: " + code;

  #data phase:
  code = read_smtp_response(sock);
  if (code != '354'):
    return "Server rejected message data: " + code;
  code = read_smtp_response(sock);
  if (code != '250'):
    return "Server rejected message: " + code;

  #quit phase:
  #ignore response, just close socket now
  return "Message sent."

#Any or all of these constants could, of course, be inputs to your script block
server = "";
subject = "Testing from Python";
fromaddr = "you@domain.suf";
toaddrs  = "you@domain.suf";
body = "This is an email message from a Nion!";

# create email message
msg = "Subject: %s\r\nFrom: %s\r\nTo: %s\r\n\r\n%s" %(subject, fromaddr, toaddrs, body);

#send email message
result = send_email(server, fromaddr, toaddrs, msg);

#send result to message field in your script block


Frank Vernon
Sr. Software Engineer
Peavey Electronics Corporation


Re: How-To: Send Email from a Nion


Thanks for sharing this with everyone!

I have a quick question, however...

What triggers this script to send the email message?



Josh Millward
Burnt Orange Studios


Re: How-To: Send Email from a Nion


You can use any control in your project as a trigger for your email script. You could even embedded control values from your project in the email by simply wiring them to your script block and appending them to the email message.

Here's a contrived example: Say you want to get an email if a Nion ever goes into or out of a muted state. This is a possible indication that there is a fault on the unit.

Create a Python script block and give it 1 input and no output.

Wire the unit 'Muted' LED from the Mute page in the Nion Device to your new script block.

Copy/Paste the email example script into a new script.

Change the subject variable to be:
subject = "Nion Mute State";

Change the body variable  to be:
body = "The Nion is presently ";
if (inputs[0].position_get()):
  body += "muted."
  body += "unmuted."

Assign your new script to the script block and you are ready to go. You should now get an email when the mute state changes along with an indication of the state itself.



Re: How-To: Send Email from a Nion

Very cool!

Thank you for the clarification, Frank!

Josh Millward
Burnt Orange Studios