邮件html转换文本怎么弄,如何将html电子邮件作为常规文本接收?

在Python3.x中,您可以通过导入“imaplib”和“email”包以非常简单的方式完成这项工作。虽然这是一个老职位,但也许我的回答可以帮助这个职位的新人。status, data = self.imap.fetch(num, '(RFC822)')

email_msg = email.message_from_bytes(data[0][1]) #email.message_from_string(data[0][1])

#If message is multi part we only want the text version of the body, this walks the message and gets the body.

if email_msg.is_multipart():

for part in email_msg.walk():

if part.get_content_type() == "text/plain":

body = part.get_payload(decode=True) #to control automatic email-style MIME decoding (e.g., Base64, uuencode, quoted-printable)

body = body.decode()

elif part.get_content_type() == "text/html":

continue

现在您可以打印body变量,它将是纯文本格式:)如果它对您足够好,那么选择它作为接受的答案将是很好的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值