Showing posts with label 4. Receiving and reading email. Show all posts
Showing posts with label 4. Receiving and reading email. Show all posts

Saturday, January 4, 2014

Arduino Yun: reading mails with Python: the simple way

All the previous stuff works, but was unnecessarily complicated. It can be done much simpler with Python. The following code will check all unseen messages and print the main 'body' of the mail.


#!/usr/bin/python
import sys
import imaplib
import private.pw
import email

USERNAME = private.pw.myMailUser
PASSWORD = private.pw.myMailPass
MAILTO  = private.pw.myMailTo

# first check if there is new mail
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"

n_email = int(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)["feed"]["fullcount"])
if n_email > 0:
    print "New mail!"
else:
    print "No new mail!"
    exit(0)

imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
imap_server.login(USERNAME, PASSWORD)
imap_server.select('INBOX')

result, data = imap_server.uid('search', None, "UNSEEN") # get unseen message UIDs
for my_uid in data[0].split():
   my_msg=''
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[HEADER.FIELDS (FROM)])')   # spaces are important here !!
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[])')   # use this if dont want to flag mail as seen
   result, data = imap_server.uid('fetch',my_uid, '(BODY[])')
   raw_email=data[0][1]
   email_msg = email.message_from_string(raw_email)
   maintype = email_msg.get_content_maintype()
   if maintype == 'multipart':
      for part in email_msg.get_payload():
         if part.get_content_maintype() == 'text':
            my_msg = part.get_payload()
   elif maintype == 'text':
     my_msg = email_msg.get_payload()
   print my_msg
imap_server.close()
imap_server.logout()



Note: feedparser should be installed on the Arduino Yun first. I did this as follows:
  • download it from https://code.google.com/p/feedparser/downloads/list (tar.gz) 
  • copy it to somewhere on the Yun SD card 
  • unzip and untar: 
    • gunzip feedp...
    • tar -xvf feedpa...
  • opkg update 
  • opkg install setuptools 
  • cd to the uncompressed folder 
  • python setup.py install
Note2: you need to have openssl stuff installed for this:
  • opkg update
  • opkg install python-openssl

Sunday, November 3, 2013

Getting the arduino Yun ready to receive and read google mails - part5

Now the procmail part. For now I just want all mails sent to me@yafasomething.com to come in a certain file. Again I want all my files on the SD card.

on the SD card create folders (in arduino):

mail.
mail/mailboxes/
mail/mailboxes/yafa/
mail/mailboxes/default

create the file mail/procmailrc containing:

SHELL=/bin/sh
MAILDIR=/mnt/sda1/arduino/mail/mailboxes
DEFAULT=$MAILDIR/default
LOGFILE=$MAILDIR/procmail.log


:0:/mnt/sda1/arduino/mail/procmail.lock
* ^TOme@yafasomething.com
| cat - > yafa/yafa_msg.txt

you can test procmail by e.g.
procmail ./procmailrc < testmail2.txt
where testmail2.txt contains e.g.
From: me@example.com
To: me@yafasomething.com
X-info: I'm just testing

BODY OF MESSAGE SEPARATED BY EMPTY LINE
txt txt txt txt txt txt txt txt txt txt
Now in our fetchmailrc change the procmail line to:

mda "/usr/bin/procmail /mnt/sda1/arduino/mail/procmailrc"

Now if a mail is sent to me@yafasomething.com and we run fetchmail -vk the message should arrive in the specified yafa_msg.txt file. To avoid procmail errors I also created the folder /var/spool/mail, but it is not the goal that something arrives in here.

 This topic is continued here.

Getting the arduino Yun ready to receive and read google mails - part4

Time to work on the ssl certificates

openssl s_client -connect pop.gmail.com:995 -showcerts

returns

CONNECTED(00000003)
depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=pop.gmail.com
   i:/C=US/O=Google Inc/CN=Google Internet Authority G2
