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
Code: Select all
Error: cannot unpack file; cannot detect archive format
Code: Select all
Traceback (most recent call last):
File "<stdin>", line 25, in <module>
File "umail.py", line 30, in __init__
OSError: -2
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.