AttributeError:“ bytes”对象在python 3中没有属性“ encode”

时间:2020-04-01 16:27:59

标签: python-3.x encode

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

我收到错误AttributeError:'bytes'对象没有属性'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 (自身,_text,_subtype,_charset,policy)

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

1 个答案:

答案 0 :(得分:1)

文档介绍了字符集编码的详细信息:https://docs.python.org/3/library/email.mime.html#email.mime.text.MIMEText

添加标题时,请使用msg.add_header('Subject', subject) 而不是msg['Subject'] = subject

相关问题