From: John Bent <johnbent@lanl.gov>
To: sup-talk <sup-talk@rubyforge.org>
Subject: Re: [sup-talk] printing emails (but not attachments)
Date: Wed, 17 Mar 2010 13:25:02 -0600 [thread overview]
Message-ID: <1268853590-sup-9103@guava.lanl.gov> (raw)
In-Reply-To: <1268341771-sup-2631@guava.lanl.gov>
[-- Attachment #1: Type: text/plain, Size: 1254 bytes --]
Excerpts from John Bent's message of Thu Mar 11 14:11:21 -0700 2010:
> I've been piping emails into muttprint which works well unless there's
> a binary attachment. When there's a binary attachment, muttprint
> prints it and it usually takes tens and tens of pages. Whoops.
>
> Anyone else figure out how to print emails but not attachments?
>
Well, I'm sure I've once again reinvented the wheel but I wrote my own
script to print emails without printing binary attachments. It reads an
email, pipes the headers to muttprint, then pipes text/plain and
text/html through w3m into muttprint, for everything else it pipes "#
Attachment: filename (type)." It requires w3m and muttprint to be in
the user's path.
I'm sorry that it's horribly in violation of sup philosophy but I wrote
it in python. :( Sorry but my ruby is just barely good enough to do
'if' statements in my hooks.
It's attached in case anyone else wants it. I'm scared I really did
just reinvent the wheel, otherwise I'd add it to the wiki. To print an
email from sup using this, hit '|' in thread-view-mode to open a pipe
and then just type sup-print (assuming you've put this attachment in
your path). I believe all the python imports it uses come standard.
--
Thanks,
John
[-- Attachment #2: sup-print --]
[-- Type: application/octet-stream, Size: 1963 bytes --]
#! /usr/bin/env python
import email
import sys
from subprocess import Popen,PIPE
# read in the email message from an arg or stdin
def get_message():
try:
fp = open(sys.argv[1])
except IndexError:
fp = sys.stdin
return email.message_from_file(fp)
# open a pipe to muttprint
def open_muttprint():
try:
mutt = Popen("muttprint",shell=True,stdin=PIPE)
except OSError, e:
print "popen muttprint failed:", e
sys.exit()
return mutt
# write all the headers to muttprint
def write_header( msg, mutt ):
for key in msg.keys( ):
val = msg.__getitem__(key)
pipe_mutt( "%s: %s\n" % ( key, val ), mutt )
pipe_mutt( "\n", mutt ) # one extra newline in case text abuts headers
# utility for piping text to mutt
# useful for debugging, just select which one
def pipe_mutt( text, mutt ):
mutt.stdin.write(text)
#sys.stdout.write(text)
# write all the text or html parts to muttprint
# otherwise write an attachment notation
def write_part( part, mutt ):
type = part.get_content_type()
if type == "text/plain" or type == "text/html":
command = "w3m -dump -T %s" % type
try:
ch = Popen(command,shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE)
except OSError, e:
print >>sys.stderr, "Execution failed:", e
sys.exit()
ch.stdin.write(str(part.get_payload()))
ch.stdin.close()
out = ch.stdout.read().strip()
err = ch.stderr.read()
ch.wait()
if len(err):
print "Error with w3m: %s" % err
sys.exit()
pipe_mutt( out + "\n", mutt )
elif part.get_filename() is not None:
pipe_mutt("# Attachment: %s (%s)\n" % (part.get_filename(),type),mutt)
if __name__ == '__main__':
msg = get_message( )
mutt = open_muttprint()
write_header( msg, mutt )
for part in msg.walk(): write_part( part, mutt )
mutt.stdin.close()
[-- Attachment #3: Type: text/plain, Size: 140 bytes --]
_______________________________________________
sup-talk mailing list
sup-talk@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-talk
next prev parent reply other threads:[~2010-03-17 19:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-11 21:11 John Bent
2010-03-17 19:25 ` John Bent [this message]
2010-03-17 19:47 ` Michael McDermott
2010-03-18 0:28 ` John Bent
2010-03-17 20:31 ` John Bent
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1268853590-sup-9103@guava.lanl.gov \
--to=johnbent@lanl.gov \
--cc=sup-talk@rubyforge.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox