uMail - A lightweight SMTP client for MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

uMail - A lightweight SMTP client for MicroPython

Post by shawwwn » Tue Sep 18, 2018 3:03 pm

I was trying to port smtplib but then found out it was too bloated, so I ended up writing my own.

https://github.com/shawwwn/uMail

The API was designed in a way so that it can accommodate sending long emails in a memory constrained system.

There is no pre-defined options for email headers(from/to/subject) so you have to draft up your own ;)

Code: Select all

import umail
smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True) # Gmail's SSL port
smtp.login('bob@gmail.com', 'bobspassword')
smtp.to('alice@gmail.com')
smtp.write("From: Bob <bob@gmail.com>\n")
smtp.write("To: Alice <alice@gmail.com>\n")
smtp.write("Subject: Poem\n")
smtp.write("Roses are red.\n")
smtp.write("Violets are blue.\n")
smtp.write("...\n")
smtp.send()
smtp.quit()
The SMTP protocol is simple but be can be slow due to the back-and-forth ehlo. So far I am thinking of writing a non-blocking version with uasyncio.

UPDATE 1: Changed argument name 'email' to 'username' as brought up by @SpotlightKid
Last edited by shawwwn on Sat Sep 22, 2018 8:06 am, edited 1 time in total.

User avatar
pythoncoder
Posts: 3707
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uMail - A lightweight SMTP client for MicroPython

Post by pythoncoder » Tue Sep 18, 2018 5:04 pm

That could be very useful, especially an asynchronous version.
Peter Hinch

bitninja
Posts: 116
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: uMail - A lightweight SMTP client for MicroPython

Post by bitninja » Wed Sep 19, 2018 8:23 pm

Hi!

I'm getting an

Code: Select all

   AssertionError: auth error 535
on line 73 of umail.py. My example is almost the same as yours.

Do I have to enable something in my gmail account to allow smtp access?

Thanks!

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

Re: uMail - A lightweight SMTP client for MicroPython

Post by loboris » Thu Sep 20, 2018 7:16 am


bitninja
Posts: 116
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: uMail - A lightweight SMTP client for MicroPython

Post by bitninja » Thu Sep 20, 2018 8:46 pm

loboris wrote:
Thu Sep 20, 2018 7:16 am
Allow less secure apps
Ahh! Thank you. That did the trick!

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

Re: uMail - A lightweight SMTP client for MicroPython

Post by SpotlightKid » Fri Sep 21, 2018 12:31 am

When Iooked at the source code, I noticed your module employs STARTTLS, if the server supports it. I think it would be good to mention that in the readme, for those where the server does not support a direct SSLL connection and who may be wondering if/how they can have a secure connection.

In your examples, you should end all header and body lines with '\r\n', not just newline, as the RFC prescribes.

shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

Re: uMail - A lightweight SMTP client for MicroPython

Post by shawwwn » Fri Sep 21, 2018 9:08 am

SpotlightKid wrote:
Fri Sep 21, 2018 12:31 am
When Iooked at the source code, I noticed your module employs STARTTLS, if the server supports it. I think it would be good to mention that in the readme, for those where the server does not support a direct SSLL connection and who may be wondering if/how they can have a secure connection.
SMTP server typically uses different ports for secure/unsecure connection(like HTTP). If client connect via an unsecure connection 'ssl=False', then it's up to the server to request whether or not the client should use encryption. If the server response(after the first ELHO) contains STARTTLS, and the client must switch to TLS protocol. If client connect via TLS 'ssl=True', then the STARTTLS won't even appear.
Take Gmail's SMTP for example, its unsecured port is 587. If you want to connect to this port, you must set 'ssl=False', but then Gmail will still request for TLS, otherwise it won't proceed -- that's when our client wraps the connection with TLS. It's all done in a transparent manner, so nothing to worry about.
SpotlightKid wrote:
Fri Sep 21, 2018 12:31 am
In your examples, you should end all header and body lines with '\r\n', not just newline, as the RFC prescribes.
I only tested it on Gmail, it seems to accept any kind of newline character. But yea, you are probably right. I will attach a notice in readme. Thanks for bring this up! :lol:

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

Re: uMail - A lightweight SMTP client for MicroPython

Post by SpotlightKid » Fri Sep 21, 2018 9:22 am

shawwwn wrote:
Fri Sep 21, 2018 9:08 am

SMTP server typically uses different ports for secure/unsecure connection(like HTTP). If client connect via an unsecure connection 'ssl=False', then it's up to the server to request whether or not the client should use encryption. If the server response(after the first ELHO) contains STARTTLS, and the client must switch to TLS protocol.
I see. I thought that the server response would just indicate the support for STARTTLS.

Then you should probably add a boolean keyword arg starttls, to enforce use of STARTTLS with servers, which also allow unsecured connections.

Also, I would suggest, renaming the email argument for the init and login methods to user or username, which IMHO is more semantically correct, since the SMTP server account name doesn't need to be an email address.

shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

Re: uMail - A lightweight SMTP client for MicroPython

Post by shawwwn » Fri Sep 21, 2018 9:35 am

SpotlightKid wrote:
Fri Sep 21, 2018 9:22 am
Then you should probably add a boolean keyword arg starttls, to enforce use of STARTTLS with servers, which also allow unsecured connections.
'ssl=True|False' already does that -- if set True, then there is no need to STARTTLS; if set false, then client will switch to TLS upon server's request.
SpotlightKid wrote:
Fri Sep 21, 2018 9:22 am
Also, I would suggest, renaming the email argument for the init and login methods to user or username, which IMHO is more semantically correct, since the SMTP server account name doesn't need to be an email address.
For sure.

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

Re: uMail - A lightweight SMTP client for MicroPython

Post by SpotlightKid » Fri Sep 21, 2018 9:42 am

There are SMTP servers, which do not accept SSL connections on port 465, but support connections on port 587 either with using STARTTLS or without. For those the starttls arg would be useful. I.e. setting ssl=False and starttls=True, would enforce use of STARTTLS with those.

Post Reply