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


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

我得到一个错误AttributeError:"字节"对象没有属性"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

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

最新更新