【问题标题】:AttributeError: 'bytes' object has no attribute 'encode' in python 3AttributeError:'bytes'对象在python 3中没有属性'encode'
【发布时间】:2020-07-13 11:43:10
【问题描述】:
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 文件

【问题讨论】:

    标签: python-3.x encode


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多