{"id":3348,"date":"2022-10-16T15:01:19","date_gmt":"2022-10-16T06:01:19","guid":{"rendered":"https:\/\/apr20.net\/?p=3348"},"modified":"2022-10-16T15:01:19","modified_gmt":"2022-10-16T06:01:19","slug":"python-imaplib%e3%81%a7%e3%83%a1%e3%83%bc%e3%83%ab%e5%8f%97%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/apr20.net\/?p=3348","title":{"rendered":"Python imaplib\u3067\u30e1\u30fc\u30eb\u53d7\u4fe1"},"content":{"rendered":"<p>[php]import imaplib<br \/>\nimport email<br \/>\nfrom email.header import decode_header, make_header<\/p>\n<p># \u30e1\u30fc\u30eb\u3092\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\u3059\u308b<br \/>\nmail = imaplib.IMAP4_SSL(&#8216;xxxx.xserver.jp&#8217;, 993)<br \/>\nmail.login(&#8216;\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9&#8217;, &#8216;\u30d1\u30b9\u30ef\u30fc\u30c9&#8217;)<\/p>\n<p># \u5c4a\u3044\u305f\u30e1\u30fc\u30eb\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306binbox\u3092\u9078\u629e\u3002<br \/>\nmail.select(&#8216;inbox&#8217;)<\/p>\n<p># \/\/\u30e1\u30fc\u30eb\u3092\u4e00\u89a7\u53d6\u5f97\u3057type\u5909\u6570\u3068data\u5909\u6570\u306b\u4ee3\u5165<br \/>\ntype, data = mail.search(None, &#8216;ALL&#8217;)<\/p>\n<p># \/\/ \u30eb\u30fc\u30d7\u3092\u958b\u59cb<br \/>\nfor i in data[0].split():<br \/>\n    # \/\/ \u30e1\u30fc\u30eb\u3092\u30c7\u30b3\u30fc\u30c9\u3059\u308b<br \/>\n    ok, x = mail.fetch(i, &#8216;RFC822&#8217;)<br \/>\n    ms = email.message_from_string(x[0][1].decode(&#8216;iso-2022-jp&#8217;))<\/p>\n<p>    # \/\/ \u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u5909\u6570\u306b\u5165\u308c\u308b\u3002<br \/>\n    to_ = str(make_header(decode_header(ms[&quot;To&quot;])))<br \/>\n    date_ = str(make_header(decode_header(ms[&quot;Date&quot;])))<br \/>\n    from_ = str(make_header(decode_header(ms[&quot;From&quot;])))<br \/>\n    subject_ = str(make_header(decode_header(ms[&quot;Subject&quot;])))<\/p>\n<p># \/\/ \u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u51e6\u7406<br \/>\n# \/\/ \u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u3058\u3083\u306a\u304b\u3063\u305f\u5834\u5408<br \/>\nif ms.is_multipart() is False:<br \/>\n    payload = ms.get_payload(decode=True)<br \/>\n    charset = ms.get_content_charset()<\/p>\n<p>    if charset is not None:<br \/>\n        payload = payload.decode(charset, &quot;ignore&quot;)<\/p>\n<p># \/\/ \u305d\u308c\u4ee5\u5916\uff08\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u3060\u3063\u305f\u5834\u5408\uff09<br \/>\nelse:<br \/>\n    for part in ms.walk():<br \/>\n        payload = part.get_payload(decode=True)<br \/>\n        if payload is None:<br \/>\n            continue<br \/>\n        charset = part.get_content_charset()<br \/>\n        if charset is not None:<br \/>\n            payload = payload.decode(charset, &quot;ignore&quot;)<\/p>\n<p>    # \/\/ \u30c7\u30d0\u30c3\u30af\u7528\u3060\u3051\u3069\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b<br \/>\n    print(to_)<br \/>\n    print(date_)<br \/>\n    print(from_)<br \/>\n    print(subject_)<br \/>\n    print(payload)[\/php]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[php]import imaplib import email from em&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-3348","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/apr20.net\/index.php?rest_route=\/wp\/v2\/posts\/3348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/apr20.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/apr20.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/apr20.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/apr20.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3348"}],"version-history":[{"count":0,"href":"https:\/\/apr20.net\/index.php?rest_route=\/wp\/v2\/posts\/3348\/revisions"}],"wp:attachment":[{"href":"https:\/\/apr20.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/apr20.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/apr20.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}