Python3.6でgmailを受信&表示するプログラムを書いています。
ヘッダー情報で、
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
となっているメールは以下のコードで解読できるのですが、ヘッダー情報が
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
のメールが読み込まれるとエラーが出てしまいます。
どうすれば、両者とも解読&表示することができるのでしょうか。
import imaplib import email import base64 gmail = imaplib.IMAP4_SSL("imap.gmail.com") gmail.login(“mymail@gmail.com”,”my password”) gmail.select('INBOX',readonly=True) typ, [data] = gmail.search(None,'(UNSEEN)') for num in data.split(): count+=1 result, d = gmail.fetch(num,"(RFC822)") raw_email = d[0][1] msg = email.message_from_string(raw_email.decode('iso-2022-jp')) msg_encoding = email.header.decode_header(msg.get('Subject'))[0][1] or 'iso-2022-jp' #本文の取得 body = "" if msg.is_multipart(): for payload in msg.get_payload(): if payload.get_content_type() == "text/plain": body = payload.get_payload() if msg_encoding == 'utf-8': body = base64.urlsafe_b64decode(body.encode('ASCII')).decode("utf-8") else: if msg.get_content_type() == "text/plain": body = msg.get_payload() if msg_encoding == 'utf-8': body = base64.urlsafe_b64decode(body.encode('ASCII')).decode("utf-8") print(body) print('') gmail.close() gmail.logout()

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/29 02:24 編集
2018/09/29 02:26
2018/09/30 02:11
2018/09/30 05:25