looking for a user friendly smtplib alternative for emailing

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
kysodev
Posts: 6
Joined: Sat Apr 09, 2022 10:44 am

looking for a user friendly smtplib alternative for emailing

Post by kysodev » Sat Apr 09, 2022 1:24 pm

Hi, would like to start off by saying my knowledge of programming is with iOS programming and Swift so what not a great deal of experience with low level stuff/git/pip/terminal etc.

I've been learning a bit of Python after tinkering around with a little ESP8266 (NodeMCU) and a TFT display with C code. Finding that I would have more use from Python in general over C lead me here. I have since wired up an ESP8266 to a SSD1306 display and have installed MicroPython (v1.18) firmware. I've successfully managed to log onto my Wi-Fi with the ESP device and have sussed the display. I now want to send an email from the ESP8266 using smtp2go which is something I was quite easily able to do in C.

I've been directly to the site for smtp2go and successfully managed to send an email using the following code within Visual studio. Come to using this code on the ESP board I hit the error:

Code: Select all

ImportError: no module named 'smtplib'

Code: Select all

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

username = 'USERNAME'
password = 'PASSWORD'
msg = MIMEMultipart('mixed')

sender = 'sender@example.com'
recipient = 'recipient@example.com'

msg['Subject'] = 'Your Subject'
msg['From'] = sender
msg['To'] = recipient

text_message = MIMEText('It is a text message.', 'plain')
html_message = MIMEText('It is a html message.', 'html')
msg.attach(text_message)
msg.attach(html_message)
mail.smtp2go.com', 2525) # 8025, 587 and 25 can also be used.
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username, password)
mailServer.sendmail(sender, recipient, msg.as_string())
mailServer.close()

After some reading I've found that the whilst the `smtplib` library is included in python, it appears it's no longer included in MicroPython. I have come across an alternative that seems to work for everyone else but for the life of me, I'm either doing something stupidly obvious or wrong; I cannot get it to work.

uMail
It appears that this is a common module that others are using for smtp but I appear to be unable to install this package directly through Thonny. I have then tried to do a pip install with errors that it could not find it, I then try the following different bits of code in terminal to install via git:

Code: Select all

pip install http://github.com/shawwwn/umail
pip install http://github.com/shawwwn/umail.git
pip install git+http://github.com.shawwwn/umail
pip install git+http://github.com.shawwwn/umail.git
I've also tried a few other variations, more than I can remember write now but I keep hitting the same wall. It seems to manage to find the file but keeps returning the error:

Code: Select all

Error: cannot unpack file; cannot detect archive format
After hitting this wall I thought surely I can simply just take the contents of `umail.py` and copy them across to my ESP8266 directly in it's own file. Having done this it seems to import umail.py okay but I get hit again at:

Code: Select all

Traceback (most recent call last):
File "<stdin>", line 25, in <module>
File "umail.py", line 30, in __init__
OSError: -2
with using this code:

Code: Select all

24	import umail
25	smtp = umail.SMTP(host, port, username, password)
26	smtp.to(recipient)
27	smtp.send(text)
28	smtp.quit()


I have used interpolation here for ease of writing but I'm guessing theres no issues importing the `umail.py` file else I would first hit an error at line 24, not 25? I've googled for `OSError: -2` and I'm now thinking that this is either that the disk is full or the file is not found. I can't make heads nor tails of the documentation for how to capture an error that I don't know the name of and when implementing a Try Except i get hit with syntax errors.

Is anyone able to help me understand where I'm going wrong with the initial `umail` implementation or point me to a package that is easy to use/implement and more widely used/documented as a replacement for `smtplib`.

Many thanks.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: looking for a user friendly smtplib alternative for emailing

Post by davef » Sat Apr 09, 2022 7:49 pm

I believe you only have uMail. I have been using it for the last year or more.

Yes, just copy paste the code into a file called umail.py and place it in flash on your device. There is a long umail thread, does that error get mentioned? I seem to recall seeing that one during initial implementation.

I have a my_umail.py which has a lot of code in it to try to trap errors, but this sounds like you are stuck in the "opening-gate".

kysodev
Posts: 6
Joined: Sat Apr 09, 2022 10:44 am

Re: looking for a user friendly smtplib alternative for emailing

Post by kysodev » Sun Apr 10, 2022 7:57 am

davef wrote:
Sat Apr 09, 2022 7:49 pm
I believe you only have uMail. I have been using it for the last year or more.

Yes, just copy paste the code into a file called umail.py and place it in flash on your device. There is a long umail thread, does that error get mentioned? I seem to recall seeing that one during initial implementation.

