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
-
- Posts: 128
- Joined: Sun Dec 23, 2012 9:44 pm
- davef21370
- Posts: 897
- Joined: Fri Sep 21, 2012 4:13 pm
- Location: Earth But Not Grounded
Re: Run command embedded in GMail mail
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.
Once you have your mail as a string use the string's find function to search for your text.
Dave.
Apple say... Monkey do !!
-
- Posts: 128
- Joined: Sun Dec 23, 2012 9:44 pm
Re: Run command embedded in GMail mail
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.
http://stackoverflow.com/questions/1225 ... ith-python
This relies on a conventional POP connection so is not limited to GMail or its RSS feature.
- davef21370
- Posts: 897
- Joined: Fri Sep 21, 2012 4:13 pm
- Location: Earth But Not Grounded
Re: Run command embedded in GMail mail
Apple say... Monkey do !!
-
- Posts: 128
- Joined: Sun Dec 23, 2012 9:44 pm
Re: Run command embedded in GMail mail
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.
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.
- PangolinPaws
- Posts: 89
- Joined: Wed Mar 05, 2014 9:04 pm
- Location: Wiltshire, UK
- Contact: Website
Re: Run command embedded in GMail mail
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! ^_^
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
Re: Run command embedded in GMail mail
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.chorlton2080 wrote: I'll post the script if anyone requests.
Thanks
Jon.
-
- Posts: 128
- Joined: Sun Dec 23, 2012 9:44 pm
Re: Run command embedded in GMail mail
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).
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()