digi_smtplib SMTP Library

I am trying to write a Python app that sends an e-mail alert. I am using the digi_smtplib.py file that is in the Dia /lib folder. When I try to create an instance of the digi_smtplib.SMTP(“locahost”) class, I got the following exception:

Exception in thread Thread-1:
Traceback (most recent call last):
File “./threading.py”, line 442, in __bootstrap
File “WEB/python/SMTPHandler.py”, line 32, in send_email
s = digi_smtplib.SMTP(“localhost”)
File “WEB/python/digi_smtplib.py”, line 255, in init
(code, msg) = self.connect(host, port)
File “WEB/python/digi_smtplib.py”, line 290, in connect
self.sock.connect((host, port))
File “”, line 1, in connect
error: (111, ‘Connection refused’)

The example in the documentation for the SMTP class uses “localhost”. Does the ConnectPort X4 not have an onboard SMTP server? I browsed the web site on my X4 but I did not see any settings for one.

If it does not have an onboard SMTP server, I could use gmail (http://mail.google.com/support/bin/answer.py?hl=en&answer=13287) but I think there are some issues with the digi_smtplib library. First, can the host name be a domain name (such as smtp.gmail.com)? Second, does it support authentication? Third, does it support STARTTLS?

I discovered that the digi_smtplib.py file is just a slightly modified version of the smtplib.py file in the python standard library. I downloaded the source code for python version 2.4.6 and grabbed smptlib.py and hmac.py from it. The library looks almost identical except it appears to have support for authentication and TLS. I haven’t tried it yet but this looks like something I could use with gmail.

This is an interesting thread. Please keep us posted on how this goes for you.

Hello Red0x,

Very interesting set of questions!

The digi_smtplib.py IS just a modified version of the python standard library version. The reason why it was modified from the standard was to work around the getfqdn() calls that the standard version makes. An earlier version was also used to reduce the number of dependencies required at the cost of functionality.

The interesting aspect of this is, on the latest set of firmware (82001536_E1 I believe), the getfqdn calls now work, and using a smtplib.py from the standard library should be 1 step closer to possible. Additional support is made in an upcoming version of the DIA where the getfqdn calls are supported and input such as DNS names are supported in the smtp driver in the DIA.

In checking it out briefly, it seems the 2.4.x version of the smtplib requires some stuff that is not implemented on the device. import hmac->imports hashlib-> Requires C implementations of hashing libs. This would prevent TLS from working, as far as I can tell.

It works! I can securely log into my gmail account from the gateway and send e-mail. I even brought in the MIME classes from python-2.4.6 so I could send more robust e-mails. So far, I’ve only tried MIMEText e-mails but I don’t see why other types wouldn’t work as well.

Hi RedOx,

I’m trying to do the same thing like you performed. Using the Dia, I would like to send email notifications when an alarm is triggered. (via smtp.gmail.com port 465 or 587).

Viewing smtp.py (instead of digi_smtplib.py), the code does not have the settings for authentication? And I see that the smtp.py imports the module digi_smtplib.py. How do I proceed to edit the code (smtp.py or digi_smtplib.py) so I can write it on a yml file like when running the green_getting_started.yml demo?

Totally noob here, would appreciate greatly if you could explain what you have tweaked :slight_smile:

Has anybody else done this too? I would like to learn how to do so

I got it working with the following changes:

smtplib_digi.py
line 200 PORT=0 here, so i used pre-defined SMTP_PORT 25
def init(self, host = ‘’, port = SMTP_PORT):

findings:
when using my PC as a gateway to the internet, with the digi X2 gateway on LAN, urls do not resolve from the X2. IP’s work just fine.

i used my cable ISP’s SMTP relay, because they block port 25.

from the PC, i pinged the smtp url to get the IP
then hardcoded the IP - it works just fine, but the X2 is still behind the gateway on a LAN.

the mail was successfully sent.

Here ya go:

###############################################################################

modified digital_example.py

###############################################################################

Make known the zip files

import sys
sys.path.append(“WEB/python/DigiXBeeDrivers.zip”)
sys.path.append(“WEB/python/Smtplib_python.zip”)

Import the necessary library from the zip file

import xbeedin

Import the zigbee library for ddo_get_param function

import zigbee

Import the smtp Lib hdr

import smtplib_digi
print “Library: smtplib_digi OK”

The XBee Digital Adapter’s 802.15.4 64bit address

Modify the destination to match the digital adapter’s address

DESTINATION = “[00:13:a2:00:40:56:19:68]!”

fromaddr = “@”
toaddrs = “@”
subject = “EMAIL TEST 01 - Sent from x2 Gateway !!!”
text = "sensor location [00:13:a2:00:40:56:19:68]! "
smtp_server = ‘68.6.19.4’
#‘smtp.west.cox.net’ FAILS ping your ISP to get your relay IP

Add the From: and To: headers at the start!

msg = ("From: %s
To: %s
Subject: %s
"
% (fromaddr, toaddrs, subject))
msg = msg + "
" + text

print "Message length is " + repr(len(msg))

server = smtplib_digi.SMTP(smtp_server)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()