Page 2 of 4

Re: Sending email

Posted: Thu Oct 22, 2015 9:54 am
by mbirth
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?

Re: Sending email

Posted: Thu Oct 22, 2015 11:00 am
by danicampora
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

Re: Sending email

Posted: Thu Oct 22, 2015 12:11 pm
by mbirth
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?

Re: Sending email

Posted: Thu Oct 22, 2015 12:22 pm
by danicampora
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

Re: Sending email

Posted: Thu Oct 22, 2015 12:24 pm
by danicampora
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...

Re: Sending email

Posted: Thu Oct 22, 2015 1:11 pm
by mbirth
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?

Re: Sending email

Posted: Thu Oct 22, 2015 1:13 pm
by danicampora
Yes, it is on our plans.

Re: Sending email

Posted: Thu Oct 22, 2015 2:56 pm
by danicampora
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

Re: Sending email

Posted: Thu Oct 22, 2015 6:03 pm
by Damien
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.

Re: Sending email

Posted: Thu Oct 22, 2015 6:15 pm
by danicampora
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!