Exim filter pipe script only working on last email - not current

391 views Asked by At

This little combination is processing the second to most recent email coming in - not the most recent.

Here's the filter:

# Exim filter
save /srv/domain.com/bin/mail 660
pipe "/srv/domain.com/bin/sendToMailChimp.py"

It works - i.e. I can see it saving to the mail file and it does call the script.. so great! So far so good..

Here's the script:

#!/usr/bin/python

MAILBOX_DIR = '/srv/domain.com/mailboxes/enqace/mail'
import mailbox
import logging
import time

time.sleep(10)

logging.basicConfig(filename='/srv/domain.com/mailboxes/enqace/logging.log',level=logging.DEBUG)

inbox = mailbox.mbox(MAILBOX_DIR)

logging.info('Script called')

for key in inbox.iterkeys():
    logging.info(key)
    message = inbox[key]
    subject = message['subject']
    logging.info(subject)


logging.info('===FINISH====')

(Prints 0-6 along with subject lines of each)

I can tail the log. It runs on getting an email - but seems to parse the mail mbox and completes before getting to the most recent email (i.e. the last one). It then hits the last one (the most recent) the next time an email comes in. So if there were 6 mails already in the mail file the new one - the 7th - subject line doesn't come up in the log entry. Only when an 8th comes in..

Is the script in effect running to quickly - like before exim has had a chance to send to the mail file? If so whats the work around? Adding a sleep(10) doesn't seem to do anything as seen above..

UPDATE: By pulling up a python prompt I can see that python does get the correct amount of keys (i.e. message num) when called outside of the pipe

MAILBOX_DIR = 'mail'
import mailbox
inbox = mailbox.mbox(MAILBOX_DIR)

for key in inbox.iterkeys():
    print key

(Prints 0-7)

0

There are 0 answers