77from roundup import __version__
88from roundup .date import get_timezone , Date
99
10+ from email import charset
1011from email .utils import formatdate , formataddr , specialsre , escapesre
12+ from email .charset import Charset
1113from email .message import Message
1214from email .header import Header
1315from email .mime .base import MIMEBase
1416from email .mime .text import MIMEText
1517from email .mime .multipart import MIMEMultipart
18+ from email .mime .nonmultipart import MIMENonMultipart
1619
1720from roundup .anypy import email_
1821from roundup .anypy .strings import b2s , s2b , s2u
2629class 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-
3632def 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