-----BEGIN CERTIFICATE-----
MIIEdDCCA1ygAwIBAgIICnqidmpojoAwDQYJKoZIhvcNAQEFBQAwSTELMAkGA1UE
BhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2dsZSBJbnRl
cm5ldCBBdXRob3JpdHkgRzIwHhcNMTMwOTEwMDc1NzM5WhcNMTQwOTEwMDc1NzM5
WjBnMQswCQYDVQQGEwJVUzETMBEGA1UECAwK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Q4EFgQUSt0GFhu89mi1dvWBtrtiGrpagS8wEgYD
VR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMwMTAvoC2g
K4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9ndGdsb2JhbC5jcmwwPQYI
KwYBBQUHAQEEMTAvMC0GCCsGAQUFBzABhiFodHRwOi8vZ3RnbG9iYWwtb2NzcC5n
ZW90cnVzdC5jb20wFwYDVR0gBBAwDjAMBgorBgEEAdZ5AgUBMA0GCSqGSIb3DQEB
BQUAA4IBAQA21waAESetKhSbOHezI6B1WLuxfoNCunLaHtiONgaX4PCVOzf9G0JY
/iLIa704XtE7JW4S615ndkZAkNoUyHgN7ZVm2o6Gb4ChulYylYbc3GrKBIxbf/a/
zG+FA1jDaFETzf3I93k9mTXwVqO94FntT0QJo544evZG0R0SnU++0ED8Vf4GXjza
HFa9llF7b1cq26KqltyMdMKVvvBulRP/F/A8rLIQjcxz++iPAsbw+zOzlTvjwsto
WHPbqCRiOwY1nQ2pM714A5AuTHhdUDqB1O6gyHA43LL5Z/qHQF1hwFGPa4NrzQU6
yuGnBXj8ytqU0CwIPX4WecigUCAkVDNx
-----END CERTIFICATE-----
 2 s:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
   i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
-----BEGIN CERTIFICATE-----
MIIDfTCCAuagAwIBAgIDErvmMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT
MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0
aWZpY2F0ZSBBdXRob3JpdHkwHhcNMDIwNTIxMDQwMDAwWhcNMTgwODIxMDQwMDAw
WjBCMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UE
AxMSR2VvVHJ1c3QgR2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEA2swYYzD99BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9m
OSm9BXiLnTjoBbdqfnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIu
T8rxh0PBFpVXLVDviS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6c
JmTM386DGXHKTubU1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmR
Cw7+OC7RHQWa9k0+bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5asz
PeE4uwc2hGKceeoWMPRfwCvocWvk+QIDAQABo4HwMIHtMB8GA1UdIwQYMBaAFEjm
aPkr0rKV10fYIyAQTzOYkJ/UMB0GA1UdDgQWBBTAephojYn7qwVkDBF9qn1luMrM
TjAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBBjA6BgNVHR8EMzAxMC+g
LaArhilodHRwOi8vY3JsLmdlb3RydXN0LmNvbS9jcmxzL3NlY3VyZWNhLmNybDBO
BgNVHSAERzBFMEMGBFUdIAAwOzA5BggrBgEFBQcCARYtaHR0cHM6Ly93d3cuZ2Vv
dHJ1c3QuY29tL3Jlc291cmNlcy9yZXBvc2l0b3J5MA0GCSqGSIb3DQEBBQUAA4GB
AHbhEm5OSxYShjAGsoEIz/AIx8dxfmbuwu3UOx//8PDITtZDOLC5MH0Y0FWDomrL
NhGc6Ehmo21/uBPUR/6LWlxz/K7ZGzIZOKuXNBSqltLroxwUCEm2u+WR74M26x1W
b8ravHNjkOR/ez4iyz0H7V84dJzjA1BOoa+Y7mHyhD8S
-----END CERTIFICATE-----
---
.
.
.
    Start Time: 1383482982
    Timeout   : 300 (sec)
    Verify return code: 20 (unable to get local issuer certificate)
---
+OK Gpop ready for requests from ....

Looks like we need 3 certificates...let's make a directory in our fetchmail folder:

mkdir certs

Then get the root certificates from http://www.geotrust.com/resources/root-certificates/index.html
We need:

  • Root 1 - Equifax Secure Certificate Authority
  • Root 2 - GeoTrust Global CA

You can download them to your certs folder, or just copy paste the content (e.g. from a browser) in the files. So in the certs folder we will have a file called Equifax_Secure_Certificate_Authority.pem with as content:

