chorlton2080
Posts: 126
Joined: Sun Dec 23, 2012 9:44 pm

Run command embedded in GMail mail

Sat Aug 02, 2014 2:07 pm

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

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Run command embedded in GMail mail

Sat Aug 02, 2014 2:48 pm

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.
Apple say... Monkey do !!

chorlton2080
Posts: 126
Joined: Sun Dec 23, 2012 9:44 pm

Re: Run command embedded in GMail mail

Sun Aug 03, 2014 11:40 am

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.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Run command embedded in GMail mail

Sun Aug 03, 2014 12:50 pm

Apple say... Monkey do !!

chorlton2080
Posts: 126
Joined: Sun Dec 23, 2012 9:44 pm

Re: Run command embedded in GMail mail

Sun Aug 03, 2014 4:51 pm

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.

User avatar
PangolinPaws
Posts: 89
Joined: Wed Mar 05, 2014 9:04 pm
Location: Wiltshire, UK
Contact: Website

Re: Run command embedded in GMail mail

Mon Aug 04, 2014 9:28 am

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! ^_^
https://github.com/PangolinPaw

n3tm4n
Posts: 29
Joined: Tue Jun 10, 2014 11:34 am
Location: East Midlands, UK
Contact: Website

Re: Run command embedded in GMail mail

Thu Aug 07, 2014 12:48 pm

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.

chorlton2080
Posts: 126
Joined: Sun Dec 23, 2012 9:44 pm

Re: Run command embedded in GMail mail

Sat Aug 16, 2014 5:04 pm

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()

Return to “Python”