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
\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:
sock.send("data\r\n");
code = read_smtp_response(sock);
if (code != '354'):
return "Server rejected message data: " + code;
sock.send(msg);
sock.send("\r\n.\r\n");
code = read_smtp_response(sock);
if (code != '250'):
return "Server rejected message: " + code;
#quit phase:
sock.send("quit\r\n");
#ignore response, just close socket now
sock.close();
return "Message sent."
#Any or all of these constants could, of course, be inputs to your script block
server = "0.0.0.0";
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
message.set_string(result);
-------------------------------------------------------------------------------------------------------------------
Frank Vernon
Sr. Software Engineer
Peavey Electronics Corporation