gpt4 book ai didi

python - “MIMEText”对象没有属性 'encode'

转载 作者:行者123 更新时间:2023-12-01 09:28:08 25 4
gpt4 key购买 nike

您好,我正在尝试找出为什么会出现此错误。这让我有点困惑。我正在使用Python 3.6

logger = logging.getLogger(__name__)
message_text = 'this is a test body'
message = MIMEText(message_text)
message['to'] = 'me@example.com'
message['from'] = 'you@example.com'
message['subject'] = 'test subject'
logger.debug('++++++++++++++++++++++++++++++++++')
logger.debug(message)
logger.debug('++++++++++++++++++++++++++++++++++')
try:
raw = base64.urlsafe_b64encode(message.encode('UTF-8')).decode('ascii')
except Exception as e:
logger.debug('---------------')
logger.debug(e)
logger.debug('---------------')

这就是输出。

++++++++++++++++++++++++++++++++++
Content-Type: text/plain; charset="us-ascii".
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit.
to: me@example.com
from: you@example.com
subject: test subject

this is a test body
++++++++++++++++++++++++++++++++++

---------------
'MIMEText' object has no attribute 'encode'
---------------

最佳答案

MIMEText 没有 .encode() 方法,看起来您需要 as_string() 方法。

message.as_string() 将返回以下字符串:

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: me@example.com
from: you@example.com
subject: test subject

this is a test body

尝试一下:

raw = base64.urlsafe_b64encode(message.as_string().encode('UTF-8')).decode('ascii')

关于python - “MIMEText”对象没有属性 'encode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50200540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com