python2.7 发邮件脚本

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
import smtplib
import time
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys

reload(sys)
sys.setdefaultencoding('utf8')

msg_from = "xxxxxxxxxxxxxxxxx"  # 发件人邮箱
passwd = "xxxxxxxxxx"  # 邮箱登陆密码
msg_to = ["xxxxxxxxxxxxx", "xxxxxxxxxxxxx"]  # 接收人邮箱

m = MIMEMultipart()
subject = u'xxxxxxxxxxx'
m['Subject'] = Header(subject, 'utf-8')
m['from'] = msg_from
m['to'] = ';'.join(msg_to)
# m["cc"] = msg_from
t = time.strftime('%Y-%m-%d',time.localtime(time.time()))
m.attach(MIMEText(u'xxxxxxxxxxxxxxxxxxxxxxx', 'plain', 'utf-8'))
# 添加xls文件
att1 = MIMEApplication(open('./xxxx/xxx.xlsx', 'rb').read())
att1["Content-Type"] = 'application/octet-stream'
att1.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', u'xxxxxxxxxxxxxx_{}.xlsx'.format(t).encode('utf-8')))
m.attach(att1)

s = smtplib.SMTP("smtp.qiye.163.com", 25)
s.starttls()
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, m.as_string())
s.quit()

遇到一个奇葩问题,发送的邮件被判定为垃圾邮件。。

解决方法:

在网页端设置白名单,或调整反垃圾级别。