Sending mails with esp8266 and micropython

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Tetje
Posts: 5
Joined: Sat Aug 27, 2016 3:26 pm

Sending mails with esp8266 and micropython

Post by Tetje » Sat Aug 27, 2016 5:01 pm

Hi,

I am new to micropython. Until now I mostly used the Arduino and a bit PIC24. But I wanted to feel what it's like to use a more high level language than C/C++ on an microcontroller. So I searched for my nearly unused esp in the deepest deep of my tressure chest to flash the micropython onto it.
The first project that came into my mind, were to send an email, when something special happens. The problem is, that their is no smtplib on the board, but he there are sockets and a tried to work a bit with them, but we did can far. Perhaps, because of the problem, that I don't have any email provider overring SMTP with port 25 anymore (which I think is very good..., but dosn't change it to the easilier side).
Does anyone know how to write a script that enables the use of STARTTLS or SSL/TLS, or has already done so? It would be super cool to get it done.

A question at the end. Is it normal that I only can import the ussl module with "import ussl" and not with "import ssl". I tried it with utime and I was able to import it with "import time"... Just seamed a bit strange to me...

Have a nice weekend.

jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

Re: Sending mails with esp8266 and micropython

Post by jms » Sun Aug 28, 2016 7:05 am

In practice you'll find this really difficult to get right and not break at some time in the future when somebody flicks a switch, perhaps disabling a cipher, on a relay. Use the ESP to do I/O, talk the simplest protocol and nothing else.

Jon

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Sending mails with esp8266 and micropython

Post by markxr » Sun Aug 28, 2016 11:11 am

You definitely want to host your own server on the internet, and have the esp8266 talk to it using some other protocol (not smtp).

Many customer ISPs don't allow smtp to leave their network directly to arbitrary destinations - this is just to reduce the amount of spam leaving their network (it turns out that nearly all outbound smtp traffic from a home user's connection will be spam from botnets). They sometimes don't do it for commercial customers either unless specifically asked.

So have a "middle tier" server - talking http is the most popular (but be careful not to open a spam gateway) - and have the esp talk to that. https might be a good idea, if you can get it to work with the limited tls library in the esp8266. HTTP is also the protocol which is almost never blocked (assuming that you're not in a captive portal)

Using your own internet-hosted relay, also allows you to queue and retry the email if it fails to be delivered initially, the esp8266 doesn't need to handle smtp retries etc.

Mark

Christian
Posts: 4
Joined: Thu Jul 28, 2016 6:32 am

Re: Sending mails with esp8266 and micropython

Post by Christian » Sun Aug 28, 2016 12:24 pm

>> The first project that came into my mind, were to send an email, when something special happens. <<

Close match to where I started some weeks ago. After reading the forum I ended up in using a socket connection that uses APIs from ThingSpeak (for visualization) and Prowl (for push notification). Works like a charm.

Kudos to Damien and the whole team for creating MicroPython.

Christian

flynnguy
Posts: 4
Joined: Mon Aug 29, 2016 7:03 pm

Re: Sending mails with esp8266 and micropython

Post by flynnguy » Wed Aug 31, 2016 11:57 am

To followup with Christian's post, I also found using a push service works really well. I'm on android though so prowl won't work for me so I use Pushbullet. Another service i've used is pushover and I think ifttt also has a http endpoint with their Maker channel though I've never used it. These all have http endpoints so it's much easier to use than trying to compose an email.

Tetje
Posts: 5
Joined: Sat Aug 27, 2016 3:26 pm

Re: Sending mails with esp8266 and micropython

Post by Tetje » Fri Sep 02, 2016 9:19 am

Thanks, for that many answers I read and tried a bit and what came up until now is (using posteo.de for email and trying STARTLS):

import network
sta_if = networrk.WLAN(network.STA_IF)
import socket
addr_info = socket.gettaddrinfo("posteo.de", 587)
addr = addr_info[0][-1]
s = socket.socket()
s.connect(addr)
recv = []
recv.append(s.recv(1024))
s.send("HELO posteo.de \r\n")
recv.append(s.recv(1024))
s.send("STARTTLS \r\n")
recv.append(s.recv(1024))

until that point it works... I receive:


[b'220 submission01.posteo.de ESMTP Postfix\r\n', b'250 submission01.posteo.de\r\n', b'220 2.0.0 Ready to start TLS\r\n']

then I tried to:
import ussl
sSSL = ussl.wrap_socket(s)

and I don't now further is there a send function for ussl?

Well, I wanted use as little hardware as possible I don't own myself. Addiontly the hardware should deepsleep as long as possible, perhaps change to sleep to get some values from the pins and send a message or go to deep sleep agaim depending on the read values. As alternative I thought I could use a raspi with REST and waits for a message from the esp and than send a mail. But that would be far more current consumpt for the same idea... more hardware and more code... I have (yet) no idea how to write. But, well that's what makes a maker a maker. Isn't it?

a) Do you know I could proceed with the mail, or...
b) What you think about the alternative... additional downside of push notifications... don't know whether I can receive one with sailfish.

Thanks, for that many posts!

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Sending mails with esp8266 and micropython

Post by markxr » Fri Sep 02, 2016 3:20 pm

It is really impressive that you can send TLS mail from a ESP8266, but I'm afraid I don't think it's a useful way to work.

You're connecting to tcp port 587 on some mail server - but this connection is likely to be blocked by many people's internet providers (or home routers) - so it won't work for those users even if you can make it speak the correct protocol.

If you plan on deploying this device widely (or even in only a few locations) you will find that this approach doesn't work everywhere.

Tetje
Posts: 5
Joined: Sat Aug 27, 2016 3:26 pm

Re: Sending mails with esp8266 and micropython

Post by Tetje » Sat Sep 03, 2016 11:05 am

Ok,

I'am a bit confused. How then does your mail client the send messsages to the mail provider? What would you advice to then? Perhaps an other protocoll that's ports are not blocked... and can send messages directly to your local PC, mobile phone, or tablet and than go into deep sleep?

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Sending mails with esp8266 and micropython

Post by markxr » Sat Sep 03, 2016 12:24 pm

Tetje wrote:Ok,

I'am a bit confused. How then does your mail client the send messsages to the mail provider?
Like I said before - send a HTTP (HTTPS ideally) request to your own server on the public internet. Https won't be blocked and you can control the rate and contents of the message on your server (to prevent spam).

Your server can host a very simple script e.g. PHP, to send the mail, and have its own MTA (Message transfer agent) which can queue and send the messages, so the ESP can go back to sleep and know that the message should arrive, not waiting for its delivery.

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

Re: Sending mails with esp8266 and micropython

Post by SpotlightKid » Sat Sep 03, 2016 1:11 pm

Or use MQTT:

* Run mosquitto (or another MQTT message broker) on your server
* Your ESP publishes messages via the umqtt library to the server
* On your server (or even on another client), run something like mqttwarn, to subscribe to the MQTT topic your ESP publishes to and send an email, when a message arrives.

Post Reply