Spaces:
Runtime error
Runtime error
添加mail邮箱发送
Browse files- redis/mail.py +73 -0
redis/mail.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import smtplib
|
| 2 |
+
import os
|
| 3 |
+
from email.mime.text import MIMEText
|
| 4 |
+
from email.mime.multipart import MIMEMultipart
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class MyEmail:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.user = None
|
| 10 |
+
self.passwd = None
|
| 11 |
+
self.to_list = []
|
| 12 |
+
self.cc_list = []
|
| 13 |
+
self.tag = None
|
| 14 |
+
self.doc = None
|
| 15 |
+
self.txt = None
|
| 16 |
+
|
| 17 |
+
def send(self):
|
| 18 |
+
"""
|
| 19 |
+
发送邮件
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
|
| 23 |
+
server.login(self.user, self.passwd)
|
| 24 |
+
server.sendmail(
|
| 25 |
+
"<%s>" % self.user, self.to_list + self.cc_list, self.get_attach()
|
| 26 |
+
)
|
| 27 |
+
server.close()
|
| 28 |
+
print("send email successful")
|
| 29 |
+
except Exception:
|
| 30 |
+
print("send email failed")
|
| 31 |
+
|
| 32 |
+
def get_attach(self):
|
| 33 |
+
"""
|
| 34 |
+
构造邮件内容
|
| 35 |
+
"""
|
| 36 |
+
attach = MIMEMultipart()
|
| 37 |
+
# 添加邮件内容
|
| 38 |
+
txt = MIMEText(self.txt)
|
| 39 |
+
attach.attach(txt)
|
| 40 |
+
if self.tag is not None:
|
| 41 |
+
# 主题,最上面的一行
|
| 42 |
+
attach["Subject"] = self.tag
|
| 43 |
+
if self.user is not None:
|
| 44 |
+
# 显示在发件人
|
| 45 |
+
attach["From"] = "Chat<%s>" % self.user
|
| 46 |
+
if self.to_list:
|
| 47 |
+
# 收件人列表
|
| 48 |
+
attach["To"] = ";".join(self.to_list)
|
| 49 |
+
if self.cc_list:
|
| 50 |
+
# 抄送列表
|
| 51 |
+
attach["Cc"] = ";".join(self.cc_list)
|
| 52 |
+
if self.doc:
|
| 53 |
+
# 估计任何文件都可以用base64,比如rar等
|
| 54 |
+
# 文件名汉字用gbk编码代替
|
| 55 |
+
name = os.path.basename(self.doc).encode("gbk")
|
| 56 |
+
f = open(self.doc, "rb")
|
| 57 |
+
doc = MIMEText(f.read(), "base64", "gb2312")
|
| 58 |
+
doc["Content-Type"] = "application/octet-stream"
|
| 59 |
+
doc["Content-Disposition"] = 'attachment; filename="' + name + '"'
|
| 60 |
+
attach.attach(doc)
|
| 61 |
+
f.close()
|
| 62 |
+
return attach.as_string()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
my = MyEmail()
|
| 67 |
+
|
| 68 |
+
my.user = "lizhen@huggingprompt.com"
|
| 69 |
+
my.passwd = "todo"
|
| 70 |
+
my.to_list = ["hisansui@gmail.com"]
|
| 71 |
+
my.tag = "Chat注册验证码"
|
| 72 |
+
my.txt = "【123456】Chat邮箱注册验证码,您正在注册Chat账号,请勿泄露。"
|
| 73 |
+
my.send()
|