Page 1 of 1

Run command embedded in GMail mail

Posted: Sat Aug 02, 2014 2:07 pm
by chorlton2080
Is anyone able to suggest a script which will check for emails from a nominated GMail account and parse a command contained within?

Commands will be limited to 'take picture and email back' and 'restart pi'.

Scratching head all day on this!

Thank you

Re: Run command embedded in GMail mail

Posted: Sat Aug 02, 2014 2:48 pm
by davef21370
Google "Python read email" and you'll find plenty of examples.
Once you have your mail as a string use the string's find function to search for your text.

Dave.

Re: Run command embedded in GMail mail

Posted: Sun Aug 03, 2014 11:40 am
by chorlton2080
The basics of this need are answered thus:

http://stackoverflow.com/questions/1225 ... ith-python

This relies on a conventional POP connection so is not limited to GMail or its RSS feature.

Re: Run command embedded in GMail mail

Posted: Sun Aug 03, 2014 12:50 pm
by davef21370

Re: Run command embedded in GMail mail

Posted: Sun Aug 03, 2014 4:51 pm
by chorlton2080
Thanks for the link and the advice.

I'm now able to issue a deliberately limited set of instructions to my Pi via email without having to keep a port open at the router, as was the requirement.

I've used the built-in Python modules rather than download new for reasons of compatibility and future availability.

I'll post the script if anyone requests.

Re: Run command embedded in GMail mail

Posted: Mon Aug 04, 2014 9:28 am
by PangolinPaws
This is a great idea.

I built a motion-sensitive wildlife camera which posted it's images to Twitter, but also looked out for a 'start' and 'stop' command from a specific account.

I probably lost a few followers because they were tired of seeing 'PI_STOP' and 'PI_START' popping up on their feed over and over... I think I'll have to copy you and set up an email interface instead.

Thanks for the inspiration! ^_^

Re: Run command embedded in GMail mail

Posted: Thu Aug 07, 2014 12:48 pm
by n3tm4n
chorlton2080 wrote: I'll post the script if anyone requests.
I would be interested, I'm currently doing something similar using SMS text messages, but using GMAIL would be easier, especially if I can reuse an existing script.

Thanks
Jon.

Re: Run command embedded in GMail mail

Posted: Sat Aug 16, 2014 5:04 pm
by chorlton2080
Folks

For those who asked me to share the working code, sorry about the delay.

I'm using this to reboot the Pi (really as a test in calling basic shell commands) and in running Python scripts: both triggered by email. Remember to use the full path (including for shell commands). This is much better than leaving a port open for SSH in my opinion. I make no apologies for the script style, but it does work.

I have this run every 5 minutes using cron (let me know if you need advice on this).

Code: Select all

import subprocess
import poplib
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('yourgmailusername')
pop_conn.pass_('yourpassword')

#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print message['subject']
    if 'Call' in message['subject']:
       execfile("[FULL PATH TO PYTHON SCRIPT WHICH EMAILS LATEST SECURITY CAM CAPTURE")
       print "I sent the latest file on request"
    elif 'Reboot' in message['subject']:
       subprocess.call("/sbin/shutdown -r now", shell=True)
    else:
       print "No command in email. I did nothing"
pop_conn.quit()