Rishabhg
Posts: 24
Joined: Thu Jan 30, 2014 9:41 am

Send email to raspberry pi to do tasks

Thu Jan 30, 2014 9:59 am

Hello,

I am new to raspberry pi and do not have much experience in programming related to internet.
My project involves sending a mail to the pi asking it to capture an image and send that image as an attachment via email back to me. I followed some tutorials and resources online and am now able to capture an image and send it using a gmail account I created for the pi. I used two separate Python scripts to do that. What I haven't been able to do is to tell the pi to do these tasks through email. Is it possible to write a Python script to make the pi read the contents of the email that I send to it from my address and then take actions accordingly?

I checked this tutorial http://mitchtech.net/connect-raspberry- ... tter-more/ . This tutorial doesn't read the contents of the body of the email.

I would really appreciate any kind of helpful suggestion.

Thank You
Rishabh

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 11:12 am

"procmail" is a really nice, venerable program for this sort of thing.

Google it.

Not sure if it is "in the repos" or not. I've been using it since the early 90s and have compiled it from source on many platforms.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Rishabhg
Posts: 24
Joined: Thu Jan 30, 2014 9:41 am

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 6:49 pm

I just looked into procmail. I found it to be a little complicated. I was actually hoping to import some sort of library and use its functions, like I did for sending email. I used the smtp library. I actually followed this http://solvingmytechworld.blogspot.in/2 ... cript.html

Can something similar be done for reading mails?

User avatar
DougieLawson
Posts: 39618
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 6:56 pm

Use Twitter.

That's well tested technology. I was driving Twitter from a transaction running on a z/OS mainframe in Greenford to tweak the Xmas tree lights in Hursley about three or four years ago (long before Twitter banned the use of basic authentication).

You can use a protected account if you need to restrict who can control your system. Tweeting and reading tweets is trivial. I'm using python with Tweepy to do that stuff. You a) know who sent the tweet, b) know it won't be more than 140 chars c) can easily send a reply with success or failure.

Reading and parsing email is just a nasty design.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 7:11 pm

Hi,
Rishabhg wrote:Can something similar be done for reading mails?
You can redirect incoming mail into script/binary, which parses the content and performs some related action. The message content is accessible to the code via stdin.

