Created
June 25, 2014 15:59
-
-
Save mhl/57008cc52d4d081285a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import mailbox | |
import os | |
import re | |
from subprocess import check_output | |
import sys | |
top_level_mail_directory = os.path.join(os.environ['HOME'], 'Mail') | |
if len(sys.argv) != 2: | |
print "Usage: OUTPUT-MBOX-FILENAME" | |
sys.exit(1) | |
output_filename = sys.argv[1] | |
def handle_mail(filename): | |
# header_name = 'Envelope-to' | |
# work_header_value = 'mark-mysocietyaddress@longair.net' | |
# header_name = 'Delivered-To' | |
# work_header_value = 'mark@mysociety.org' | |
header_name = 'Envelope-to' | |
work_header_value = 'mark-mysocietycron@longair.net' | |
with open(filename, 'rb') as file_input: | |
header_value = check_output( | |
['formail', '-x', header_name], | |
stdin=file_input).strip() | |
if header_value != work_header_value: | |
return | |
print filename | |
with open(output_filename, 'ab') as mbox_output: | |
with open(filename, 'rb') as file_input: | |
mbox_output.write("From IGNORETHIS\n") | |
mbox_output.write(file_input.read()) | |
mbox_output.write("\n\n") | |
for folder_name in sorted(os.listdir(top_level_mail_directory)): | |
nnml_directory = os.path.join(top_level_mail_directory, folder_name) | |
if not os.path.isdir(nnml_directory): | |
continue | |
if folder_name in ('spam', 'temporary'): | |
continue | |
for numbered_filename in os.listdir(nnml_directory): | |
if not re.search(r'^\d+$', numbered_filename): | |
continue | |
full_mail_filename = os.path.join(nnml_directory, numbered_filename) | |
handle_mail(full_mail_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment