Mailbox selection when retrieving Gmail with imaplib in python

There are various ways to use Gmail using imaplib, which is the standard library of python. This time I stumbled upon selecting a mailbox when getting Gmail, so I will summarize it.

Mailbox selection

After authentication, you must first select your mailbox to retrieve your email. If you do not set it in particular, INBOX will be in the specified state. To move to a mailbox other than INBOX, such as a sent box or draft box, you need to specify the mailbox name with the following method.

IMAP4.select()

You can find out the mailbox name here with the following method.

IMAP4.list()

In my environment, the result of ʻIMAP4.list ()` was as follows.


from imaplib import IMAP4_SSL
from pprint import pprint
user = 'xxx'
passwd = 'xxx'
gmail = IMAP4_SSL("imap.gmail.com", '993')
gmail.login(user, passwd)
mboxes = gmail.list()

pprint(mboxes[1])
[b'(\\HasNoChildren) "/" "INBOX"',
 b'(\\HasChildren \\Noselect) "/" "[Gmail]"',
 b'(\\All \\HasNoChildren) "/" "[Gmail]/&MFkweTBmMG4w4TD8MOs-"',
 b'(\\HasNoChildren \\Trash) "/" "[Gmail]/&MLQw33ux-"',
 b'(\\Flagged \\HasNoChildren) "/" "[Gmail]/&MLkwvzD8TtgwTQ-"',
 b'(\\Drafts \\HasNoChildren) "/" "[Gmail]/&Tgtm+DBN-"',
 b'(\\HasNoChildren \\Junk) "/" "[Gmail]/&j,dg0TDhMPww6w-"',
 b'(\\HasNoChildren \\Sent) "/" "[Gmail]/&kAFP4W4IMH8w4TD8MOs-"',
 b'(\\HasNoChildren \\Important) "/" "[Gmail]/&kc2JgQ-"']

Here, if you use ʻIMAP4.select ()earlier, for example, to move to the sent box, you can specify it in the[Gmail] xxxx` part at the end of each list as shown below. I will.


gmail.select('[Gmail]/&kAFP4W4IMH8w4TD8MOs-')

Recommended Posts

Mailbox selection when retrieving Gmail with imaplib in python
Precautions when dealing with control structures in Python 2.6
Character encoding when dealing with files in Python 3
Draft Gmail in Python
Send Gmail in Python
Freeze with send_keys of file selection when running Selenium WebDriver in Python [PhantomJS]
Precautions when dealing with ROS MultiArray types in Python
Japanese output when dealing with python in visual studio
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Attention when os.mkdir in Python
Combined with permutations in Python
Error when playing with python
Send using Python with Gmail
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Number recognition in images with Python
How to not escape Japanese when dealing with json in python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Behavior when listing in Python heapq
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Create Gmail in Python without API
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
When matplotlib doesn't work with python2.7
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Get started with Python in Blender
When using regular expressions in Python
When writing a program in Python
Working with DICOM images in Python
Send email via gmail with Python 3.4.3.
[Python] Format when to_csv with pandas
gRPC-Methods used when dealing with protocol buffers types in Python CopyFrom, Extend
Addictive point when going through http proxy with basic authentication in python
Write documentation in Sphinx with Python Livereload
When specifying multiple keys in python sort
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Snippet when searching all bits with python
Behavior when returning in the with block
Device monitoring with On-box Python in IOS-XE
Create ScriptableObject in Python when building ADX2