I have a my_umail.py which has a lot of code in it to try to trap errors, but this sounds like you are stuck in the "opening-gate".
Hi, can't find the specific thread that you're mentioning but I have had a read of a few of them just now. Can't seem to specifically locate OSError: -2. I was hoping that copying the umail.py file directly into the directory on my board would work as that works with how I've organized the rest of my code but something is obviously not right. I've also tried renaming it to "CustomUmail.py" to no avail.

Interestingly I've seen people getting similar errors when wrapping in a do try catch block, or "try", "except" block. I've tried to figure out how to get the readable description of the error "-2" but can't quite get it and reading the official documentation didn't help too much in terms of getting something readable.

Is there anything else I could try? Don't want to give up on this if uMail is more common/easy to set up. Just can't understand why it's not working.

I only have 5 files on my ESP8266:
* boot.py
* main.py
* ConnectToWiFi.py
* CustomDisplay.py
* CustomUmail.py

The custom ones are easily <100 lines of code each and my ESP board has 4mb flash so can't understand the storage issue if it is one?

This is so frustrating that I keep hitting walls at the first hurdle with uMail. I've had a look through the whole github and can't see that I've done anything other than the set up described on the readme. Got a few other things to do today but I will have a look later and try and make heads or tails of the umail.py file to see if I can throw in some error handling but will need to go and learn more python for this i think.

kysodev
Posts: 6
Joined: Sat Apr 09, 2022 10:44 am

Re: looking for a user friendly smtplib alternative for emailing

Post by kysodev » Sun Apr 10, 2022 8:04 am

Just want to clarify that the only thing required for uMail to work is that the umail.py file is on the board, either in it's own file or directly in the code and that I use the example code from the github page, replacing the log in details for my own and host, port etc at a bare minimum for this to work?

Something is clearly going wrong but I am tempted to try and reflash my board and try doing this. I've also made a successful internet connection so don't think that's the issue.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: looking for a user friendly smtplib alternative for emailing

Post by davef » Sun Apr 10, 2022 8:28 am

Yes, I only have a umail.py file to do the mailing.

4MB? Didn't think ESP8266 were that big. Which Micropython image are you using? Have got any ESP32 around?

Have you got print statements in your WiFiconnect to show:

Code: Select all

print(' network config:', sta_if.ifconfig())

kysodev
Posts: 6
Joined: Sat Apr 09, 2022 10:44 am

Re: looking for a user friendly smtplib alternative for emailing

Post by kysodev » Mon Apr 11, 2022 12:56 am

davef wrote:
Sun Apr 10, 2022 8:28 am
Have you got print statements in your WiFiconnect to show:
Sorry only just seen this, It appears we're on a bit of a time difference, 1:55am here for me :lol:

The MicroPython version is 1.18, specifically "esp8266-20220117-v1.18.bin". Sadly not got an ESP62 around. I have another of the same board I'm using now which is the NodeMCU ESP8266-12F and then another ESP8266 but that second one seems to be a more generic one from what I've read. Okay, I may be wrong, I'm sure when I was flashing the board that the terminal was stating it was 4MB, reading this description of the exact board I have looks like it says 32Mbit so not sure on that one now. This is board in any case: https://www.az-delivery.com/en/products ... 7236140128

I do have a print statement for that and it returns:

Code: Select all

Network config: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '208.67.222.222')
I've managed to successfully send an email with exact same code apart from replacing umail for smtplib but from within VSCode don't think that's the issue but will happily be advised If I'm missing something. Thanks


If it helps this is my complete code:
main.py

Code: Select all

import ConnectToWifi, ConfigureDisplay

display = ConfigureDisplay.configure() # Configure display
ConnectToWifi.do_connect(display) # Connect to Wi-Fi

import umail
smtp = umail.SMTP('mail.smtp2go.com', 2525, 'username', 'password')
smtp.to('recipientEmail')
smtp.send("This is a simple test ")
smtp.quit()
ConnectToWifi.py:

Code: Select all

import network
import time

def do_connect(display):
    station = network.WLAN(network.STA_IF)
    
    display.fill(0)
    display.text("Connecting...", 0, 0, 1)
    display.show()
    time.sleep(0.75)
    
    if not station.isconnected():
        station.active(True)
        station.connect("ssid", "password")
    
    display.fill(0)
    display.text("connected to:", 0, 0, 1)
    display.text("ssid", 0, 10, 1)
    display.show()
    
    
    print("Network config:", station.ifconfig())
    return display

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: looking for a user friendly smtplib alternative for emailing

Post by davef » Mon Apr 11, 2022 2:53 am

Is 2525 the port that mail.smtp2go.com uses?

And error message still OSError: -2

Now the minus is something Micropython adds to the error msg so look just for:

Code: Select all

OSError: 2
I see another post

Code: Select all

https://forum.micropython.org/viewtopic.php?f=2&t=12106
with minus signs, for non-Micropython code so I could be wrong

Post Reply