Python sendmail() 延迟12小时送达
我想减少查看邮件的频率,所以我每4个小时下载一次所有邮件。我设置了一个定时任务,它会从我的Gmail账户获取未读邮件,然后把这些邮件发送到我常查看的另一个账户。
不过最近邮件的到达时间有点不稳定,有时候会晚到超过一天。所有邮件都能正确发送,但这种延迟让我感到困惑。
这是我的代码:
imap_domain = "imap.gmail.com"
imap_port = 993
imap_username = 'remotegmailaccount@gmail.com'
imap_password = 'mypassword'
#smtp settings
smtp_domain = "mail.kburke.org"
smtp_port = 2626
smtp_username = "emailaddress@kburke.org"
smtp_password = "mypassword"
recipient = ['emailaddressIcheck@gmail.com']
imap_server = imaplib.IMAP4_SSL(imap_domain, imap_port)
imap_server.login(imap_username, imap_password)
imap_server.select('INBOX')
status, email_ids = imap_server.search(None, '(UNSEEN)')
server = smtplib.SMTP(smtp_domain, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(smtp_username, smtp_password)
for e in email_ids[0].split(' '):
if e is not '':
try:
raw_msg = imap_server.fetch(e, '(RFC822)')
msg = email.message_from_string(raw_msg[1][0][1])
#modify reply-to so we preserve email address
if not msg['Reply-To']:
msg['Reply-To'] = msg['From']
result = server.sendmail(msg['From'], recipient, msg.as_string())
我觉得从Gmail账户发送邮件可能会更好,但Gmail不允许你用不同的收件人发送邮件。你知道为什么邮件会有延迟吗?我该怎么解决这个问题呢?谢谢,凯文
2 个回答
1
看看你发送邮件的邮件头信息。每个邮件服务器都会添加一条记录,还有一个时间戳。这样你就能找到问题所在了。
0
看看你的邮件服务器日志。我敢打赌里面有很多错误信息,说明GMail不接受你的邮件,因为(看起来像垃圾邮件 | 那个主机发送了太多邮件 | 其他原因),然后你的邮件服务器就会照常执行“等一下再试”的操作。
我也遇到过这种情况,某一天突然发生了,和我用GMail的方式类似。