Skip to content

Commit a1f47c9

Browse files
committed
updated mailgw to no longer use mimetools based on jerrykan's patch
1 parent fb17c37 commit a1f47c9

File tree

8 files changed

+745
-405
lines changed

8 files changed

+745
-405
lines changed

roundup/cgi/client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SysCallError(Exception):
3232
FormError, NotFound, NotModified, Redirect, SendFile, SendStaticFile,
3333
DetectorError, SeriousError)
3434
from roundup.cgi.form_parser import FormParser
35-
from roundup.mailer import Mailer, MessageSendError, encode_quopri
35+
from roundup.mailer import Mailer, MessageSendError
3636
from roundup.cgi import accept_language
3737
from roundup import xmlrpc
3838

@@ -1538,12 +1538,11 @@ def send_error_to_admin(self, subject, html, txt):
15381538
to = [self.mailer.config.ADMIN_EMAIL]
15391539
message = MIMEMultipart('alternative')
15401540
self.mailer.set_message_attributes(message, to, subject)
1541-
part = MIMEBase('text', 'html')
1542-
part.set_charset('utf-8')
1543-
part.set_payload(html)
1544-
encode_quopri(part)
1541+
part = self.mail.get_text_message('utf-8', 'html')
1542+
part.set_payload(html, part.get_charset())
15451543
message.attach(part)
1546-
part = MIMEText(txt)
1544+
part = self.mail.get_text_message()
1545+
part.set_payload(text, part.get_charset())
15471546
message.attach(part)
15481547
self.mailer.smtp_send(to, message.as_string())
15491548

roundup/mailer.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
from roundup import __version__
88
from roundup.date import get_timezone, Date
99

10+
from email import charset
1011
from email.utils import formatdate, formataddr, specialsre, escapesre
12+
from email.charset import Charset
1113
from email.message import Message
1214
from email.header import Header
1315
from email.mime.base import MIMEBase
1416
from email.mime.text import MIMEText
1517
from email.mime.multipart import MIMEMultipart
18+
from email.mime.nonmultipart import MIMENonMultipart
1619

1720
from roundup.anypy import email_
1821
from roundup.anypy.strings import b2s, s2b, s2u
@@ -26,13 +29,6 @@
2629
class MessageSendError(RuntimeError):
2730
pass
2831

29-
def encode_quopri(msg):
30-
orig = s2b(msg.get_payload())
31-
encdata = quopri.encodestring(orig)
32-
msg.set_payload(b2s(encdata))
33-
del msg['Content-Transfer-Encoding']
34-
msg['Content-Transfer-Encoding'] = 'quoted-printable'
35-
3632
def nice_sender_header(name, address, charset):
3733
# construct an address header so it's as human-readable as possible
3834
# even in the presence of a non-ASCII name part
@@ -115,24 +111,31 @@ def set_message_attributes(self, message, to, subject, author=None):
115111
# finally, an aid to debugging problems
116112
message['X-Roundup-Version'] = __version__
117113

114+
def get_text_message(self, _charset='utf-8', _subtype='plain'):
115+
message = MIMENonMultipart('text', _subtype)
116+
cs = Charset(_charset)
117+
if cs.body_encoding == charset.BASE64:
118+
cs.body_encoding = charset.QP
119+
message.set_charset(cs)
120+
del message['Content-Transfer-Encoding']
121+
return message
122+
118123
def get_standard_message(self, multipart=False):
119124
'''Form a standard email message from Roundup.
120125
Returns a Message object.
121126
'''
122-
charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8')
123127
if multipart:
124128
message = MIMEMultipart()
125129
else:
126-
message = MIMEText("")
127-
message.set_charset(charset)
130+
message = self.get_text_message(getattr(self.config, 'EMAIL_CHARSET', 'utf-8'))
128131

129132
return message
130133

131134
def standard_message(self, to, subject, content, author=None):
132135
"""Send a standard message.
133136
134137
Arguments:
135-
- to: a list of addresses usable by rfc822.parseaddr().
138+
- to: a list of addresses usable by email.utils.parseaddr().
136139
- subject: the subject as a string.
137140
- content: the body of the message as a string.
138141
- author: the sender as a (name, address) tuple
@@ -141,17 +144,16 @@ def standard_message(self, to, subject, content, author=None):
141144
"""
142145
message = self.get_standard_message()
143146
self.set_message_attributes(message, to, subject, author)
144-
message.set_payload(content)
145-
encode_quopri(message)
147+
message.set_payload(s2u(content))
146148
self.smtp_send(to, message.as_string())
147149

148150
def bounce_message(self, bounced_message, to, error,
149151
subject='Failed issue tracker submission', crypt=False):
150152
"""Bounce a message, attaching the failed submission.
151153
152154
Arguments:
153-
- bounced_message: an RFC822 Message object.
154-
- to: a list of addresses usable by rfc822.parseaddr(). Might be
155+
- bounced_message: an mailgw.RoundupMessage object.
156+
- to: a list of addresses usable by email.utils.parseaddr(). Might be
155157
extended or overridden according to the config
156158
ERROR_MESSAGES_TO setting.
157159
- error: the reason of failure as a string.
@@ -185,18 +187,7 @@ def bounce_message(self, bounced_message, to, error,
185187
message.attach(part)
186188

187189
# attach the original message to the returned message
188-
body = []
189-
for header in bounced_message.headers:
190-
body.append(header)
191-
try:
192-
bounced_message.rewindbody()
193-
except IOError as errmessage:
194-
body.append("*** couldn't include message body: %s ***" %
195-
errmessage)
196-
else:
197-
body.append('\n')
198-
body.append(bounced_message.fp.read())
199-
part = MIMEText(''.join(body))
190+
part = MIMEText(bounced_message.flatten())
200191
message.attach(part)
201192

202193
self.logger.debug("bounce_message: to=%s, crypt_to=%s", to, crypt_to)

0 commit comments

Comments
 (0)