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
|
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import
csv
from
email.mime.multipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
from
email.mime.application
import
MIMEApplication
from
email.utils
import
COMMASPACE, formatdate
from
email.mime.base
import
MIMEBase
from
email
import
encoders
import
time
def
send_mail(server, fro, to, subject, text, chao):
assert
type
(server)
=
=
dict
assert
type
(to)
=
=
list
msg
=
MIMEMultipart()
msg[
'From'
]
=
fro
msg[
'Subject'
]
=
subject
msg[
'To'
]
=
COMMASPACE.join(to)
# COMMASPACE==', '
msg[
'Cc'
]
=
chao
# COMMASPACE==', '
msg[
'Date'
]
=
formatdate(localtime
=
True
)
xlsxpart
=
MIMEBase(
"application"
,
"msword"
)
xlsxpart.set_payload(
open
(u
'通知.docx'
,
'rb'
).read(),
'utf-8'
)
xlsxpart.add_header(
'Content-Disposition'
, u
'attachment'
, filename
=
"通知.docx"
)
msg.attach(xlsxpart)
import
smtplib
smtp
=
smtplib.SMTP(server[
'name'
], server[
'port'
])
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(server[
'user'
], server[
'passwd'
])
smtp.sendmail(fro, to
+
[chao], msg.as_string())
smtp.close()
if
__name__
=
=
'__main__'
:
server
=
{
'name'
:
'xx.163.com'
,
'user'
:
'xxxxx'
,
'passwd'
:
'xxx'
,
'port'
:
25
}
fro
=
'xxxxxxxx'
subject
=
'xxxxx'
with
open
(
'1.csv'
,
'U'
) as csvfile:
# reader = csv.DictReader(csvfile)
reader
=
csv.reader(csvfile)
l
=
[]
for
row
in
reader:
l.append(row)
print
(
"开始"
)
for
i
in
l:
name
=
i[
0
]
mail
=
i[
2
:
10
]
chao
=
i[
1
]
b
=
'''
您好!
值此“八一”建军节之际,祝愿贵公司蓬勃发展,建军节快乐!
'''
a
=
"尊敬的{0}:"
.
format
(name)
text
=
a
+
b
while
''
in
mail:
mail.remove('')
to
=
mail
print
(
'to'
, to,
'ok'
,
'chao'
, chao,
'ok'
)
time.sleep(
7
)
send_mail(server, fro, to, subject, text, chao)
|
1.csv 的格式
第一列是客户名称,第二列是抄送的人,第三列和后面的是要发送的人。
1
2
|
msg[
'To'
]
=
COMMASPACE.join(to)
# COMMASPACE==', '
msg[
'Cc'
]
=
chao
# COMMASPACE==', '
|
1
|
smtp.sendmail(fro, to
+
[chao], msg.as_string())
|
需要特别注意的是上面,抄送的是 1个人。
Cc 是指要抄送,要注意抄送的是一个人还是多个。如果是多个,需要按照to的格式。
本脚本有BUG,就是收附件的时候,如果附件是中文名字,手机客户端收的时候会显示一个 未命名。电脑客户端没有问题。
暂未解决。
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1953223,如需转载请自行联系原作者