-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsendMail.py
More file actions
114 lines (82 loc) · 2.72 KB
/
sendMail.py
File metadata and controls
114 lines (82 loc) · 2.72 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
from email.header import Header
import os.path
def sendMail(server,user,password,From,To,subject,body,file_list):
server = smtplib.SMTP(server)
server.docmd("EHLO server")
server.starttls()
server.login(user, password)
main_msg = email.MIMEMultipart.MIMEMultipart()
#text_msg = email.MIMEText.MIMEText(body)
text_msg = email.MIMEText.MIMEText(body,'plain','gbk')
main_msg.attach(text_msg)
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
for file_name in file_list:
try:
data = open(file_name, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
email.Encoders.encode_base64(file_msg)
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)
main_msg.attach(file_msg)
except:
pass
main_msg['From'] = From
main_msg['To'] =To
main_msg['Subject'] = Header(subject,'gbk')
main_msg['Date'] = email.Utils.formatdate( )
fullText = main_msg.as_string( )
# 用smtp发送邮件
try:
server.sendmail(From, To, fullText)
finally:
server.quit()
def sendMail_HTML(server,user,password,From,To,subject,body,file_list,reply_to,encode,bLogin=False):
server = smtplib.SMTP(server)
server.docmd("EHLO server")
if bLogin:
server.starttls()
server.login(user, password)
main_msg = email.MIMEMultipart.MIMEMultipart()
#text_msg = email.MIMEText.MIMEText(body)
text_msg = email.MIMEText.MIMEText(body,'html',encode)
main_msg.attach(text_msg)
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
for file_name in file_list:
try:
data = open(file_name, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
email.Encoders.encode_base64(file_msg)
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)
main_msg.attach(file_msg)
except:
pass
frmHeader = Header ('server status report',encode)
frmHeader.append('<' + From + '>', 'ascii' )
main_msg['From'] = frmHeader
main_msg['To'] =To
main_msg['Reply-To'] = reply_to
main_msg['Subject'] = Header(subject,encode)
main_msg['Date'] = email.Utils.formatdate( )
fullText = main_msg.as_string( )
# 用smtp发送邮件
try:
result = server.sendmail(From, To, fullText)
return result
finally:
server.quit()
return None
if __name__=="__main__":
data = ''
sendMail_HTML ( 'smtp.126.com', 'newsalertservice','', '[email protected]', "", "server reboot", 'vultr server reboot', (),'[email protected]','utf8',True)