-----BEGIN CERTIFICATE-----
MIIDIDCCAomgAwIBAgIENd70zzANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJV
UzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2Vy
dGlmaWNhdGUgQXV0aG9yaXR5MB4XDTk4MDgyMjE2NDE1MVoXDTE4MDgyMjE2NDE1
MVowTjELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0VxdWlmYXgxLTArBgNVBAsTJEVx
dWlmYXggU2VjdXJlIENlcnRpZmljYXRlIEF1dGhvcml0eTCBnzANBgkqhkiG9w0B
AQEFAAOBjQAwgYkCgYEAwV2xWGcIYu6gmi0fCG2RFGiYCh7+2gRvE4RiIcPRfM6f
BeC4AfBONOziipUEZKzxa1NfBbPLZ4C/QgKO/t0BCezhABRP/PvwDN1Dulsr4R+A
cJkVV5MW8Q+XarfCaCMczE1ZMKxRHjuvK9buY0V7xdlfUNLjUA86iOe/FP3gx7kC
AwEAAaOCAQkwggEFMHAGA1UdHwRpMGcwZaBjoGGkXzBdMQswCQYDVQQGEwJVUzEQ
MA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2VydGlm
aWNhdGUgQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMBoGA1UdEAQTMBGBDzIwMTgw
ODIyMTY0MTUxWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gj
IBBPM5iQn9QwHQYDVR0OBBYEFEjmaPkr0rKV10fYIyAQTzOYkJ/UMAwGA1UdEwQF
MAMBAf8wGgYJKoZIhvZ9B0EABA0wCxsFVjMuMGMDAgbAMA0GCSqGSIb3DQEBBQUA
A4GBAFjOKer89961zgK5F7WF0bnj4JXMJTENAKaSbn+2kmOeUJXRmm/kEd5jhW6Y
7qj/WsjTVbJmcVfewCHrPSqnI0kBBIZCe/zuf6IWUrVnZ9NA2zsmWLIodz2uFHdh
1voqZiegDfqnc1zqcPGUIWVEX/r87yloqaKHee9570+sB3c4
-----END CERTIFICATE-----

Do the same for the GeoTrust_Global_CA.pem file.
Finally create a pop.gmail.com.pem with as content:

-----BEGIN CERTIFICATE-----
MIIDIDCCAomgAwIBAgIENd70zzANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJV
UzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2Vy
dGlmaWNhdGUgQXV0aG9yaXR5MB4XDTk4MDgyMjE2NDE1MVoXDTE4MDgyMjE2NDE1
MVowTjELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0VxdWlmYXgxLTArBgNVBAsTJEVx
dWlmYXggU2VjdXJlIENlcnRpZmljYXRlIEF1dGhvcml0eTCBnzANBgkqhkiG9w0B
AQEFAAOBjQAwgYkCgYEAwV2xWGcIYu6gmi0fCG2RFGiYCh7+2gRvE4RiIcPRfM6f
BeC4AfBONOziipUEZKzxa1NfBbPLZ4C/QgKO/t0BCezhABRP/PvwDN1Dulsr4R+A
cJkVV5MW8Q+XarfCaCMczE1ZMKxRHjuvK9buY0V7xdlfUNLjUA86iOe/FP3gx7kC
AwEAAaOCAQkwggEFMHAGA1UdHwRpMGcwZaBjoGGkXzBdMQswCQYDVQQGEwJVUzEQ
MA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2VydGlm
aWNhdGUgQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMBoGA1UdEAQTMBGBDzIwMTgw
ODIyMTY0MTUxWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gj
IBBPM5iQn9QwHQYDVR0OBBYEFEjmaPkr0rKV10fYIyAQTzOYkJ/UMAwGA1UdEwQF
MAMBAf8wGgYJKoZIhvZ9B0EABA0wCxsFVjMuMGMDAgbAMA0GCSqGSIb3DQEBBQUA
A4GBAFjOKer89961zgK5F7WF0bnj4JXMJTENAKaSbn+2kmOeUJXRmm/kEd5jhW6Y
7qj/WsjTVbJmcVfewCHrPSqnI0kBBIZCe/zuf6IWUrVnZ9NA2zsmWLIodz2uFHdh
1voqZiegDfqnc1zqcPGUIWVEX/r87yloqaKHee9570+sB3c4
-----END CERTIFICATE-----

