Gmail Mail read with Esp32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
AKJ7
Posts: 2
Joined: Sun Jul 15, 2018 10:00 pm

Gmail Mail read with Esp32

Post by AKJ7 » Sun Jul 15, 2018 10:07 pm

I would like to receive the number of unread Mails from Gmail using an ESP32 board and micropython.
I would also like to read emails from Gmail on an ESP32 board using Micropython.

My profil: I can program well in C and in Python. I have no experience with Micropython.

Issues:
1. The Gmail APIs are not written in Micropython, rather in Python, Go, ... . Is there a way to import Python Modules, convert them and use them on the ESP32?
2. If not, is there any other way to receive the number of unread mails from Gmail and if possibly, get the unread mails themselves on an ESP32?

Thank you.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Gmail Mail read with Esp32

Post by SpotlightKid » Mon Jul 16, 2018 7:04 am

re 1) No. At least not without manual work. If a library for standard Python has no or only basic dependencies, it might work also under MicroPython with no or minimal changes, but this is rare, since many libraries from the Python standard library are not available on MicroPython in general or unavailable on embedded devices orr are lacking some capabilities.

In general, libraries need to ported from standard Python (or another language) to MicroPython manually or implemented specifically for MicroPython from scratch to cater for the limited resources available and platform-specific contraints there.

re 2) In theory, GMail resources can be accessed as REST web services with plain HTTP requests (e.g. using the urequests module in MicroPython). But in practice you must jump through the hoops of OAuth2 authentication first to access these web services. The oauth2 library for standard Python is probably too big and complex to port to MicroPython.

A possible solution might be to use the auth workflow for appliances described here: https://developers.google.com/identity/ ... ForDevices

This requires two things:

a) your device must have a capability to present to the user a URL for authentication (e.g. a display for showing a QR code)
b) the user must have a device (e.g. a smartphone) to open the authentication URL and log into his/her Google account there.

AFAIK, nobody has written code for MicroPython to handle this authentication scheme yet, so you'd have to write that yourself.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: Gmail Mail read with Esp32

Post by loboris » Mon Jul 16, 2018 1:38 pm

In my MicroPython implementation sending mail to GMail is implemented since the last year using libcurl and works without issues (curl module).

Reading mail, as well as listing IMAP directories, searching, etc. was implemented recently and will be pushed to the repository probably by the end ow the week.

Code: Select all

>>> import curl
#Send the test mail
>>> curl.sendmail('user_name','password', 'mymail@gmail.com', 'TEST', 'Mail sent from MicroPython')
True

# list the mail IDs (received today)
>>> res=curl.getmail('INBOX?SINCE 16-Jul-2018','user_name','password')
>>> print(res[2])
* SEARCH 11376 11377 11378 11379 11380

# Get the mail body
>>> res=curl.getmail('INBOX/;UID=11379/;SECTION=TEXT','user_name','password')
>>> print(res[2])
--=BODY_SEPARATOR_0779_6971_8655_4937_0948_5501
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

Mail sent from MicroPython
--=BODY_SEPARATOR_0779_6971_8655_4937_0948_5501--

>>> 

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Gmail Mail read with Esp32

Post by SpotlightKid » Mon Jul 16, 2018 2:09 pm

If I understand this correctly, the curl module uses (will be using) IMAP to access the GMail inbox. Please note that you have to enable IMAP access manually in the GMail settings for each account you want to use it with.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: Gmail Mail read with Esp32

Post by loboris » Mon Jul 16, 2018 6:35 pm

SpotlightKid wrote:
Mon Jul 16, 2018 2:09 pm
If I understand this correctly, the curl module uses (will be using) IMAP to access the GMail inbox. Please note that you have to enable IMAP access manually in the GMail settings for each account you want to use it with.
Yes, IMAP is used, POP3 will be added later. IMAP and POP3 access can be configured easily in GMail settings.
curl.sendmail and curl.getmail can be used not only with GMail, but with any mail server. GMail is the default, but mail server and port can be specified in the methods call.

AKJ7
Posts: 2
Joined: Sun Jul 15, 2018 10:00 pm

Re: Gmail Mail read with Esp32

Post by AKJ7 » Mon Jul 16, 2018 7:09 pm

Thank you guys. I will try these codes and notify you once i make some progress.

henkoegema
Posts: 4
Joined: Sat Sep 01, 2018 3:24 pm

Re: Gmail Mail read with Esp32

Post by henkoegema » Tue Oct 02, 2018 10:07 pm

loboris wrote:
Mon Jul 16, 2018 1:38 pm
In my MicroPython implementation sending mail to GMail is implemented since the last year using libcurl and works without issues (curl module).

Reading mail, as well as listing IMAP directories, searching, etc. was implemented recently and will be pushed to the repository probably by the end ow the week.

Code: Select all

>>> import curl
#Send the test mail
>>> curl.sendmail('user_name','password', 'mymail@gmail.com', 'TEST', 'Mail sent from MicroPython')
True

# list the mail IDs (received today)
>>> res=curl.getmail('INBOX?SINCE 16-Jul-2018','user_name','password')
>>> print(res[2])
* SEARCH 11376 11377 11378 11379 11380

# Get the mail body
>>> res=curl.getmail('INBOX/;UID=11379/;SECTION=TEXT','user_name','password')
>>> print(res[2])
--=BODY_SEPARATOR_0779_6971_8655_4937_0948_5501
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

Mail sent from MicroPython
--=BODY_SEPARATOR_0779_6971_8655_4937_0948_5501--

>>> 
How did you import curl ?
Can't find it anywhere. (it's not in https://github.com/micropython/micropython-lib)

Post Reply