-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmtp.py
More file actions
224 lines (175 loc) · 7.08 KB
/
smtp.py
File metadata and controls
224 lines (175 loc) · 7.08 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#coding: utf-8
import smtplib
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
from email.MIMEMultipart import MIMEMultipart
from email.mime.base import MIMEBase
import re
import os
import email.mime.multipart
from email.MIMEBase import MIMEBase
from email import Encoders
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def smtpCreMailWithAttach(from_addr, to_addr, subject, msg_files, attach_files):
from_name = re.split('@', from_addr)[0]
to_name = re.split('@', to_addr)[0]
# 邮件对象:
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Header(subject, 'utf-8').encode()
msgRoot['From'] = _format_addr('%s<%s>' % (from_name, from_addr))
msgRoot['To'] = _format_addr('%s<%s>' % (to_name, to_addr))
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
for msgfile in msg_files:
with open('./mail-msg/%s' %(msgfile), 'r') as msgfp:
Text= msgfp.read()
if len(Text) > 0:
if msgfile[-4:] == '.txt':
#设定纯文本信息
msgText = MIMEText(Text, 'plain', 'utf-8')
elif msgfile[-4:] == '.htm':
#设定HTML信息
msgText = MIMEText(Text, 'html', 'utf-8')
msgAlternative.attach(Text)
'''
# 邮件正文是MIMEText:
#msgRoot.attach(MIMEText(msg_text, 'plain', 'utf-8'))
for attfile in attach_files:
att = MIMEBase('application', 'octet-stream')
att.set_payload(open('send-bin/%s' %(attfile), 'rb').read())
#att.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', attfile))
att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', attfile.decode('utf-8').encode('gbk')))
encoders.encode_base64(att)
msgRoot.attach(att)
'''
return msgRoot
def smtpSendMail(from_addr, to_addr, subject, msg_lists, attach_lists, svr_inf):
print 'Connect SMTP Server:%s....' %(svr_inf['smtphost'])
smtpsvr= smtplib.SMTP(svr_inf['smtphost'], 25)
smtpsvr.set_debuglevel(0)
print 'Login in!Smtp User:%s...' %(svr_inf['smtp_user'])
smtpsvr.login(svr_inf['smtp_user'], svr_inf['smtp_pwd'])
msg = smtpCreMailWithAttach(from_addr, to_addr, subject, msg_lists, attach_lists)
#smtpsvr.sendmail(from_addr, [to_addr], msg.as_string())
smtpsvr.quit()
return 0
def sendEmail(fromAdd, toAdd, subject, msg_lists, attach_lists, svr_inf):
strFrom = fromAdd
#strTo = ', '.join(toAdd)
strTo = toAdd
server = svr_inf.get('smtphost')
user = svr_inf.get('smtp_user')
passwd = svr_inf.get('smtp_pwd')
if not (server and user and passwd) :
print 'incomplete login info, exit now'
return
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
for msgfile in msg_lists:
with open('./mail-msg/%s' %(msgfile), 'r') as msgfp:
Text = msgfp.read()
if len(Text) > 0:
if msgfile[-4: ] == '.txt':
#设定纯文本信息
msgText = MIMEText(Text.decode('gbk').encode('utf-8'), 'plain', 'utf-8')
msgAlternative.attach(msgText)
elif msgfile[-4:] == '.htm':
#设定HTML信息
msgText = MIMEText(Text.decode('gbk').encode('utf-8'), 'html', 'utf-8')
msgAlternative.attach(msgText)
#plainText = 'aaabbbccc'
#msgText = MIMEText(plainText, 'plain', 'utf-8')
#msgAlternative.attach(msgText)
#htmlText = '<B>HTML文本2</B>'
#msgText = MIMEText(htmlText, 'html', 'utf-8')
#msgAlternative.attach(msgText)
for attfile in attach_lists:
att = MIMEBase('application', 'octet-stream')
att.set_payload(open('send-bin/%s' %(attfile), 'rb').read())
#att.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', attfile))
att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', attfile.decode('utf-8').encode('gbk')))
encoders.encode_base64(att)
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.set_debuglevel(0)
smtp.connect(server)
smtp.login(user, passwd)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
return
def sendEmail1(authInfo, fromAdd, toAdd, subject, plainText, htmlText):
strFrom = fromAdd
strTo = ', '.join(toAdd)
server = authInfo.get('server')
user = authInfo.get('user')
passwd = authInfo.get('password')
if not (server and user and passwd) :
print 'incomplete login info, exit now'
return
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText(plainText, 'plain', 'utf-8')
msgAlternative.attach(msgText)
msgText = MIMEText(htmlText, 'html', 'utf-8')
msgAlternative.attach(msgText)
'''
fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
'''
smtp = smtplib.SMTP()
smtp.set_debuglevel(1)
smtp.connect(server)
smtp.login(user, passwd)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
return
def sendmail_use_mutt(to_addr, subject, msg_text, attach_lists):
file_lists = ''
for att_name in attach_lists:
file_lists = file_lists + './send-bin/' + att_name + ' '
print('echo "%s" | mutt -s "%s" %s -a%s' %(msg_text, subject, to_addr, file_lists))
#os.popen ('echo "%s" | mutt -s "%s" %s -a%s' %(msg_text, subject, to_addr, file_lists))
return 0
#pop_inf = {'pophost':'pop.163.com', 'smtphost':'smtp.qq.com', 'username':'39728797@qq.com', 'password':'qwerty-123', 'act_type':1, 'reply_mail':'madlas1977@aa.cc', 'allow_list':'', 'smtp_user':'madlas1977@163.com', 'smtp_pwd':'12345678l'}
#smtpSendMail('madlas1977@163.com', '39728797@qq.com', os.listdir('./send-bin'), pop_inf)
'''
if __name__ == '__main__' :
authInfo = {}
authInfo['server'] = 'smtp.163.com'
authInfo['user'] = 'madlas1977'
authInfo['password'] = '12345678l'
svrinfo = {}
svrinfo['smtphost'] = 'smtp.163.com'
svrinfo['smtp_user'] = 'madlas1977'
svrinfo['smtp_pwd'] = '12345678l'
fromAdd = 'madlas1977@163.com'
#toAdd = ['39728797@qq.com']
toAdd = '39728797@qq.com'
#subject = '邮件主题'
subject = 'test'
plainText = '这里是普通文本'
htmlText = '<B>HTML文本</B>'
sendEmail(fromAdd, toAdd, subject, os.listdir('./mail-msg'), '', svrinfo)
'''