Then it is time to make the hashes. I have seen typically there is a c_rehash command, but I don't find it for Linino/OpenWrt. we can do this manually:

openssl x509 -hash -noout -in pop.gmail.com.pem

This returns a number (09cc127e). Make a link using this number (adding .0):


ln -s pop.gmail.com.pem 09cc127e.0 
Do the same for the other 2. Now add to the fetchmailrc file the location of our certs:
sslcertpath /mnt/sda1/arduino/fetchmail/certs/

If we now run fetchmail -vk we get something like:
fetchmail: WARNING: Running as root is discouraged.
fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Sun, 03 Nov 2013 16:52:40 +0100 (CET): poll started
Trying to connect to 173.194.70.108/995...connected.
fetchmail: Server certificate:
fetchmail: Issuer Organization: Google Inc
fetchmail: Issuer CommonName: Google Internet Authority G2
fetchmail: Subject CommonName: pop.gmail.com
fetchmail: Subject Alternative Name: pop.gmail.com
fetchmail: pop.gmail.com key fingerprint: 91:63:CF:6F:DF:03:3C:B9:3E:19:3B:1C:B6:EB:3C:25
fetchmail: POP3< +OK Gpop ready for requests from 91.179.156.171 n2pf31788823eef.22
fetchmail: POP3> CAPA
fetchmail: POP3< +OK Capability list follows
fetchmail: POP3< USER
fetchmail: POP3< RESP-CODES
fetchmail: POP3< EXPIRE 0
fetchmail: POP3< LOGIN-DELAY 300
fetchmail: POP3< TOP
fetchmail: POP3< UIDL
fetchmail: POP3< X-GOOGLE-VERHOEVEN
fetchmail: POP3< X-GOOGLE-RICO
fetchmail: POP3< .
fetchmail: POP3> USER xxxxxx
fetchmail: POP3< +OK send PASS
fetchmail: POP3> PASS *
fetchmail: POP3< +OK Welcome.
fetchmail: POP3> STAT
fetchmail: POP3< +OK 0 0
fetchmail: No mail for xxxxxxxx at pop.gmail.com
fetchmail: POP3> QUIT
fetchmail: POP3< +OK Farewell.
fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Sun, 03 Nov 2013 16:52:41 +0100 (CET): poll completed
fetchmail: normal termination, status 1


In this case there was no mail (status 1).

Saturday, November 2, 2013

Getting the arduino Yun ready to receive and read google mails - part3

I changed to users homedir to reside on the micro-SD card. In /etc/passwd, just add /mnt/sda1/arduino/ in front of the existing path for the self-created users. Obviously also move the existing user dirs to that location.

A bit later I discovered that there isn't good support for running sketches as non-root on the Yun yet.
Furthermore to be able to have user owned directories on the SD card, the SD card should be formatted in Linux format. For now I am giving up on running Yafa as a normal user. I'll do things as root for now.

However I want to avoid fetchmail writing in the /root folder, so I want all my fetchmail stuff to reside on the SD card. Because the .fetchmailrc needs specific permissions, I need to format the SD card in linux format anyway:

opkg update
opkg install e2fsprogs
opkg install mount-utils
umount /dev/sda1
mkfs.ext4 /dev/sda1

I didn't succeed in mounting it again. A reboot of the Yun took care of that.

cd /dev/sda1
mkdir arduino
cd arduino
mkdir fetchmail
cd fetchmail
touch fetchmail.log
touch fetchmailrc
chmod 600 fetchmailrc

In fetchtmailrc I have put:

set idfile /mnt/sda1/arduino/fetchmail/fetchids
#set logfile /mnt/sda1/arduino/fetchmail/fetchmail.log
set pidfile /mnt/sda1/arduino/fetchmail/fetchmail.pid
poll pop.gmail.com
proto pop3
user "me@yafasomething.com"
password "mypassword"    
mda "/usr/bin/procmail -d %T"
ssl
sslcertck

