0

Gmail automatically put emails under three different tabs: Primary, Social, Promotions, when I try to read the latest email with the code below, it gets all emails ignoring the tabs. How do I get email under Primary tab only? Is there another folder name I should use?

M.select("INBOX")
v_start_date = (datetime.date.today() - datetime.timedelta(days_limit)).strftime("%d-%b-%Y")
v_result, v_mail = M.uid('search', None, '(SENTSINCE {date})'.format(date=v_start_date))

imaplib code sample: https://gist.github.com/robulouski/7441883

Gmail inbox screenshot:

Gmail inbox sample

Frank
  • 1,315
  • 7
  • 24
  • 43

1 Answers1

2

If you use the GMAIL IMAP Extensions, you can do a custom search in the gmail query language using X-GM-RAW. Here's an example of getting a list of recent UIDs in the primary category:

a UID SEARCH SINCE 1-May-2018 X-GM-RAW "Category:Primary"
* SEARCH 25032 25033 25034 25035 25036
a OK SEARCH completed (Success)

And here's the promotions tab:

a UID SEARCH SINCE 1-May-2018 X-GM-RAW "Category:Promotions"
* SEARCH 25026 25028 25030 25031
a OK SEARCH completed (Success)

In Python, this probably looks like:

m.uid('search', 'SINCE 1-May-2018 X-GM-RAW "Category:Primary"')
Max
  • 10,701
  • 2
  • 24
  • 48
  • (Note, it's unclear to me whether 'Primary' needs to be localized per language, like the folder names) – Max Jul 03 '18 at 15:21