アプリケーション用のPythonのシンプルなGmail APIクライアント。
現在サポートされている行動:
必要な唯一のセットアップは、アプリケーションを承認するGoogleからOAUTH 2.0クライアントIDファイルをダウンロードすることです。
これは、https://console.developers.google.com/apis/credentialsで行うことができます。上記のリンクをクリックした後(適切なアカウントにログインする)、GoogleのAPIの資格情報を作成していない人のために、
この認証のプロジェクトを選択/作成します(新しいプロジェクトを作成する場合は、OAuthの同意画面を構成してください。アプリケーション名を設定するだけです)
[ダッシュボード]タブをクリックしてから、「APIとサービスを有効に」します。 Gmailを検索して有効にします。
[資格情報]タブをクリックしてから、「資格情報の作成」>「OAUTHクライアントID」をクリックします。
これがどのようなアプリケーションであるかを選択し、記憶に残る名前を付けます。資格情報に必要なすべての情報に記入します(たとえば、「Webアプリケーション」を選択する場合は、承認されたリダイレクトURIを追加してください。https://developers.google.com/Identity/protocols/oauth2を参照してください。
資格情報の画面に戻り、作成した資格情報の横にあるダウンロードアイコンをクリックして、JSONオブジェクトとしてダウンロードします。
このファイルを「client_secret.json」として保存し、アプリケーションのルートディレクトリに配置します。 ( Gmailクラスは、そうでないと名前を付けることを選択した場合、このファイルの名前の引数を取り入れます。)
Gmailクラスの新しいインスタンスを初めて作成したときに、ブラウザウィンドウが開き、アプリケーションに許可を与えるように求められます。これにより、「gmail-token.json」という名前のファイルにアクセストークンが保存され、1回だけ発生する必要があります。
あなたは今行くのがいいです!
認証方法に関する注意:Googleの承認を使用することは大幅に安全であり、Googleの多くのセキュリティ対策との衝突を避けるため、ユーザー名パスワード認証を使用しないことを選択しました(IMAP/SMTPを介して)。
pip (Python3)を使用してインストールします。
pip3 install simplegmail from simplegmail import Gmail
gmail = Gmail () # will open a browser window to ask you to log in and authenticate
params = {
"to" : "[email protected]" ,
"sender" : "[email protected]" ,
"subject" : "My first email" ,
"msg_html" : "<h1>Woah, my first email!</h1><br />This is an HTML email." ,
"msg_plain" : "Hi n This is a plain text email." ,
"signature" : True # use my account signature
}
message = gmail . send_message ( ** params ) # equivalent to send_message(to="[email protected]", sender=...) from simplegmail import Gmail
gmail = Gmail ()
params = {
"to" : "[email protected]" ,
"sender" : "[email protected]" ,
"cc" : [ "[email protected]" ],
"bcc" : [ "[email protected]" , "[email protected]" ],
"subject" : "My first email" ,
"msg_html" : "<h1>Woah, my first email!</h1><br />This is an HTML email." ,
"msg_plain" : "Hi n This is a plain text email." ,
"attachments" : [ "path/to/something/cool.pdf" , "path/to/image.jpg" , "path/to/script.py" ],
"signature" : True # use my account signature
}
message = gmail . send_message ( ** params ) # equivalent to send_message(to="[email protected]", sender=...)簡単ではありません!
from simplegmail import Gmail
gmail = Gmail ()
# Unread messages in your inbox
messages = gmail . get_unread_inbox ()
# Starred messages
messages = gmail . get_starred_messages ()
# ...and many more easy to use functions can be found in gmail.py!
# Print them out!
for message in messages :
print ( "To: " + message . recipient )
print ( "From: " + message . sender )
print ( "Subject: " + message . subject )
print ( "Date: " + message . date )
print ( "Preview: " + message . snippet )
print ( "Message Body: " + message . plain ) # or message.html from simplegmail import Gmail
gmail = Gmail ()
messages = gmail . get_unread_inbox ()
message_to_read = messages [ 0 ]
message_to_read . mark_as_read ()
# Oops, I want to mark as unread now
message_to_read . mark_as_unread ()
message_to_star = messages [ 1 ]
message_to_star . star ()
message_to_trash = messages [ 2 ]
message_to_trash . trash ()
# ...and many more functions can be found in message.py! from simplegmail import Gmail
gmail = Gmail ()
# Get the label objects for your account. Each label has a specific ID that
# you need, not just the name!
labels = gmail . list_labels ()
# To find a label by the name that you know (just an example):
finance_label = list ( filter ( lambda x : x . name == 'Finance' , labels ))[ 0 ]
messages = gmail . get_unread_inbox ()
# We can add/remove a label
message = messages [ 0 ]
message . add_label ( finance_label )
# We can "move" a message from one label to another
message . modify_labels ( to_add = labels [ 10 ], to_remove = finance_label )
# ...check out the code in message.py for more! from simplegmail import Gmail
gmail = Gmail ()
messages = gmail . get_unread_inbox ()
message = messages [ 0 ]
if message . attachments :
for attm in message . attachments :
print ( 'File: ' + attm . filename )
attm . save () # downloads and saves each attachment under it's stored
# filename. You can download without saving with `attm.download()` from simplegmail import Gmail
from simplegmail . query import construct_query
gmail = Gmail ()
# Unread messages in inbox with label "Work"
labels = gmail . list_labels ()
work_label = list ( filter ( lambda x : x . name == 'Work' , labels ))[ 0 ]
messages = gmail . get_unread_inbox ( labels = [ work_label ])
# For even more control use queries:
# Messages that are: newer than 2 days old, unread, labeled "Finance" or both "Homework" and "CS"
query_params = {
"newer_than" : ( 2 , "day" ),
"unread" : True ,
"labels" :[[ "Work" ], [ "Homework" , "CS" ]]
}
messages = gmail . get_messages ( query = construct_query ( query_params ))
# We could have also accomplished this with
# messages = gmail.get_unread_messages(query=construct_query(newer_than=(2, "day"), labels=[["Work"], ["Homework", "CS"]]))
# There are many, many different ways of achieving the same result with search. from simplegmail import Gmail
from simplegmail . query import construct_query
gmail = Gmail ()
# For even more control use queries:
# Messages that are either:
# newer than 2 days old, unread, labeled "Finance" or both "Homework" and "CS"
# or
# newer than 1 month old, unread, labeled "Top Secret", but not starred.
labels = gmail . list_labels ()
# Construct our two queries separately
query_params_1 = {
"newer_than" : ( 2 , "day" ),
"unread" : True ,
"labels" :[[ "Finance" ], [ "Homework" , "CS" ]]
}
query_params_2 = {
"newer_than" : ( 1 , "month" ),
"unread" : True ,
"labels" : [ "Top Secret" ],
"exclude_starred" : True
}
# construct_query() will create both query strings and "or" them together.
messages = gmail . get_messages ( query = construct_query ( query_params_1 , query_params_2 ))クエリでできることの詳細については、 query.pyのconstruct_query()のdocstringをお読みください。
追加されたい機能がある場合、またはこのプロジェクトのバグをご覧ください。問題を投稿するか、プルリクエストを送信してお知らせください!