I could run fetchmail with the -f option, but it is easier to set an environment variable, which will also make sure the SD card is used (could probably remove the first lines from fetchmailrc)
add to /etc/profile the following line (and execute it or log in again):



export FETCHMAILHOME="/mnt/sda1/arduino/fetchmail/"

Note that when running fetchmail with the 2nd line not commented out, the messages do not come on the screen anymore, but come in the log-file. For now I want them to come to standard out.

Friday, November 1, 2013

Getting the arduino Yun ready to receive and read google mails - part2

I noticed that the first 'return message' that I got from fetchmail is:

gethostbyname failed for Arduino
Name or service not knownCannot find my own host in hosts database to qualify it!
Trying to continue with unqualified hostname.
DO NOT report broken Received: headers, HELO/EHLO lines or similar problems!
DO repair your /etc/hosts, DNS, NIS or LDAP instead.

Arduino is the hostname of my Arduino Yun (it is the name you can choose on the Arduino's web-interface). For now I got this message to disappear by changing the content of /etc/hosts from

127.0.0.1 localhost

to

127.0.0.1 localhost
127.0.1.1 Arduino
fetchmail also suggested to add sslcertck to the setup (I think this is more strict). So I did add this to the .fetchmailrc file.

Wednesday, October 30, 2013

Getting the arduino Yun ready to receive and read google mails - part1

I found a lot of examples of how to send mail with the Yun, or how to fetch the google atom feed, but I didn't find any example of how to really receive the full emails with the Yun.

I have a google mail account, so I am focussing only on accessing the google mail (gmail).
Below I will assume my google mail user has the following 'properties':
username:  me@yafasomething.com
password:  mypassword

Disclaimer: I have no experience with setting up all this email stuff, so there might be better ways of doing this. What I did is based on a lot of googling.

I asked on the arduino forum for some pointers. I got a suggestion to look into the google python API's, but I got the impression that this is not for free at some point. So I abandoned that route.
I started reading about linux mail 'clients'. I learned a lot of things. I now know more or less what an MTA, MDA, MUA, etc... is :-)

I had no idea if I should use POP3 or IMAP. I went for POP3 for now. So first thing to do is to enable POP3 in your google account (I assume you know how to do this).

Next steps are on the arduino Yun:

login as root and run the following commands:

opkg update
opkg install openssl-util
opkg install fetchmail
opkg install procmail
opkg install shadow-useradd

This installs the packages that I think I will need.

I wasn't sure if I should eventually run my app as root, or if I can do it as another user.
I will give it a try as non-root. I decided to create a user called MrYafa:

mkdir /home 
useradd -m -c "Mr Yafa" MrYafa
passwd MrYafa

Maybe the first command isn't needed. In the last command you obviously need to choose a password.

Then edit the /etc/passwd file   (e.g vi /etc/passwd - I will assume you know how to work with vi)
You should see the newly created user. At the end of that line add /bin/ash

Now exit as root and try to login as MrYafa:

ssh MrYafa@arduino.local

(on windows you might need to enter the IP address as arduino.local might not work)

You should be able to login an arrive in your home directory.

pwd

should return

/home/MrYafa

now create a file .fetchmailrc in that directory (vi .fetchmailrc) containing:

poll pop.gmail.com
with proto pop3
user "me@yafasomething.com"
there with password "mypassword"     
is "MrYafa" here 
mda "/usr/bin/procmail -d %T"
options
ssl

Now, if you give the command

fetchmail -vk

you should see the communication with your google mail on the screen, ending with (in my case):

fetchmail: POP3> QUIT
fetchmail: POP3< +OK Farewell.
fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Wed, 30 Oct 2013 18:51:33 +0100 (CET): poll completed
fetchmail: normal termination, status 1

One warning is that the connection is insecure. I know that you should use certificates, but haven't figured out yet how to do so. On mu Ubuntu PC I have ca-certificates installed, and there it seems OK. But the ca-certificates package does not seem to be available for Linino. So I need to find another way of making it more secure.

That's it for now. Not so difficult when you know what to do, but it took a while to find that out.