Sending mails with esp8266 and micropython
Sending mails with esp8266 and micropython
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.
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.
Re: Sending mails with esp8266 and micropython
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
Jon
Re: Sending mails with esp8266 and micropython
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
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
Re: Sending mails with esp8266 and micropython
>> 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
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
Re: Sending mails with esp8266 and micropython
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.
Re: Sending mails with esp8266 and micropython
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!
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!
Re: Sending mails with esp8266 and micropython
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.
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.
Re: Sending mails with esp8266 and micropython
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?
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?
Re: Sending mails with esp8266 and micropython
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).Tetje wrote:Ok,
I'am a bit confused. How then does your mail client the send messsages to the mail provider?
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.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Sending mails with esp8266 and micropython
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.
* 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.