[Pythonmac-SIG] controlling Mail.app and GnuPG

Henning Hraban Ramm hraban at fiee.net
Sun Aug 24 12:02:55 CEST 2008


The issue was raised before, but I didn't find any hints how to solve  
my little problems...

BTW, this is framework Python 2.5.2 on OSX 10.5

It is known that the public API of Apple's Mail.app is incomplete and  
buggy, therefore stuff like the GnuPG plugin stay in eternal beta (and  
just don't work on my German system - I guess i might my a language  
problem).
Anyway.

I tried to write a little helper for GPG encrypted mails - before I  
dragged the "encrypted.asc" attachment in some folder, called gpg in  
the bash, copied the decrypted text back in my reply window and  
replaced all the MIME codes with proper umlauts etc.
(I can encrypt the mail via S/MIME, my friend can decrypt that, but  
didn't get S/MIME encryption to work in her Linux Thunderbird or  
Evolution, so she sends GPG encrypted...)

So, my script gets the selected message in Mail.app...

* issue (1) [appscript syntax for Mail.app]:
I can list the attachments with appscript, but I didn't find a way to  
save them or to access them as files.

Thus, I read the source of the message.

* issue (2) [dysfunctional email module]:
The email module (std lib) claims to be able to decode multipart MIME,  
but finds only 1 part in that text that consists of at least 2  
attachments. So I cannot use that.

Thus, I extract the "PGP MESSAGE" part using regular expressions.

In the cheeseshop there are two modules to access GnuPG:
http://pypi.python.org/pypi/pygpgme/0.1
http://pypi.python.org/pypi/GnuPGInterface/0.3.2

The first is from 2006 and seems unmaintained, the latter has a really  
ugly, un-pythonic syntax. I could also call gnupg via system calls.
I don't care about security issues on my own system, otherwise I could  
just forget any approach, for I must store my passphrase anywhere, and  
if the decrypted text stays in my bash history, it isn't safe anymore.

Ok, so I decrypt the encrypted part via GnuPGInterface (you must open  
and close 3 different file handles for that). The decrypted text still  
contains header information and signature and is in MIME encoding  
(i.e. "quoted-printable").

Using regular expressions I look for the MIME boundary string and  
extract the message part.
The headers tell me, it's in ISO-8859-15 encoding (= latin-9), same as  
8859-1 (= latin-1) plus Euro sign.

unicode(text, 'quoted-printable') isn't possible, because it returns  
string, not unicode.

Of course - "quoted-printable" is no encoding by itself, but needs an  
encoding defined.

unicode(text.decode('quoted-printable'), 'iso-8859-15') should work.

* issue (3) [missing encoding in Python]:
Python doesn't know ISO-8859-15! -- Hey, that exists since 1999!

So I can't just use the detected encoding, but hardcode 'latin-1'  
instead (should usually fit my needs, but isn't generally usable),  
i.e. unicode(text.decode('quoted-printable'), 'latin-1')

Now I've finally the readable text of the message in proper unicode.
Let's make a reply mail:

reply = message.reply(opening_window=True)

I don't understand why usually a message window is invisible, but  
anyway, that works so far.

reply.content.set(decrypted_text)

Doesn't work: If I try it in PyCrust, the new text appears above the  
initial content, can't delete that.
In my script I get only a CommandError:

CommandError -10000: Apple event handler failed.
	Failed command: app(u'/Applications/ 
Mail.app').outgoing_messages.ID(409455472).content.set(u'bla')

Despite that error, when I close Mail.app, a new message containing my  
text appears, i.e. instead of setting the content of my previously  
created reply mail, it makes a new outgoing message in the background.

* issue (4) [appscript syntax for Mail.app]:
How can I set the complete content of a reply message?


Your help on any of that issues is appreciated!


Greetlings from Lake Constance!
Hraban
---
http://www.fiee.net
https://www.cacert.org (I'm an assurer)



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3708 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20080824/ef75cdfb/attachment.bin>


More information about the Pythonmac-SIG mailing list