[英]No national characters in mail subject using imaplib, email in Python to read gmail inbox
Windows. Windows。 Python 3.9. Python 3.9。 As a value of mail subject I get other characters instead of Polish characters - I get:作为邮件主题的值,我得到其他字符而不是波兰字符 - 我得到:
Odpowied�� automatyczna: "Re: Program licz��cy ceny i sprzeda�� w allegro dla EAN��w" Odpowied�� automatyczna: “Re: Program licz��cy ceny i sprzeda�� w allegro dla EAN��w”
instead of:代替:
Odpowiedź automatyczna: "Re: Program liczący ceny i sprzedaż w allegro dla EANów" Odpowiedź automatyczna: "Re: Program liczący ceny i sprzedaż w allegro dla EANów"
How to make it correct?如何使它正确? Should I apply some codepage information somewhere?我应该在某处应用一些代码页信息吗?
I notice all out dictionary values are string except for the subject which is of type Header.我注意到所有的字典值都是字符串,除了类型为 Header 的主题。
import imaplib, email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user', 'pwd')
mail.select('inbox')
data = mail.search(None, 'ALL')
_, data = mail.fetch(str(7), '(RFC822)')
message = email.message_from_bytes(data[0][1])
out = {
'from': message['from'],
'subject': message['subject'],
'to': message['Delivered-To'],
'datetime': message['Date'],
'cc': message['Cc']
}
if understand correctly you need to decode bytes.如果理解正确,您需要解码字节。 try something like尝试类似的东西
from email.header import decode_header
subject, encoding = decode_header(message["subject"])[0]
if isinstance(subject, bytes):
subject = subject.decode(encoding)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.