[Solved] Email with Python?

On our last NAS (Buffalo), my company was able to send a daily status update to monitor free space on our backup device. Now that we have switched over to WD NAS (My Cloud Mirror) 8TB, we are no longer able to do this. After doing some Googling, I found this: https://community.wd.com/t/sending-email-with-python-on-wd-my-cloud/96006

After logging into SSH and creating my python script (send_email.py), I keep running into an error message:

ImportError: cannot import name SMTP_SSL

Has anyone been able to get this to work, or something similar?

NOTE: I am not asking for troubleshooting on this particular script… I’m asking if you have found a different way to accomplish the same thing, please share.

Thanks a bunch!

One of these threads might help:

Thanks for the answers, but I had already looked at those and did not get anywhere. My script is named “send_email.py”, so those answers didn’t apply to me.

Eventually, I figured this out. Here’s what I did:

I changed SMTP_SSL to just just SMTP. Since my situation was a little different in that we are using an open relay internally, I commented out the login portion of the script. Then… I created a BASH script to keep the emailing and the free space check broken out for troubleshooting (yes, I could have modified the Python script to do all of this for me, but I chose not to do it that way). After that, I just called the BASH script from the crontab.

Here’s my code for those who need to do this in the future:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from email.header       import Header
from email.mime.text    import MIMEText
from getpass            import getpass
from smtplib            import SMTP

import sys

#edit the line below
login, password, server, recipients = "redacted@redacted.com", "thepassword", "smtp.redacted.local", "redacted@redacted.com"

#send email
subject = sys.argv[1]
body = sys.argv[2]

msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = login
msg['To'] = recipients

s = SMTP(server, 25, timeout=10)
 #s.login(login, password) #Uncomment if you need authentication
 s.sendmail(msg['From'], recipients, msg.as_string())
except Exception, error:
 print "Unable to send e-mail: '%s'." % str(error)


message=`df -h`;
python /send_email.py "Backup NAS Status: $(df -h /dev/md1 | awk '{ print $5 }' | tail -n 1) Used" "$message"