前提・実現したいこと
Pythonを使用して、自分のgmail内のメールを習得するコードを書きたいと思っています。
(参考:https://qiita.com/stkdev/items/a44976fb81ae90a66381)
参考サイトのコードを真似て実装しよう試みたのですが、下記の様なエラーコードが発生し
実装できません。エラーコードの内容は推測するに、自分のメール内に指定した読み込み方法以外
のメールがあると思うのですが、その対応外のメールの処理をどうすれば良いのかが分かりません。
下記に書いたソースコードの「for num~」以下にある 「raw_email = d[0][1]」
のコードの意味なのですが、d[0][1]のリスト表記の意味が分からないです。
該当のソースコード
Python3
1import imaplib 2import email 3 4UserName = ("hoge") 5PassName = ("hoge") 6 7gmail = imaplib.IMAP4_SSL("imap.gmail.com", '993') 8gmail.login(UserName, PassName) 9gmail.select("inbox")#メールを全部習得する 10 11# ALLでSearchすると指定したラベルの全てのメールを読み込む 12#[data]でメールに割り振らているidを全習得し、後にこのdataをfetch()で本文を習得する 13typ, [data] = gmail.search(None, "(ALL)") 14 15for num in data.split(): 16 ### 各メールへの処理 ### 17 result, d = gmail.fetch(num, "(RFC822)") 18 raw_email = d[0][1]★ 19 20 #文字コード取得用 21 msg = email.message_from_string(raw_email.decode('utf-8')) 22 msg_encoding = email.header.decode_header(msg.get('Subject'))[0][1] or 'iso-2022-jp' 23 #パースして解析準備 24 msg = email.message_from_string(raw_email.decode(msg_encoding)) 25
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) <ipython-input-35-f627aacee1e4> in <module>() 6 #文字コード取得用 7 msg = email.message_from_string(raw_email.decode('utf-8')) ----> 8 msg_encoding = email.header.decode_header(msg.get('Subject'))[0][1] or 'iso-2022-jp' 9 #パースして解析準備 10 msg = email.message_from_string(raw_email.decode(msg_encoding)) ~\Anaconda3\lib\email\header.py in decode_header(header) 78 for string, charset in header._chunks] 79 # If no encoding, just return the header with no charset. ---> 80 if not ecre.search(header): 81 return [(header, None)] 82 # First step is to parse all the encoded parts into triplets of the form TypeError: expected string or bytes-like object
補足情報(FW/ツールのバージョンなど)
Anaconda
Python
お忙しいとは思いますが、よろしくお願いいたします。
情報に不足がありましたら、ご指摘お願いいたします。
あなたの回答
tips
プレビュー