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()
遇到一個奇葩問題,發送的郵件被判定爲垃圾郵件。。
解決方法:
在網頁端設置白名單,或調整反垃圾級別。