Skip to content

Instantly share code, notes, and snippets.

@nlitsme
Last active August 29, 2015 14: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 nlitsme/3335570a60a26f2ceb06 to your computer and use it in GitHub Desktop.
Save nlitsme/3335570a60a26f2ceb06 to your computer and use it in GitHub Desktop.
send pgp mail from vim
#!/usr/local/bin/python
"""
Reads message from stdin, pgp encrypts, launch mailto: url to send with mail app.
I use this to send pgp messages from within vim.
First i type a message like this:
----
Subject: test
To: person@example.com
hi there
----
Then with "!G sendpgp" i pipe the mail through sendpgp,
which will encrypt the mail and open it in my mail app using
a mailto: url.
Willem Hengeveld <itsme@xs4all.nl>
"""
# http://tools.ietf.org/html/rfc5322
# http://tools.ietf.org/html/rfc6068
import sys
import pipes
import urllib
import subprocess
import StringIO
import webbrowser
def pgpencrypt(body, dst):
p= subprocess.Popen(["gpg2", "-e", "-a"]+map(lambda x: "-r %s" % x, dst), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err= p.communicate(body)
return out
to= ""
if len(sys.argv)>1:
to= sys.argv[1]
msg=dict()
while True:
hdrline= sys.stdin.readline()
if len(hdrline)<=1:
break
key, value= map(lambda x: x.strip(), hdrline.split(':', 1))
key= key.capitalize()
if key in msg:
msg[key] += ", "+value
else:
msg[key]= value
body= sys.stdin.read()
dst= []
if to:
for addr in map(lambda x:x.strip(), to.split(',')):
if addr:
dst.append(addr)
if msg['To']:
for addr in map(lambda x:x.strip(), msg['To'].split(',')):
if addr:
dst.append(addr)
pgpbody= pgpencrypt(body, dst)
msg['body']= pgpbody
mailto= "mailto:%s?%s" % (urllib.quote(to), "&".join(map( lambda x:"%s=%s" %(urllib.quote(x[0]),urllib.quote(x[1])), msg.items() )))
webbrowser.open(mailto)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment