1 Python module and tools for constructing and sending PGP/MIME email.
3 The ``pgp_mime`` module makes it easy to construct and dispatch signed
4 and/or encrypted email using PGP_ and :RFC:`3156`. It uses GnuPG_
5 (via `gpgme-tool`_) to perform the cryptography.
16 I've packaged ``pgp-mime`` for Gentoo_. You need layman_ and
17 my `wtk overlay`_. Install with::
19 # emerge -av app-portage/layman
21 # emerge -av dev-python/pgp-mime
26 ``pgp-mime`` is a simple package with no external dependencies outside
27 the Python 3 standard library. There are a number of GnuPG_ wrappers
28 for python `out there`__, but none of them seem mature/stable enough
29 to be worth installing. Instead, we use the `pyassuan`_ module to
30 talk to `gpgme-tool`_ over pipes or sockets. If this isn't working
31 for you, you need only replace the ``pgp_mime.crypt`` module to handle
39 ``pgp-mime`` is available as a Git_ repository::
41 $ git clone git://tremily.us/pgp-mime.git
43 See the homepage_ for details. To install the checkout, run the
46 $ python setup.py install
51 Pgp-mime has grown up as I've become more experienced with Python.
52 The current interface is much simpler, and there are lots of
53 docstrings showing you how to use each function.
55 If you're looking for a higher level example, pgp-mime includes a
56 command line script ``send-pgp-mime.py`` that allows you to send
57 signed and/or encrypted email from the command line. I recommend you
58 use ``gpg2`` with my `wrappers and pinentry program`_ to allow easy
59 pinentry from the command line. Here's how you could mail signed
60 grades to your class::
62 $ FROM="From: Rincewind <rincewind@uu.edu>"
64 Twoflower <tf@isa.ae.cw>|9
65 Eric Thursley <et@pseudopolis.net>|10
67 STUDENT=$(echo "$LINE" | cut -d '|' -f 1)
68 GRADE=$(echo "$LINE" | cut -d '|' -f 2)
69 HEAD=$(echo -e "$FROM\nTo: $STUDENT\nSubject: Grades")
70 BODY=$(echo -e "$STUDENT,\n\nYou got a $GRADE.\n\nGood job.")
71 send-pgp-mime.py -H <(echo "$HEAD") -B <(echo "$BODY") --mode sign
74 If you can convince your students to get PGP keys, you could also
75 encrypt their grades by changing ``--mode sign`` to ``--mode
78 Of course, if you're interested in working with students and grades,
79 you might also be interested in my `pygrader`_ package, which uses
80 pgp-mime under the hood.
82 Configuring the SMTP connection
83 -------------------------------
85 Pgp-mime supports two methods for sending messages (via
86 ``pgp_mime.mail``). It can either call your system's ``sendmail``
87 equivalent, or connect directly to an SMTP_ server using ``smtplib``.
88 Since I imagine SMTP will be more common, you can easily configure
89 your SMTP connection via ``~/.config/smtplib.conf``::
92 host: smtp.mail.uu.edu
98 All of these fields are optional. ``host`` defaults to ``localhost``
99 and ``port`` defaults to 25. If ``username`` is not given, we do not
100 attempt to login to the SMTP server after connecting.
102 If ``starttls`` is ``no`` or not given, the SMTP transaction occurs in
103 plain text (although the underlying emails will still be encrypted).
104 However, if you set a ``username`` (to login), pgp-mime will require a
105 STARTTLS_ to protect your password from sniffing.
110 Run the internal unit tests using nose_::
112 $ nosetests --with-doctest --doctest-tests pgp_mime
114 If a Python-3-version of ``nosetests`` is not the default on your
115 system, you may need to try something like::
117 $ nosetests-3.2 --with-doctest --doctest-tests pgp_mime
122 This project is distributed under the `GNU General Public License
123 Version 3`_ or greater.
131 .. _PGP: http://en.wikipedia.org/wiki/Pretty_Good_Privacy
132 .. _Gentoo: http://www.gentoo.org/
133 .. _layman: http://layman.sourceforge.net/
134 .. _wtk overlay: http://blog.tremily.us/posts/Gentoo_overlay/
135 .. _wrappers: http://wiki.python.org/moin/GnuPrivacyGuard
136 .. _pyassuan: http://blog.tremily.us/posts/pyassuan/
138 http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gpgme.git;a=blob;f=src/gpgme-tool.c;hb=HEAD
139 .. _Git: http://git-scm.com/
140 .. _homepage: http://blog.tremily.us/posts/pgp-mime/
141 .. _wrappers and pinentry program: http://blog.tremily.us/posts/gpg-agent/
142 .. _pygrader: http://blog.tremily.us/posts/pygrader/
143 .. _SMTP: http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
144 .. _STARTTLS: http://en.wikipedia.org/wiki/STARTTLS
145 .. _GnuPG: http://www.gnupg.org/
146 .. _nose: http://readthedocs.org/docs/nose/en/latest/
147 .. _GNU General Public License Version 3: http://www.gnu.org/licenses/gpl.html