Python sendmail() delivering after 12 hour delay
I'm trying to avoid checking my email all the time, so I download all of my email once every 4 hours. I have a cron job that fetches unread messages from a Gmail account and then sends them to an account that I check.
Recently however messages have been showing up inconsistently, sometimes with over a day's delay. All messages get sent correctly, but the delay is bizarre.
Here's my code:
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_pass开发者_运维百科word)
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())
I think that it would be better to send from a Gmail account, but Gmail won't let you send email with a different recipient than the account owner. Do you know why emails are showing up with a delay, and what I can do to fix the problem? Thanks, Kevin
Have a look into the mail headers of your send mails. One is added for each mail server, together with a timestamp. Then you can see the culprit.
Take a look at your mail server logs. I bet you have lots of errors in there about GMail not accepting your email because (it looks like spam | that host has sent too many emails | other reason) and your mail server is doing it's usual "Hold on and try again later" routine.
It happened to me, rather suddenly one day for a similar type of use with GMail.
精彩评论