Commit de845a29 authored by Antoine Beaupré's avatar Antoine Beaupré
Browse files

properly encode email headers

parent d69159fc
......@@ -25,6 +25,8 @@ from email.mime.message import MIMEMessage
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib
import subprocess
......@@ -457,9 +459,11 @@ mailto: who to send the mail to (usually similar to recipient, but can be used t
p2.set_payload(encrypted)
msg = MIMEMultipart('encrypted', None, [p1, p2], protocol="application/pgp-encrypted")
msg.preamble = _('This is a multi-part message in PGP/MIME format...')
msg['Subject'] = self.subject
msg['From'] = self.mailfrom
msg['To'] = self.mailto
msg['Subject'] = Header(self.subject.encode('utf-8'), 'UTF-8').encode()
name, address = parseaddr(self.mailfrom)
msg['From'] = formataddr((Header(name.encode('utf-8'), 'UTF-8').encode(), address))
name, address = parseaddr(self.mailto)
msg['To'] = formataddr((Header(name.encode('utf-8'), 'UTF-8').encode(), address))
return msg
class NowrapHelpFormatter(optparse.IndentedHelpFormatter):
......
......@@ -233,7 +233,7 @@ Content-Description: PGP Key <keyid>, uid <uid> \(<idx\), signed by <keyid>
match = re.compile("""Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";
boundary="===============%s=="
MIME-Version: 1.0
Subject: Your signed OpenPGP key
Subject: .*
From: nobody@example.com
To: nobody@example.com
......@@ -260,7 +260,7 @@ Content-Transfer-Encoding: 7bit
def test_weird_from(self):
"""make sure we don't end up with spaces in our email address"""
self.email = EmailFactory(self.ui.tmpkeyring.export_data(self.pattern), self.pattern, 'Antoine Beaupré <anarcat@orangeseeds.org>', 'Antoine Beaupré (home address) <anarcat@anarcat.ath.cx>', 'nobody@example.com')
match = re.compile("""From: [^ ]*$""", re.DOTALL | re.MULTILINE)
match = re.compile("""From: (([^ ]* )|("[^"]*" ))?<[^> ]*>$""", re.DOTALL | re.MULTILINE)
self.assertRegexpMatches(self.email.as_string(), match)
class KeyserverTests(BaseTestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment