Skip to content

Commit db7595e

Browse files
Send exceptions using quoted-printable encoding (#12)
This change helps to avoid bumping into SMTP max line limits.
1 parent 9902798 commit db7595e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

webware/ExceptionHandler.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from random import randint
1010
from time import time, localtime
1111

12+
import email
1213
from email.message import Message
1314
from email.utils import formatdate
1415

@@ -489,6 +490,12 @@ def emailException(self, htmlErrMsg):
489490
Send the exception via mail, either as an attachment,
490491
or as the body of the mail.
491492
"""
493+
494+
# we use quoted-printable encoding, which will automatically split long lines.
495+
# this is important because tracebacks can contain long representations of python
496+
# data, longer than the max line length smtp servers will accept (a typical limit is 990 bytes).
497+
cs = email.charset.Charset('utf-8')
498+
cs.body_encoding = email.charset.QP
492499
message = Message()
493500

494501
# Construct the message headers
@@ -519,19 +526,18 @@ def emailException(self, htmlErrMsg):
519526
message.attach(part)
520527
part = Message()
521528
# now add htmlErrMsg
522-
part.add_header('Content-Transfer-Encoding', '7bit')
523529
part.add_header(
524530
'Content-Description',
525531
'HTML version of Webware error message')
526532
part.add_header(
527533
'Content-Disposition',
528534
'attachment', filename='WebwareErrorMsg.html')
529535
part.set_type('text/html')
530-
part.set_payload(htmlErrMsg)
536+
part.set_payload(htmlErrMsg, charset=cs)
531537
message.attach(part)
532538
else:
533539
message.set_type('text/html')
534-
message.set_payload(htmlErrMsg, 'us-ascii')
540+
message.set_payload(htmlErrMsg, charset=cs)
535541

536542
# Send the message
537543
server = self.setting('ErrorEmailServer')

0 commit comments

Comments
 (0)