-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_mail.py
67 lines (55 loc) · 1.78 KB
/
send_mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding:utf-8 _*-
"""
@author:gjl
@file: send_email.py
@time: 2018/07/31
code description:
(1) description: 异常时发送报警邮件
"""
from email.mime.text import MIMEText
import smtplib
def sendmail(text=None, receive_list=None, send_list=None, title=None, from_addr=None, password=None):
'''
:param text: 发送内容主体
:param receive_list: 接收人列表
:param send_list: 发送人列表
:param title: 邮件主体
:return: True
'''
if not receive_list:
return "请设置接收人列表"
if not send_list:
return "请设置发送人列表"
if not text:
return "请设置text参数"
# 第一个为文本内容,第二个设置文本格式,第三个编码格式
msg = MIMEText(text)
# 显示于发件人
# msg['From'] = '[email protected]'
# 显示与收件人
# 就是标题,最醒目的
msg['Subject'] = title
msg["Accept-Language"] = "zh-CN"
msg["Accept-Charset"] = "ISO-8859-1,utf-8"
#
# 发送方
if not from_addr:
from_addr = '' # 需手动添加发件人
# 必须是自动授权码,需要发送人的授权码
if not password:
password = '' # 需手动添加授权码默认值
# smtp服务器
smtp_server = 'smtp.263.net'
server = smtplib.SMTP(smtp_server, 25)
# 使用了ssl模式
# server = smtplib.SMTP_SSL(smtp_server, 465)
# 设置为调试模式
# server.set_debuglevel(1)
# 登陆ssl服务器
server.login(from_addr, password)
server.sendmail(from_addr, receive_list, msg.as_string())
# 退出
server.quit()
return "发送邮件成功,收件人为{}".format(receive_list)
if __name__ == '__main__':
print(sendmail(text="testing", receive_list=["[email protected]"]))