Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Created January 29, 2016 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanasco/8788a8d2e40a9c8045a4 to your computer and use it in GitHub Desktop.
Save jvanasco/8788a8d2e40a9c8045a4 to your computer and use it in GitHub Desktop.
silly little file for reading postfix files on a dev server.
import argparse
import os
import subprocess
"""
$ mailq
show mail queue contents
$ sudo postcat -q {queue_id}
show message details
http://manpages.ubuntu.com/manpages/lucid/man1/postcat.1.html
reading email on dev is a pain because of the encoding.
this will reformat the email to human readable:
$ sudo python read_email.py -q {queue_id}
you can also specify a text file (-f) if you have the email as a file.
IMPORTANT make sure you do this with permissions. otherwise `postcat` can't read the file and their error message isn't great
"""
def clean_message(text):
# process this in order
_map = [('=\n', ''),
('=09', '\t'),
('=3D', '='),
('=20', ' '),
]
for (a, b) in _map:
text = text.replace(a, b)
return text
def parse_args():
parser = argparse.ArgumentParser(description='make file readable')
parser.add_argument('-f',
'--filename',
type = str,
help = 'Which file for action?'
)
parser.add_argument('-q',
'--queue_id',
type = str,
help = 'What queue id file for action?'
)
args = parser.parse_args()
print args
if not args.filename and not args.queue_id:
raise ValueError("Missing filename or queue_id")
if args.filename:
fname = args.filename
if not os.path.exists(fname):
raise ValueError("Invalid filename: `%s`" % fname)
try:
text = open(fname, 'r').read().strip()
text = clean_message(text)
print "*" * 100
print text
print "*" * 100
except:
raise
if args.queue_id:
queue_id = args.queue_id
proc = subprocess.Popen(["postcat", "-q", queue_id], stdout=subprocess.PIPE)
(out, err) = proc.communicate()
if err:
raise ValueError("Error communicating... %s" % err)
output_clean = clean_message(out)
print "*" * 100
print output_clean
print "*" * 100
if __name__ == '__main__':
parse_args()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment