Sending email with python on WD My Cloud

first SSH to your device, then input the following:

cd /
nano mail.py

change your email, password, recipient and smtp server in the following then paste on terminal (right click on putty):

#!/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_SSL

import sys

#edit the line below
login, password, server, recipients = "youremail@gmail.com", "password", "smtp.gmail.com", "recipient@domain.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_SSL(server, 465, timeout=10)
s.set_debuglevel(1)
try:
    s.login(login, password)
    s.sendmail(msg['From'], recipients, msg.as_string())
except Exception, error:
    print "Unable to send e-mail: '%s'." % str(error)
finally:
    s.quit()

to save the file, type Ctrl + X , then Y to confirm

you can then use the script as follows:

python mail.py "subject" "message"

or to email the contents of a file (e.g. a log file):

message=`cat file.log`;python mail.py "subject" "$message"
1 Like

good find!

This program is all on one line, is it supposed to be that way? I substituted my login, password, and recipient address where I thought they should go, made it executable by running “chmod +x mail.py”, and then tried using it as you said, but nothing seemed to happen.

Indeed it wasn’t supposed to be in one line and indentation in python is important. It seems the forum update messed up with the formatting.

I updated the code above; you can give it another try.

Thanks, it works great! Now I just have to figure out how to scan my shares for changes and then use your program to email notifications to my brother so he doesn’t have to keep asking me “what’s new to watch”.

Great! That would depend on how you define “change”. One viable option would be to periodically poll for changes (every x hours) and then send a report: datetime - how to use shell script checking last changed time of a file - Stack Overflow

Ideally, after I write “Episode Name.*” to the directory “\\MYCLOUD\TV\Show Name”, I’d like it to send an email saying “Show Name-Episode Name.* now available”. However, most of the scripts I’ve found by googling use something called “inotify”, which doesn’t seem to be available on the MyCloud.

Another way I’ve seen that doesn’t need inotify is to periodically generate a listing of a directory using “ls” and then compare it to the last one to see if anything has changed. My scripting/programming skills are not up to doing this myself from scratch, and I haven’t yet found one that I can figure out how to modify to work as I want.

Thanks for your help!

The command below will list those files modified in the last day and put the results in filename which you could then email the file.
find -mtime -1 -print >

RAC

OK, I have the following script named “notify_new.sh” located in / along with the “mail.py” script:
#!/bin/bash
find /var/media/TV -type f -mtime 1 -print > whatsnew.txt
message=$(cat whatsnew.txt) # I can’t figure out how to type a backtick, let alone get it to display correctly on this forum!! LOL
python mail.py “What’s New” “$message”

I made it executable, then ran ./notify_new.sh, and got the following in the email body:

/var/media/TV/SHOWNAME/EPISODE.mkv

I would like it to read “\\MYCLOUD\SHOWNAME\EPISODE.mkv” but all of the escaped characters necessary to do this with sed make my head swim!

Do you need the MYCLOUD in the name? if not you could cd to /var/media/TV
Then do the find . -type f -mtime 1 -print > whatsnew.txt. Tat way you will get ./SHOWNAME/EPSODE.mkv

RAC

Thanks to everybody’s help I’ve mostly got everything the way I want it, except for sending the mail to multiple recipients. I tried putting multiple email addresses in the appropriate place separated by commas, and with a comma followed by a space, but only the first address gets the mail, although the “To:” header in the received mail contains both addresses.

In order to get it to work, I had to make changes to mail.py, here is the version that worked for me:

#!/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_SSL

import sys

#edit the lines below
login, password, server = “sender@gmail.com”, “PASSWORD”, “smtp.gmail.com” ######CHANGED
recipients = [‘RECIPIENT1@EXAMPLE.COM’, ‘RECIPIENT2@EXAMPLE.COM’] ######CHANGED

#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’] = ", ".join(recipients) ######CHANGED

s = SMTP_SSL(server, 465, timeout=10)
s.set_debuglevel(1)
try:
s.login(login, password)
s.sendmail(msg[‘From’], recipients, msg.as_string())
except Exception, error:
print “Unable to send e-mail: ‘%s’.” % str(error)
finally:
s.quit()

Thanks again for your help!

ps How do you get the code blocks to display properly here?

the email by using python won’t work for firmware v 2.xxxxx.
It is a busybox – its python does not have SMTP_SSL.

Not sure why it didn’t work. But SMPT_SSL is in /usr/lib/python2.7/smtplib.py. This file is on gen2 firmware 2.31.174