python3使用smtplib发邮件被退回

背景:公司自己的邮件服务器,不支持ssl

退回信息:

This is the mail system at host mail.*****.net.cn.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

                  The mail system

<zhaimingyu@*****.net.cn>: host 127.0.0.1[127.0.0.1] said: 554 5.6.0 Reject,
   id=19530-16 - BAD HEADER (in reply to end of DATA command)
Reporting-MTA: dns; mail.*****.net.cn
X-Postfix-Queue-ID: 899B32129B
X-Postfix-Sender: rfc822; zhaimingyu@*****.net.cn
Arrival-Date: Thu, 12 Jul 2018 16:31:42 +0800 (CST)

Final-Recipient: rfc822; zhaimingyu@*****.net.cn
Original-Recipient: rfc822;zhaimingyu@*****.net.cn
Action: failed
Status: 5.6.0
Remote-MTA: dns; 127.0.0.1
Diagnostic-Code: smtp; 554 5.6.0 Reject, id=19530-16 - BAD HEADER

发件人: zhaimingyu@*****.net.cn
主题: test
收件人: zhaimingyu@*****.net.cn

test

代码是百度找来的,也从没配置过邮件服务器,对邮件的运作流程完全不懂,关键的错误信息就是'BAD HEADER',意为错误的报头,试了几天终于找到解决办法,需要配置一个邮件的date,代码:

msg_from = '发送人'
passwd = '密码'
msg_to = '收件人'        
msg = MIMEText('内容')
msg['Subject'] = '标题'
msg['From'] = msg_from
msg['Date'] = Header('时间', 'utf-8')  # 时间可以这么获取:datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
msg['To'] = msg_to
s = smtplib.SMTP(self.mail_server, int(self.mail_port))
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())

不明白为什么官方文档里没提msg['date']这个参数,真是踩了个深坑。

猜你喜欢

转载自blog.csdn.net/zmy941110/article/details/81027528