RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Receiving emails on Pi

Thu Jan 21, 2016 7:30 pm

I have successfully followed the advice given in the following link:
http://mitchtech.net/connect-raspberry- ... tter-more/
to send emails from my Pi.

In order to move to the next stage of my program I need to be able to receive emails.

I have used the following code from the same website link'

Code: Select all

#!/usr/bin/env python
import feedparser

USERNAME = "###########@gmail.com"
PASSWORD = "#########"

response = feedparser.parse("https://" + USERNAME + ":" + PASSWORD + "@mail.google.com/gmail/feed/atom")
unread_count = int(response["feed"]["fullcount"])

for i in range(0,unread_count):
	print (" + str((i+1)) + "/" + str(unread_count) + ") + response['items'][i].title
However, when running the the code, I get the following error:

Traceback (most recent call last):
File "/home/pi/Receive email on #######", line 2, in <module>
import feedparser
ImportError: No module named 'feedparser'

I (thought I) have installed 'feedparser' using:
sudo pip install feedparser

and the system reported:
Successfully installed feedparser

I would welcome advice regarding what I am doing wrong.
Thank you.

User avatar
topguy
Posts: 5569
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Receiving emails on Pi

Thu Jan 21, 2016 7:41 pm

Based on previous similar posts.
Are you running Python 2 or Python 3 ?

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Thu Jan 21, 2016 8:29 pm

I am running Python 3.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Receiving emails on Pi

Thu Jan 21, 2016 8:48 pm

RDS wrote:I (thought I) have installed 'feedparser' using:
sudo pip install feedparser
and the system reported:
Successfully installed feedparser
I would welcome advice regarding what I am doing wrong.
Thank you.
I am running Python 3.
Just to drive you completely bug house nutty, Python allows you to install your libraries against various versions of itself without making that clear. As far as I recall, if you use pip, you get stuff installed to some version of Python 2.7. If you use pip3, it ends up with the default version of Python 3.x. But I think this depends on how pip or pip3 was installed. whereis under Ubuntu 14.04 LTS for me shows a pip3 is installed but not a pip. This same outcome is true on x86 Wheezy on another box. You could have both installed or perhaps you have pip as the name for your version of pip3, who knows?

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Receiving emails on Pi

Thu Jan 21, 2016 8:49 pm

RDS wrote:I am running Python 3.
That tutorial recommends the installation of these

Code: Select all

sudo apt-get install python-pip python2.7-dev
Which suggests it may only work work python2.7
Yep

Code: Select all

[email protected] ~ $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import feedparser
>>>
[email protected] ~ $ python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import feedparser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named feedparser
>>>

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Thu Jan 21, 2016 9:41 pm

It seems strange though that the code in the same article, to send an email runs perfectly on Python 3.

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Receiving emails on Pi

Thu Jan 21, 2016 9:54 pm

RDS wrote:It seems strange though that the code in the same article, to send an email runs perfectly on Python 3.
It doesn't import feedparser

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Thu Jan 21, 2016 10:26 pm

Good point!
There must be an alternative though surely.

User avatar
topguy
Posts: 5569
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Receiving emails on Pi

Fri Jan 22, 2016 12:06 am

Based on the package list of Debian Jessie its most likely called "python3-feedparser". (https://packages.debian.org/jessie/python3-feedparser)

Something you can verify by running

Code: Select all

apt-cache search feedparser

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Fri Jan 22, 2016 4:55 pm

@topguy
Thank you very much for taking the time to help me.

I have run that statement from the command window with the following results:

apt-cache search feedparser
python-feedparser - Universal Feed Parser for Python
python3-feedparser - Universal Feed Parser for Python 3
ruby-feedparser - Ruby library to parse ATOM/RSS feeds

which does seem to suggest that it is the feedparser for Python 3.

But how do I use that in my program.

I tried just changing feedparser for python3-feedparser or python3- feedparser or python3 - feedparser
but it just reports:
invalid syntax

User avatar
topguy
Posts: 5569
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Receiving emails on Pi

Fri Jan 22, 2016 6:59 pm

First you have to install it.

Code: Select all

sudo apt-get install python3-feedparser
"apt-cache search" just gives you which packages are available for installation. (including stuff already installed)

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Fri Jan 22, 2016 10:32 pm

@topguy
Great. Thank you very much. I have now got it running correctly.
(I had to amend the 'print' command line as well because that was Python 2 code)

I know need to gather more information about the possibilities with feedparser because after the Pi has received the email and acted on the contents, I now want the Pi to be able to delete it.

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Sun Jan 24, 2016 11:27 pm

I have so far, been unable to make any further progress with being able to delete emails using the Pi, once they have been received.

Is there any way of looking at the code in feedparser (whatever that is!) to see what actions can be made, or is there another way of deleting emails.

My end goal is as follows:
1) I have a program that monitors my Solar Panels and this works very well.
2)The program currently sends an email to my mobile, after every (n) units have been generated.
3) I have a variable in that program, that defines (n)
4) I want to be able to send the Pi an email to make various changes, e.g. to change the value of (n)
5) I want the Pi to be able to delete the email (and the one it has sent me)

