AttributeError: 'bytes' object has no attribute 'encode' in python 3

AttributeError: 'bytes' object has no attribute 'encode' in python 3

email = 'aashita9317@gmail.com'
send_email('Happy Hour Update',message,
            from_addr=GMAIL_LOGIN, to_addr=email)

我收到一个错误 AttributeError: 'bytes' object has no attribute 'encode'

def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=GMAIL_LOGIN):
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
msg['Reply-To'] = 'happyhours@noreply.com'

上面是它引用的send_email函数,指向msg = MIMEText(message) 请帮忙

if _charset is None:
        try:
            _text.encode('us-ascii')
            _charset = 'us-ascii'
        except UnicodeEncodeError:
            _charset = 'utf-8'

上面是它在init(self, _text, _subtype, _charset, policy)

中~\anaconda3\lib\email\mime\text.py中引用的anaconda3文件]

文档解释了字符集编码细节:https://docs.python.org/3/library/email.mime.html#email.mime.text.MIMEText

添加 headers 时,使用例如msg.add_header('Subject', subject) 而不是 msg['Subject'] = subject