Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
μMail (MicroMail)
一个轻量级,可扩展的SMTP客户端,用于在MicroPython中发送电子邮件。
用法:
最低限度的方法
import umail
smtp = umail.SMTP('smtp.gmail.com', 587, username='my@gmail.com', password='mypassword')
smtp.to('someones@gmail.com')
smtp.send("This is an example.")
smtp.quit()
接口文档:
umail.SMTP(host, port, [ssl, username, password])
主机 - smtp 服务器
端口 - 服务器的端口号
ssl - 在服务器需要 SSL 时设置True
用户名 - 我的用户名/电子邮件到服务器
密码 - 我的密码
SMTP.login(用户名、密码)如果您在初始化服务器时未登录,请在此处进行!
SMTP.to(addrs, mail_from)
地址 - 收件人的电子邮件地址。如果有多个食谱,请使用列表,例如。['aaa@mail.com', 'bbb@mail.com']
mail_from - 手动指定 MAIL FROM 地址,默认值为 smtp 用户名。例
SMTP.write(content)要发送长电子邮件或包含大型附件的电子邮件,您很可能会超出MCU的内存限制。
使用此功能将电子邮件分解为较小的块。
每次调用 都会导致将电流发送到服务器,以便您可以加载下一个块。write()content
SMTP.send([content])完成电子邮件的编写。
使SMTP服务器实际将您的电子邮件发送到配方地址。
SMTP.quit()关闭与服务器的连接
用法1:
# # An bare minimium example for sending a email # without SSL connection # import umail smtp = umail.SMTP('smtp.gmail.com', 587, username='my@gmail.com', password='mypassword') smtp.to('someones@gmail.com') smtp.send("This is an example.") smtp.quit()
用法2:
# # Setting the MAIL FROM address # # Some services may require you to set the MAIL FROM address different than your # login username. In such cases, you can manually specify the address by # smtp.to(mail_from=address) # If argument not set, MAIL FROM address will be default to your login username. # # Read more about MAIL FROM: # https://docs.aws.amazon.com/ses/latest/DeveloperGuide/mail-from.html # import umail smtp = umail.SMTP('email-smtp.us-west-2.amazonaws.com', 587, username='myusername', password='mypassword') smtp.to('someones@gmail.com', mail_from='my@gmail.com') smtp.send("This is an example.") smtp.quit()
# # An example for sending a long email # with SSL connection # # NOTE: # If the email is too long to fit in an variable, # you may use write() to send a chunk of the email # each time. # 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\n") smtp.write("Roses are red.\n") smtp.write("Violets are blue.\n") smtp.write("...\n") smtp.send() smtp.quit()
# uMail (MicroMail) for MicroPython # Copyright (c) 2018 Shawwwn <shawwwn1@gmai.com> # License: MIT import usocket DEFAULT_TIMEOUT = 10 # sec LOCAL_DOMAIN = '127.0.0.1' CMD_EHLO = 'EHLO' CMD_STARTTLS = 'STARTTLS' CMD_AUTH = 'AUTH' CMD_MAIL = 'MAIL' AUTH_PLAIN = 'PLAIN' AUTH_LOGIN = 'LOGIN' class SMTP: def cmd(self, cmd_str): sock = self._sock; sock.write('%s\r\n' % cmd_str) resp = [] next = True while next: code = sock.read(3) next = sock.read(1) == b'-' resp.append(sock.readline().strip().decode()) return int(code), resp def __init__(self, host, port, ssl=False, username=None, password=None): import ussl self.username = username addr = usocket.getaddrinfo(host, port)[0][-1] sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) sock.settimeout(DEFAULT_TIMEOUT) sock.connect(addr) if ssl: sock = ussl.wrap_socket(sock) code = int(sock.read(3)) sock.readline() assert code==220, 'cant connect to server %d, %s' % (code, resp) self._sock = sock code, resp = self.cmd(CMD_EHLO + ' ' + LOCAL_DOMAIN) assert code==250, '%d' % code if not ssl and CMD_STARTTLS in resp: code, resp = self.cmd(CMD_STARTTLS) assert code==220, 'start tls failed %d, %s' % (code, resp) self._sock = ussl.wrap_socket(sock) if username and password: self.login(username, password) def login(self, username, password): self.username = username code, resp = self.cmd(CMD_EHLO + ' ' + LOCAL_DOMAIN) assert code==250, '%d, %s' % (code, resp) auths = None for feature in resp: if feature[:4].upper() == CMD_AUTH: auths = feature[4:].strip('=').upper().split() assert auths!=None, "no auth method" from ubinascii import b2a_base64 as b64 if AUTH_PLAIN in auths: cren = b64("\0%s\0%s" % (username, password))[:-1].decode() code, resp = self.cmd('%s %s %s' % (CMD_AUTH, AUTH_PLAIN, cren)) elif AUTH_LOGIN in auths: code, resp = self.cmd("%s %s %s" % (CMD_AUTH, AUTH_LOGIN, b64(username)[:-1].decode())) assert code==334, 'wrong username %d, %s' % (code, resp) code, resp = self.cmd(b64(password)[:-1].decode()) else: raise Exception("auth(%s) not supported " % ', '.join(auths)) assert code==235 or code==503, 'auth error %d, %s' % (code, resp) return code, resp def to(self, addrs, mail_from=None): mail_from = self.username if mail_from==None else mail_from code, resp = self.cmd(CMD_EHLO + ' ' + LOCAL_DOMAIN) assert code==250, '%d' % code code, resp = self.cmd('MAIL FROM: <%s>' % mail_from) assert code==250, 'sender refused %d, %s' % (code, resp) if isinstance(addrs, str): addrs = [addrs] count = 0 for addr in addrs: code, resp = self.cmd('RCPT TO: <%s>' % addr) if code!=250 and code!=251: print('%s refused, %s' % (addr, resp)) count += 1 assert count!=len(addrs), 'recipient refused, %d, %s' % (code, resp) code, resp = self.cmd('DATA') assert code==354, 'data refused, %d, %s' % (code, resp) return code, resp def write(self, content): self._sock.write(content) def send(self, content=''): if content: self.write(content) self._sock.write('\r\n.\r\n') # the five letter sequence marked for ending line = self._sock.readline() return (int(line[:3]), line[4:].strip().decode()) def quit(self): self.cmd("QUIT") self._sock.close()
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.008558988571167 seconds