You need to have a mail server installed/configured and create alias for the related email address, which points to your script.
Years ago I did this:
- installed & configured postfix
- added alias "robot" in /etc/aliases (line like robot: |/my/script/here
- created a script (due to some specific reason it was PHP, but could be anything able to read stdin)

When I send email to robot@my.domain, the script is launched and email message content (including header) is passed to the script for processing.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 7:14 pm

Hi,
DougieLawson wrote:Reading and parsing email is just a nasty design.
Who told you such a big lie ?


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

User avatar
DougieLawson
Posts: 39618
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 7:21 pm

FLYFISH TECHNOLOGIES wrote:Hi,
DougieLawson wrote:Reading and parsing email is just a nasty design.
Who told you such a big lie ?
Email is a tired and ugly protocol. And it's horrible parsing anything with regexes. 'Nuf said.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 7:31 pm

Hi,
DougieLawson wrote:
FLYFISH TECHNOLOGIES wrote:Hi,
DougieLawson wrote:Reading and parsing email is just a nasty design.
Who told you such a big lie ?
Email is a tired and ugly protocol. And it's horrible parsing anything with regexes. 'Nuf said.
I see,... you mix in the same box very different things - emails, SMTP, regex...

I don't want to start any "holy war", I just wanted you to expand the statement beyond one-liners.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

User avatar
fracai
Posts: 13
Joined: Thu Jan 31, 2013 8:41 pm

Re: Send email to raspberry pi to do tasks

Thu Jan 30, 2014 8:40 pm

DougieLawson wrote:Email is a tired and ugly protocol. And it's horrible parsing anything with regexes. 'Nuf said.
Why would you need to parse the entire message?

Just put something like "Command:blah" in the subject or body and then search for "Command:([^\r\n]+)" and you've got your command as capture 1.
You can get more complicated by adding things like "Arguments:blah,bleh,foo,12" and searching a similar regex. Then split on commas.

There's no need to parse the entire message.

Note that allowing commands to be sent via email potentially opens you up to security issues.
Don't run arbitrary code from the message (someone could send you "rm -rf /").
Preferably have a way to authenticate the sender of the command (GPG sign each message?).
Preferably have a way to ignore replayed messages (store the received messages on the Pi and ignore anything that has already been received).
i am jack's amusing sig file

Rishabhg
Posts: 24
Joined: Thu Jan 30, 2014 9:41 am

Re: Send email to raspberry pi to do tasks

Sat Feb 08, 2014 3:04 pm

Ok. Can you write a sample Python script for extracting the subject of the latest unread email from a specific sender?
That would be really helpful and clarify the idea a lot.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Send email to raspberry pi to do tasks

Sun Feb 09, 2014 12:22 am

You have created a gmail account for the Pi, so it's not necessary to configure the Pi as a mail host. You just need to write a program on the Pi that looks at the mailbox at gmail.

There is a simple example here to retrieve a list of email messages:
http://docs.python.org/2/library/poplib.html

There are more functions to let you do things with each message. Look at the documentation for the poplib module. You might also like to look at the imaplib module, since POP is a little old these days.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Sun Feb 09, 2014 12:36 am

DougieLawson wrote:
FLYFISH TECHNOLOGIES wrote:Hi,
DougieLawson wrote:Reading and parsing email is just a nasty design.
Who told you such a big lie ?
Email is a tired and ugly protocol. And it's horrible parsing anything with regexes. 'Nuf said.
SMTP is a wonderful protocol. Simple, Human readable, robust. What makes it ugly is dealing with all the anti-spam, attachments, having to deal with Microsoft and their stupidity, etc etc.

I know of very few protocols that have been so widely adopted and implemented on so many platforms as SMTP. I have even seen SMTP for the Amiga, mainframes, and microcontrollers.
Last edited by Richard-TX on Sun Feb 09, 2014 12:55 am, edited 1 time in total.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Sun Feb 09, 2014 12:42 am

What we did when we wanted to send commands via email was to use the dot-forward file mechanism.

You can pipe email messages into any command and you don't have to parse a thing. A simple grep would suffice.

For example, it you sent an email with the message "TuRn OfF PuMp" a simple grep would match and that is all you need.

Just use your imagination and you will find you could do anything you wanted via email.

Exim and sendmail both are capable of handling .forward files.

http://www.freebsd.org/cgi/man.cgi?quer ... ormat=html
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Sun Feb 09, 2014 12:47 am

Don't forget that you have to uuencode binary files before emailing.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
chrisryall
Posts: 155
Joined: Wed Nov 27, 2013 11:45 am
Location: Wirral UK
Contact: Website

Re: Send email to raspberry pi to do tasks

Sun Feb 09, 2014 8:06 am

Agree, all this is possible. But it's no way elegant?

"Excuse me, could you please show me the way to Dublin"
"Well, if I were going to Dublin … I certainly wouldn't start from here"

Rishabhg
Posts: 24
Joined: Thu Jan 30, 2014 9:41 am

Re: Send email to raspberry pi to do tasks

Wed Feb 19, 2014 2:55 pm

I have written a Python script which seems to be doing the job

Code: Select all

import imaplib
import email
import time

def checkEmail():
        
    mail = imaplib.IMAP4_SSL('imap.gmail.com');
    mail.login('raspberrypi_emailid@gmail.com','raspberrypi_password');
    mail.list();  # Gives list of folders or labels in gmail.
        
    count = 0

    while count < 6:
        try:
            # Connect to inbox
            mail.select("inbox"); 
    
            # Search for an unread email from user's email address
            result, data = mail.search(None,'(UNSEEN FROM "my_email_id@gmail.com")');
    
            ids = data[0]   # data is a list
            id_list = ids.split() # ids is a space separated string

            latest_email_id = id_list[-1] # get the latest
            result, data = mail.fetch(latest_email_id, "(RFC822)");

            raw_email = data[0][1];

            recv_msg = email.message_from_string(raw_email)

            if(recv_msg['Subject'] == "Allow person"):
                print("The person has been granted access. Hurray!!!")
            else:
                print("ACCESS DENIED!!!")
                
            count = 6

        except IndexError:
            time.sleep(30*1)
            if count < 5:
                count = count + 1
                continue
            else:
                print("Sorry,No reply in the last 3 minutes.")
                count = 6

                
checkEmail()
It checks the inbox for 6 times in 3 minutes.

Anna6
Posts: 1
Joined: Wed Feb 19, 2014 3:15 pm

Re: Send email to raspberry pi to do tasks

Wed Feb 19, 2014 3:19 pm

DougieLawson wrote:Use Twitter.
DougieLawson wrote:Reading and parsing email is just a nasty design.
DougieLawson wrote:'Nuf said.
No, seriously, how can you say twitter is better than email?
And how commands sent via twitter are different to parse than via email?

Mails aren't very reliable, but twitter is much worst: going down all the time.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Send email to raspberry pi to do tasks

Wed Feb 19, 2014 3:23 pm

I can't imagine how this could possibly be any easier than using procmail.

But then again, to each his own…
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Thu Feb 20, 2014 3:43 am

chrisryall wrote:Agree, all this is possible. But it's no way elegant?

"Excuse me, could you please show me the way to Dublin"
"Well, if I were going to Dublin … I certainly wouldn't start from here"

A .forward is both elegant and robust. It also as fast as email can deliver as there is no scanning of the mailbox file.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Thu Feb 20, 2014 3:53 am

You could also use the aliases file. That way there doesn't have to be an actual account name matching the recipient name.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Thu Feb 20, 2014 4:37 am

There is one other thing to consider.

What is to prevent anyone from sending an email to your Rpi telling it to shutdown?

A little security is needed.

a simple message like

PaSsWoRd my_command

Might be enough....or not.


Don't forget that you might have permissions/ownership issues. I would NOT add the mail daemon owner as a member of the sudoers.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Send email to raspberry pi to do tasks

Thu Feb 20, 2014 4:45 am

I installed and set up exim to do either .forward or /etc/aliases in about 20 minutes. I also set up exim to forward root's mail to my gmail account.

If you need help, let me know.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Fred_Scuttle
Posts: 1
Joined: Sun Feb 23, 2014 6:11 pm
Location: Surrey, United Kingdom

Re: Send email to raspberry pi to do tasks

Sun Feb 23, 2014 6:30 pm

I'm doing something similar to Rishabhg in that I send an email to my Raspberry Pi GMail account for it to carry out tasks. It checks the GMail subject line and the recipient email address and then carries out the appropriate task. I use feedparser for this. However, once it has carried out the task how can I mark the email which contains the instructions as having been read?

Code: Select all

details = feedparser.parse("https://" + PiEmail + ":" + PiPwd + "@mail.google.com/gmail/feed/atom")
FromEmail = details['items'][i].author_detail.email
Subject = details['items'][i].title.lower()

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: Send email to raspberry pi to do tasks

Tue Feb 25, 2014 2:43 pm

https://github.com/txt3rob/sms-command-server

can be modified to do what is required.

https://github.com/txt3rob/PiCam-image-requester

also can be modified to do what is required
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

crazylooser017
Posts: 32
Joined: Wed Dec 31, 2014 4:52 pm

Re: Send email to raspberry pi to do tasks

Sat Jan 10, 2015 4:48 pm

Richard-TX wrote:I installed and set up exim to do either .forward or /etc/aliases in about 20 minutes. I also set up exim to forward root's mail to my gmail account.

If you need help, let me know.
Hi, I'm making the set up in exim4 to send messages from RPi, hoping you can help me a little bit with when the RPi needs to read a message

Return to “Networking and servers”