I have achieved steps 1 to 4 so far and just need step 5 (for now!)

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Receiving emails on Pi

Mon Jan 25, 2016 11:16 pm

Why can't the receiving email client delete the emails (I use thunderbird as a client for my Pi email server and it deletes emails)?

stevech
Posts: 144
Joined: Sun Jul 15, 2012 11:53 pm

Re: Receiving emails on Pi

Tue Jan 26, 2016 6:47 am

Excessive PITA on small comptuers. I avoid python 3 and its pythonic excesses.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Receiving emails on Pi

Wed Jan 27, 2016 3:08 pm

As feedparser is feed parser, I would not be surprised if it did not cater for message deletion. Note that for sending email that tutorial does not use feedparser but Python smtplib. You might need to look at Gmail APIs - not sure if Gmail anymore supports POP or IMAP... Some other communications method might be simpler (but of course email client is readily available on most devices, so...).
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Wed Jan 27, 2016 3:40 pm

@rst
Thank you for your reply. I do use smtplib that enables the Pi to send emails and it works perfectly. It is the receiving that I need help with because feed parser seems to work by listing all unread emails and that is why I need to be able to delete. I can delete them by going into the gmail website but I need to be able to delete them using the Pi.


RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Wed Jan 27, 2016 7:24 pm

@AiGreek
I think you may be right!

I will give those a try.

Thank you very much,

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Wed Mar 09, 2016 9:12 am

Despite many hours of reading, I have still been unable to find a simple way of deleting emails from gmail using the Pi.

I am using smtplib and I can successfully receive email but I need to get the Pi to delete the email (or even to be able to mark it as read would be a step forward), otherwise the next time I run my program, it finds the same email again.

Further help would be appreciated.

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

Re: Receiving emails on Pi

Wed Mar 09, 2016 1:27 pm

Apple say... Monkey do !!

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Thu Mar 10, 2016 10:44 pm

@davef21370
Thank you for the link. However, even after reading many of the links within the link, I am still unable to work out how to achieve what I want to do.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Receiving emails on Pi

Fri Mar 11, 2016 9:50 am

RDS wrote:I am using smtplib
Maybe that's your problem.

This answer suggests that SMTP is only good for sending emails. If you used imap then the answer above would work.

A different answer suggests that there's a third party library (for Python 2) which allows you to delete messages too.

The Gmail API that was also linked earlier seems to have methods to delete messages (see here and here).

So, I think you've got a few options, but you may need to move away from smtplib,
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Receiving emails on Pi

Fri Mar 11, 2016 5:54 pm

@elParaguayo
Thank you for your response. I have actually very successfully received emails on my Pi using smtp and the only thing that is missing is how to delete them from the gmail server after they have been received, or at least enable the gmail server to acknowledge that they have been received and for them to be marked as 'read'.
I am using Python 3 and a lot of the suggestions seem to use Python 2 .

There must (surely) be a simple way of doing what I want with Python 3 using smtp.
All my code to read and write emails has been taken from http://mitchtech.net/?s=email&x=3&y=3
Where is Michael the originator of that excellent site, when I need him!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Receiving emails on Pi

Fri Mar 11, 2016 6:24 pm

Sorry, I appreciate that you can receive emails, so my last post wasn't very clear.

I'll let Wikipedia explain my point better
SMTP is a delivery protocol only. In normal use, mail is "pushed" to a destination mail server (or next-hop mail server) as it arrives. Mail is routed based on the destination server, not the individual user(s) to which it is addressed. Other protocols, such as the Post Office Protocol (POP) and the Internet Message Access Protocol (IMAP) are specifically designed for use by individual users retrieving messages and managing mail boxes.
So, I don't think you can do what you want using smtplib.

Edit: if you're receiving emails by using feedparser then that's totally separate too. You're just reading a feed.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”