From 6be75eed079b67cadf5a2566c1bf86d1df258580 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 15 Jul 2009 15:13:39 -0400 Subject: [PATCH] Add unicode-header handling to send_pgp_mime.py Also: Switched email.message_from_string() to email.parser.Parser().parsestr() for parsing the header, for access to the headersonly option. Adjusted module import order to alphebetize non-mime email modules. Added return_realname to source_email(), which makes it more useful to be-handle-mail (currently uncommitted). Added a doctest for the plain() output and removed redundant Content-Type line from the doctests (which we'd removed from the output with the last commit). Note that many doctests _will_fail_ unless me@big.edu and you@big.edu are in your gpg keyring. At some point I should make those addresses options to --test... --- interfaces/email/interactive/send_pgp_mime.py | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/interfaces/email/interactive/send_pgp_mime.py b/interfaces/email/interactive/send_pgp_mime.py index 43c26d3..a10674a 100644 --- a/interfaces/email/interactive/send_pgp_mime.py +++ b/interfaces/email/interactive/send_pgp_mime.py @@ -32,24 +32,25 @@ import smtplib import subprocess import sys import tempfile +import types try: from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication - from email.generator import Generator from email.encoders import encode_7or8bit + from email.generator import Generator + from email.parser import Parser from email.utils import getaddress - from email import message_from_string except ImportError: # adjust to old python 2.4 from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMENonMultipart import MIMENonMultipart - from email.Generator import Generator from email.Encoders import encode_7or8bit + from email.Generator import Generator + from email.parser import Parser from email.Utils import getaddresses - from email import message_from_string getaddress = getaddresses class MIMEApplication (MIMENonMultipart): @@ -165,7 +166,7 @@ def flatten(msg): text = fp.getvalue() return text -def source_email(msg): +def source_email(msg, return_realname=False): """ Search the header of an email Message instance to find the sender's email address. @@ -173,7 +174,9 @@ def source_email(msg): froms = msg.get_all('from', []) from_tuples = getaddresses(froms) # [(realname, email_address), ...] assert len(from_tuples) == 1 - return [addr[1] for addr in from_tuples][0] + if return_realname == True: + return from_tuples[0] # (realname, email_address) + return from_tuples[0][1] # email_address def target_emails(msg): """ @@ -219,13 +222,23 @@ class Mail (object): Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit - Content-Type: text/plain Content-Disposition: inline check 1 2 check 1 2 - >>> signed = m.sign() + >>> print flatten(m.plain()) + Content-Type: text/plain; charset="us-ascii" + MIME-Version: 1.0 + Content-Transfer-Encoding: 7bit + From: me@big.edu + To: you@big.edu + Subject: testing + + check 1 2 + check 1 2 + + >>> m.sign() >>> signed.set_boundary('boundsep') >>> print m.stripSig(flatten(signed)).replace('\\t', ' '*4) Content-Type: multipart/signed; @@ -319,10 +332,12 @@ class Mail (object): --boundsep-- """ def __init__(self, header, body): - self.header = header + self.header = header.strip() self.body = body - - self.headermsg = message_from_string(self.header) + if type(self.header) == types.UnicodeType: + self.header = self.header.encode("ascii") + p = Parser() + self.headermsg = p.parsestr(self.header, headersonly=True) def sourceEmail(self): return source_email(self.headermsg) def targetEmails(self): -- 2.26.2