Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python, Imap & Gmail
Search
Shrayas Rajagopal
April 25, 2015
Technology
2
230
Python, Imap & Gmail
Shrayas Rajagopal
April 25, 2015
Tweet
Share
More Decks by Shrayas Rajagopal
See All by Shrayas Rajagopal
Know Thy Packets - PyCon India 2025
shrayasr
0
17
Embrace the Grind
shrayasr
0
110
The programmers guide to timestamps and timezones - PyCon India 2023
shrayasr
0
150
A gentle introduction to types
shrayasr
1
150
The beauty that is PostgreSQL
shrayasr
2
330
Hylang - A Python powered Lisp AND a Lisp powered Python
shrayasr
2
750
My journey with the world of Open Source
shrayasr
1
200
Introduction to SQLAlchemy ORMs
shrayasr
1
290
Intro to Mac OSX
shrayasr
2
190
Other Decks in Technology
See All in Technology
組織全員で向き合うAI Readyなデータ利活用
gappy50
5
1.8k
serverless team topology
_kensh
3
250
様々なファイルシステム
sat
PRO
0
270
CLIPでマルチモーダル画像検索 →とても良い
wm3
1
650
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
2
170
Zero Trust DNS でより安全なインターネット アクセス
murachiakira
0
120
「タコピーの原罪」から学ぶ間違った”支援” / the bad support of Takopii
piyonakajima
0
160
プロファイルとAIエージェントによる効率的なデバッグ / Effective debugging with profiler and AI assistant
ymotongpoo
1
580
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
14
82k
Dify on AWS 環境構築手順
yosse95ai
0
170
生成AI時代のPythonセキュリティとガバナンス
abenben
0
160
SREのキャリアから経営に近づく - Enterprise Risk Managementを基に -
shonansurvivors
1
480
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
84
9.2k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Automating Front-end Workflow
addyosmani
1371
200k
How to train your dragon (web standard)
notwaldorf
97
6.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
How GitHub (no longer) Works
holman
315
140k
Transcript
Chennaipy APRIL 2015
Python Imap & Gmail
@shrayasr Logic Soft
Email
Email
Email
Email POP3
Email POP3 IMAP
POP3 Post Office Protocol
SIMPLE POP3 Post Office Protocol
SIMPLE ONE MAILBOX POP3 Post Office Protocol
SIMPLE SSL/TLS ONE MAILBOX POP3 Post Office Protocol
SIMPLE SSL/TLS ONE MAILBOX MOVE POP3 Post Office Protocol
SIMPLE SSL/TLS ONE MAILBOX MOVE ALL PARTS POP3 Post Office
Protocol
IMAP Internet Msg Access Protocol
IMAP Internet Msg Access Protocol ASYNC
IMAP Internet Msg Access Protocol ASYNC MAILBOXes
IMAP Internet Msg Access Protocol ASYNC CLIENTS MAILBOXes
IMAP Internet Msg Access Protocol ASYNC CLIENTS MAILBOXes SEARCHES
IMAP Internet Msg Access Protocol ASYNC CLIENTS MAILBOXes SEARCHES EXTENSIONS
Gmail @gmail.com
Settings > Forwarding and POP/IMAP /settings/security/lesssecureapps Requirements
Python
>>> import imaplib
>>> import imaplib Standard Library
CASES
Check the count of mails in my INBOX 1
email_count.py import imaplib client = IMAP4_SSL("imap.gmail.com", "993") client.login("
[email protected]
","barfoo") result, response
= client.select("INBOX") if result == "OK": print "count is %s" % response[0]
Gmail Handling folders
Folders
Folders
Folders
“[Gmail]/” Prefix
[Gmail]/Spam SPAM
[Gmail]/Drafts DRAFTS
Gmail IMAP &
Gmail IMAP
Gmail IMAP EXT ENSIONS!
Gmail IMAP EXTENSIONS!
Mails sent to Vijay from 11 Apr to date 2
to: (<email>) after:<date> before: <date>
?
SEARCH IMAP
SEARCH X-‐GM-‐RAW IMAP Gmail
Mails sent to Vijay from 11 Apr to date 2
# ... client.select("[Gmail]/Sent Mail") client.search(None, '(X-GM-RAW "to: (
[email protected]
) after:2015/4/11 before:2015/4/26")')
# ... advanced_search.py
# ... client.select("[Gmail]/Sent Mail") client.search(None, '(AND (TO "
[email protected]
") (FROM "
[email protected]
"))')
# ... imap_search.py
Add label “Foo” to all mails sent to me from
[email protected]
3
STORE IMAP
STORE X-‐GM-‐LABELS IMAP Gmail
# ... client.select("INBOX") emails = client.search(None, '(FROM "
[email protected]
")') for email
in emails[1][0].split(): client.store(email, '+X-GM-LABELS', "foo") # ... add_foo_label.py
RECAP
• IMAP, POP3
• IMAP, POP3 • imaplib
• IMAP, POP3 • imaplib • Gmail IMAP extensions
X-‐GM-‐RAW, X-‐GM-‐LABELS
/Fin @shrayasr