Sending email

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
User avatar
mbirth
Posts: 25
Joined: Mon Oct 20, 2014 1:00 pm
Location: Berlin, Germany
Contact:

Re: Sending email

Post by mbirth » Thu Oct 22, 2015 9:54 am

Ferretproof wrote:That would be award winning, especially when it also supports HTTPS.
(tell me when I ask too much :D)
By the way: In the announcement I read that the Blynk library fully supports HTTPS calls. Is that so? Do they have a working MicroPython implementation for HTTPS?
pyBoard v1.0 + LCD32MKv1.0 | WiPy + Expansion Board | GitHub

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Sending email

Post by danicampora » Thu Oct 22, 2015 11:00 am

It says that it supports SSL. Blynk doesn't use HTTPS, they have their own protocol build on top of sockets. Check the library:

https://github.com/wipy/wipy/blob/maste ... lynkLib.py

User avatar
mbirth
Posts: 25
Joined: Mon Oct 20, 2014 1:00 pm
Location: Berlin, Germany
Contact:

Re: Sending email

Post by mbirth » Thu Oct 22, 2015 12:11 pm

danicampora wrote:It says that it supports SSL. Blynk doesn't use HTTPS, they have their own protocol build on top of sockets. Check the library
It seems they're using the ussl module which is not yet mentioned in the documentation.

Is this available on the WiPy, too?
pyBoard v1.0 + LCD32MKv1.0 | WiPy + Expansion Board | GitHub

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Sending email

Post by danicampora » Thu Oct 22, 2015 12:22 pm

The link you provide is the one for the pyboard docs, the one for the WiPy is (the one on the sticky post and on the KS update):

http://micropython.org/resources/docs/en/latest/wipy/

And yes, ssl is documented:

http://micropython.org/resources/docs/e ... /ussl.html

Cheers,
Daniel

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Sending email

Post by danicampora » Thu Oct 22, 2015 12:24 pm

Is this available on the WiPy, too?
That's a funny question because that Blynk library was written by us (with the invaluable help of Volodymyr Shymanskyy), specially for the WiPy. It's also a fact the the WiPy is the only MCU platform that can connect to the Blynk servers using ssl...

User avatar
mbirth
Posts: 25
Joined: Mon Oct 20, 2014 1:00 pm
Location: Berlin, Germany
Contact:

Re: Sending email

Post by mbirth » Thu Oct 22, 2015 1:11 pm

danicampora wrote:The link you provide is the one for the pyboard docs, the one for the WiPy is (the one on the sticky post and on the KS update):

http://micropython.org/resources/docs/en/latest/wipy/

And yes, ssl is documented:

http://micropython.org/resources/docs/e ... /ussl.html
Okay, my bad. Sorry. I somehow ended up on the main MicroPython documentation.

Is it planned to add the WiPy-specific documentation to the main MicroPython docs - like it is for the pyBoard?
pyBoard v1.0 + LCD32MKv1.0 | WiPy + Expansion Board | GitHub

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Sending email

Post by danicampora » Thu Oct 22, 2015 1:13 pm

Yes, it is on our plans.

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Sending email

Post by danicampora » Thu Oct 22, 2015 2:56 pm

Hi!

I have a first version of the smtplib.py file, it's compact and doesn't need any external libraries. Attached here. Please test and let us know your results, with your help we can make it good enough to make it part of the official library. Examples:

Following scripts assume an already established internet connection

Without TLS/SSL:

Code: Select all

to = 'recipient@mail.com'
gmail_user = 'some.user@wipy.io'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp02.hostnet.nl", 587, tls=False)
smtpserver.helo()
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject: Email from the WiPy \n'
msg = header + '\n Hi, \n this is The WiPy emailing ;-) \n\n Cheers, \n The WiPy'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
With TLS/SSL:

Code: Select all

to = 'recipient@mail.com'
gmail_user = 'some.user@gmail.com'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com", 465)
smtpserver.helo()
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject: Email from the WiPy \n'
msg = header + '\n Hi, \n this is The WiPy emailing ;-) \n\n Cheers, \n The WiPy'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
The WiPy doesn't support turning a standard socket into a secure one on the fly, it needs to be secure from the very beginning, therefore there's no need to call

Code: Select all

smtpserver.starttls()
, but on the other hand, when creating the stmp instance with tls=True (default value), the correct secure port must be used (465 in the case of gmail).

Remark about gmail: Since sometime ago gmail (and also other popular mail servers) require OAuth2 to authenticate unless you allow "non secure apps" to access your account. OAuth2 is out of the scope of smtplib and I really don't know how to deal with it yet. Details can be found here: https://support.google.com/accounts/ans ... 0255?hl=en


Cheers,
Daniel
Attachments
smtplib.zip
(2.68 KiB) Downloaded 4312 times

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Sending email

Post by Damien » Thu Oct 22, 2015 6:03 pm

Looks good Daniel! May I suggest that you define an SMTP_SSL class (derived from SMTP) that is used to create secure connections, rather than the tls argument to the constructor of the SMTP class? That's how CPython does it and will make the code much more compatible with Python on the desktop. Also, it would allow you to provide a default SSL port of 465 when port is not given in the SMTP_SSL constructor.

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Sending email

Post by danicampora » Thu Oct 22, 2015 6:15 pm

May I suggest that you define an SMTP_SSL class (derived from SMTP) that is used to create secure connections, rather than the tls argument to the constructor of the SMTP class?
Yes, that's a good idea, thanks Damien!

Post Reply