bpo-19770: Update smtp.send_message to use email.message.as_bytes()

This commit is contained in:
Dong-hee Na 2020-01-19 15:51:25 +09:00
parent cd7db76a63
commit 98467cb67b
1 changed files with 6 additions and 10 deletions

View File

@ -46,13 +46,13 @@ import io
import re import re
import email.utils import email.utils
import email.message import email.message
import email.generator
import base64 import base64
import hmac import hmac
import copy import copy
import datetime import datetime
import sys import sys
from email.base64mime import body_encode as encode_base64 from email.base64mime import body_encode as encode_base64
from email.policy import SMTP as SMTP_POLICY, SMTPUTF8 as SMTPUTF8_POLICY
__all__ = ["SMTPException", "SMTPNotSupportedError", "SMTPServerDisconnected", "SMTPResponseException", __all__ = ["SMTPException", "SMTPNotSupportedError", "SMTPServerDisconnected", "SMTPResponseException",
"SMTPSenderRefused", "SMTPRecipientsRefused", "SMTPDataError", "SMTPSenderRefused", "SMTPRecipientsRefused", "SMTPDataError",
@ -958,15 +958,11 @@ class SMTP:
" internationalized email support, but the server" " internationalized email support, but the server"
" does not advertise the required SMTPUTF8 capability") " does not advertise the required SMTPUTF8 capability")
international = True international = True
with io.BytesIO() as bytesmsg: if international:
if international: mail_options = (*mail_options, 'SMTPUTF8', 'BODY=8BITMIME')
g = email.generator.BytesGenerator( flatmsg = msg_copy.as_bytes(policy=SMTPUTF8_POLICY)
bytesmsg, policy=msg.policy.clone(utf8=True)) else:
mail_options = (*mail_options, 'SMTPUTF8', 'BODY=8BITMIME') flatmsg = msg_copy.as_bytes(policy=SMTP_POLICY)
else:
g = email.generator.BytesGenerator(bytesmsg)
g.flatten(msg_copy, linesep='\r\n')
flatmsg = bytesmsg.getvalue()
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options, return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
rcpt_options) rcpt_options)