MyWackoSite: pythontips

https://wiki.ebal.gr     Version: 1 (09/22/2013 13:30)

pythontips


Hits : 3,773

Python Tips



How to remove a specific mail from a mbox


PYTHON
#!/usr/bin/env python
# Created by Evaggelos Balaskas on Sun Jul 25 06:36:29 EEST 2010
# Last edit on Thu Jul 29 17:56:08 EEST 2010
# Remove mails from mbox by subject

import sys
import mailbox
import re

SUBJECTS = (
        'automatically rejected mail',
        'delivery failure',
        'delivery notification',
        'delivery status notification',
        'failure notice',
        'mail delivery failed',
        'mail delivery failure',
        'nondeliverable',
        'returned mail',
        'undeliverable',
        'undelivered',
        'warning: could not send message for past'
)

if len(sys.argv) == 2:
    for message in mailbox.mbox( sys.argv[1] ) :
        s = message['subject']
        if s is not None :
                flag = 1
                for i in SUBJECTS:
                        m = re.search ( i, str(s), re.I )
                        if m is not None :
                                flag = 0
                                break
                if flag :
                        print message
        else:   
                print message
else:   
        print "Usage should be: " + sys.argv[0] + " mbox > new.mbox"


top

Remove duplicate mails from mailbox




top


Remove duplicate mails from mailbox (using sets instead of lists)




top