-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
59 lines (51 loc) · 1.51 KB
/
send_email.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
# encoding=utf8
"""
send email (need a valid email agent on the server)
"""
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import smtplib
def send_mail(me, to_list, content, subject, attachfilepath, attachfilename):
"""
send email
:param to_list:
:param content:
:param subject:
:return:
"""
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = ";".join(to_list)
# contents html
cont = MIMEText(content, _subtype='html', _charset='utf8')
msg.attach(cont)
# add attachment
att1 = MIMEText(open(attachfilepath, 'rb').read(), 'base64', 'utf8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment;filename="' + attachfilename + '"'
msg.attach(att1)
s = smtplib.SMTP()
s.connect()
for item in to_list:
if item.find(".com") != -1:
s.sendmail(me, item, msg.as_string())
s.quit()
return 1
# receiverList
receiverList = []
if __name__ == '__main__':
mail_subject = 'title'
mail_content = 'mail_content_xxx'
# path of attachfile
attachfilepath = 'attachfilepath'
# name of attachfile
attachfilename = 'attachfilename'
# 发送结果邮件
print send_mail(me, receiverList, mail_content, mail_subject, attachfilepath, attachfilename)