Created
October 15, 2018 08:37
-
-
Save taketakeyyy/35db7fcfe60e3fe76859c9068e2f9eca to your computer and use it in GitHub Desktop.
imaplibのサンプルプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import email | |
import ssl | |
import imaplib | |
from email.header import decode_header, make_header | |
# https://docs.python.jp/3/library/imaplib.html | |
# [参考]https://qiita.com/ekzemplaro/items/a35e15865d57372f1d2b | |
# https://docs.python.jp/3.6/library/email.message.html | |
# https://docs.python.org/ja/3.7/library/email.compat32-message.html#module-email.message | |
# https://docs.python.jp/3.6/library/email.parser.html | |
# IMAP4クライアントインスタンスを作成する | |
# Gmailはport993, encry_method="ssl" | |
host = "imap.gmail.com" | |
#port = 143 | |
port = 993 | |
encry_method = "ssl" | |
if encry_method == "no-encrypt": | |
imapclient = imaplib.IMAP4(host, port) | |
#imapclient = imaplib.IMAP4(host) | |
elif encry_method == "starttls": | |
context = ssl.create_default_context() | |
imapclient = imaplib.IMAP4(host, port) | |
#imapclient = imaplib.IMAP4(host) | |
imapclient.starttls(ssl_context=context) | |
elif encry_method == "ssl": | |
context = ssl.create_default_context() | |
imapclient = imaplib.IMAP4_SSL(host, port, ssl_context=context) | |
#imapclient = imaplib.IMAP4_SSL(host, ssl_context=context) | |
imapclient.debug = 3 # 各命令をトレースする | |
# IMAPサーバーにログインする | |
username = "username@gmail.com" | |
password = "password" | |
imapclient.login(username, password) | |
imapclient.select() | |
typ, data = imapclient.search(None, "ALL") | |
download_num = 2 # ダウンロードしたい数 | |
for num in data[0].split(): | |
typ, data = imapclient.fetch(num, '(RFC822)') | |
msg = email.message_from_bytes(data[0][1]) | |
# 各ヘッダ情報を取得する | |
from_addr = str(make_header(decode_header(msg["From"]))) | |
subject = str(make_header(decode_header(msg["Subject"]))) | |
print("Message {}".format(num)) | |
#print(header_from) | |
#print(subject) | |
with open("hoge.txt", "w") as f: | |
f.write(msg.as_string()) | |
for part in msg.walk(): | |
""" | |
cte = part["Content-Transfer-Encoding"] | |
でContent-Transfer-Encodingヘッダを取得してもよいが、get_payload()の | |
decode引数をTrueにすると、 | |
Content-Transfer-Encodingヘッダに従って勝手にデコードしてくれる。 | |
ただしデコードしてくれるのはquoted-printable または base64のときのみで、 | |
それ以外の場合(別のエンコード方式や、ヘッダがない場合、ペイロード部が | |
bogus dataを持っている場合 (i.e. bogus base64 or uuencoded data))のとき | |
はそのまま返す。 | |
(bogus dataって何だ?) | |
""" | |
payload = part.get_payload(decode=True) | |
charset = part.get_content_charset() | |
if charset is not None: | |
payload = payload.decode(charset, "ignore") | |
#print(payload) | |
""" | |
for key, value in part.items(): | |
print("{}:{}".format(key, value)) | |
""" | |
#print(part.get_content_type()) | |
#print(part.get_content_charset()) | |
""" | |
for key, value in msg.items(): | |
print("{}:{}".format(key, value)) | |
""" | |
#print(msg.get_charsets()) | |
print() | |
""" | |
if msg.is_multipart(): | |
for item in msg.items(): | |
print(item) | |
""" | |
if download_num <= int(num.decode("ascii")): | |
break | |
imapclient.close() | |
imapclient.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment