? ? ? ? 因为某些需要,我去给我的服务器做了这玩意,并且把它做成了我喜欢的接口,附带了默认配置方案,可以做到快速发送。二话不说,上源码~
'''
主函数:send()
帮助信息:help()
提示:
使用主函数send()时候会返回一个list,由以下2个元素构成:
识别码(int): 0为正常,其他都为错误
信息(str): 执行信息,若识别码不为0时为报错信息
'''
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def send(
# 这里填写默认配置 ,方便个人使用
server = {
# 发送服务器地址(str)
"host": None,
# 发送服务器端口(int)
"port": None
}
,
sender = {
# 发信人邮箱地址(str)
"id": None,
# 发信人名称(str)
"name": None,
# 发信人密码或授权码(str)
"pwd": None
},
addressee_list = [
{
# 收件人邮箱地址(str)
"id": None,
# 收件人名称(str)
"name": None
}
# 此处可以多加几个和上面格式一样的收件人信息,做到群发的效果
],
mail = {
# 邮件标题(str)
"title": None,
# 邮件内容(str)
"body": None
}
):
connect = None
try:
connect=smtplib.SMTP_SSL(server["host"], server["port"])
except:
return [5,"连接失败"]
try:
connect.login(sender["id"], sender["pwd"])
except:
connect.quit()
return [4,"用户名或密码错误"]
if mail["title"] == "None" or mail["body"] == None:
connect.quit()
return [3,"邮件内容有空"]
for addressee in addressee_list:
msg = None
try:
msg=MIMEText(mail["body"],'plain','utf-8')
msg['From']=formataddr([sender["name"],sender["id"]])
msg['To']=formataddr([addressee["name"],addressee["id"]])
msg['Subject']=mail["title"]
except:
connect.quit()
return [2,"创建邮件失败"]
try:
connect.sendmail(sender["id"],addressee["id"],msg.as_string())
except:
connect.quit()
return [1,"未能成功发送"]
connect.quit()
return [0,"正常"]
def help():
help_list = [
{
"key": "server",
"format": "dict",
"mark": '{"host": <服务器地址(str)>,"port": <端口(int)>}'
},
{
"key": "sender",
"format": "dict",
"mark": '{"id": <发件人账号(str)>,"name": <发件人名称(str)>,"pwd": <发件人密码(str)>}'
},
{
"key": "addressee_list",
"format": "list",
"mark": '[{"id": <收件人账号(str)>,"name": <收件人名称(str)>},.....]'
},
{
"key": "mail",
"format": "dict",
"mark": '{"title": <标题(str)>,"body": <内容(str)>}'
}
]
print("="*100)
print("帮助文档:")
print("格式:[参数(类型)]注释||<注释(类型)>")
print("-"*100)
print("使用函数send()")
for single in help_list:
print("\t[" + single["key"] + "(" + single["format"] + ")]" + single["mark"])
print("="*100)
|