Move from physics.drexel.edu/~wking/unfolding-disasters to blog.tremily.us.
[blog.git] / posts / send_pgp_mime.mdwn
1 [[!meta  title="Command line PGP/MIME"]]
2 [[!meta  date="2009-02-04 11:39:37"]]
3
4 I've been scripting up a few things recently and my inability to send
5 nicely formatted [[OpenPGP|PGP]] messages was starting to really bug
6 me.  I had been encrypting my logwatch output with gpg and just
7 emailing the raw gpg out to myself, but it's annoying to have to ESC-P
8 every time I want to decrypt a log.  The tipping point came while I
9 was writing up a script to automatically email grades out to my
10 students (not that any of the student's care about PGP, but it's the
11 principle of the thing ;), since there's no way I'm going to send them
12 raw gpg output (they would die) and there's also no way I'm going to
13 hold Mutt's hand while it signs the emails.  I dunno why Mutt doesn't
14 have some `--batch` mode options to support PGP, but there it is.
15
16 So I [wrote my own][send_pgp_mime.py].  Not very complicated really,
17 just implementing the [RFC 3156][] specs.  There's obviously room for
18 improvement, but it works well enough to make me happy at the momemt.
19 [[Email me|contact]] with (encrypted!) feedback.
20
21     echo 'use_agent' >> ~/.gnupg/gpg.conf
22     export GPG_TTY=$(tty)
23     eval $(gpg-agent --daemon)
24     FROM="From: me@example.edu"
25     for student in $(cat big_list); do
26         HEAD=$(echo -e "$FROM\nTo: $STUDENT@example.edu\nSubject: Grades")
27         BODY=$(echo -e "$STUDENT,\n\nGood job!")
28         send_pgp_mime -H <(echo "$HEAD") -B <(echo "$BODY") --mode sign
29     done
30
31 Note that I'm brushing some [[gpg-agent]] details under the rug.  `man
32 gpg-agent` does a reasonable job of clarifying.
33
34 [send_pgp_mime.py]: http://git.tremily.us/?p=be.git;a=blob;f=interfaces/email/interactive/send_pgp_mime.py;hb=HEAD
35 [RFC 3156]: http://www.ietf.org/rfc/rfc3156.txt
36
37 [[!tag tags/linux]]
38 [[!tag tags/programming]]
39 [[!tag tags/python]]