Sample code for 30+ languages & platforms
Chilkat2-Python

Examine Junk/NonJunk Flags on Outlook.com and GMail

See more IMAP Examples

Examines the Junk and NonJunk FLAGS for IMAP on Outlook.com and GMail.com.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

imap = chilkat2.Imap()

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

# Connect to the outlook.com IMAP server.
# We'll do the same for GMail below..
# Use TLS
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap-mail.outlook.com")
if (success != True):
    print(imap.LastErrorText)
    sys.exit()

# Login
success = imap.Login("myAccount@outlook.com","OUTLOOK_PASSWORD")
if (success != True):
    print(imap.LastErrorText)
    sys.exit()

# Begin keeping a log of the session here.
imap.KeepSessionLog = True

# Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
if (success != True):
    print(imap.LastErrorText)
    sys.exit()

# The LastResponse property contains the full response of the last command.
# The response to the SELECT mailbox command shows the FLAGS that can be set
# for emails in the mailbox.
selectResponse = imap.LastResponse
print(selectResponse)

# The response to "SELECT Inbox" looks like this:
# There are no FLAGS for Junk/NonJunk..

# * 4 EXISTS
# * 0 RECENT
# * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
# * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
# * OK [UIDVALIDITY 14] UIDVALIDITY value
# * OK [UIDNEXT 1719] The next unique identifier value
# aaac OK [READ-WRITE] SELECT completed.

# Fetch the flags for each message in the mailbox.
# Prior to running this example, (using Mozilla Thunderbird) I marked one of the emails
# in my outlook.com Inbox as Junk.  
sequenceNum = 1
while sequenceNum <= imap.NumMessages :
    flags = imap.FetchFlags(sequenceNum,False)
    print(str(sequenceNum) + ": " + flags)
    sequenceNum = sequenceNum + 1

# The output of the above loop is this:

# 	1: \Seen
# 	2: \Seen
# 	3: \Seen
# 	4: \Seen

# As you can see, nothing is marked as Junk/Spam.  This concurs with the list of FLAGS
# documented in the response to the SELECT command.  Apparently, there is no flag for junk/nonjunk
# for outlook.com.  Mozilla Thunderbird must be storing additional information about particular
# emails locally.  (In other words, perhaps the Junk/NonJunk flag is information stored locally 
# by Thunderbird, and is not a flag set on the server.)

print("---- Session Log ----")
print(imap.SessionLog)

# The session log looks like this:

# 	----IMAP REQUEST----
# 	aaac SELECT "Inbox"
# 	----IMAP RESPONSE----
# 	* 4 EXISTS
# 	* 0 RECENT
# 	* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
# 	* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
# 	* OK [UIDVALIDITY 14] UIDVALIDITY value
# 	* OK [UIDNEXT 1719] The next unique identifier value
# 	aaac OK [READ-WRITE] SELECT completed.
# 	----IMAP REQUEST----
# 	aaad FETCH 1 (FLAGS)
# 	----IMAP RESPONSE----
# 	* 1 FETCH (FLAGS (\Seen))
# 	aaad OK FETCH completed.
# 	----IMAP REQUEST----
# 	aaae FETCH 2 (FLAGS)
# 	----IMAP RESPONSE----
# 	* 2 FETCH (FLAGS (\Seen))
# 	aaae OK FETCH completed.
# 	----IMAP REQUEST----
# 	aaaf FETCH 3 (FLAGS)
# 	----IMAP RESPONSE----
# 	* 3 FETCH (FLAGS (\Seen))
# 	aaaf OK FETCH completed.
# 	----IMAP REQUEST----
# 	aaag FETCH 4 (FLAGS)
# 	----IMAP RESPONSE----
# 	* 4 FETCH (FLAGS (\Seen))
# 	aaag OK FETCH completed.

imap.Disconnect()

# --------------------------------------------------------------------------------
# Now let's check GMail..
# Again, I've marked one email as Junk.

imap.KeepSessionLog = False
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.gmail.com")
if (success != True):
    print(imap.LastErrorText)
    sys.exit()

# Login
success = imap.Login("myAccount@gmail.com","GMAIL-IMAP-PASSWORD")
if (success != True):
    print(imap.LastErrorText)
    sys.exit()

imap.KeepSessionLog = True

success = imap.SelectMailbox("Inbox")
if (success != True):
    print(imap.LastErrorText)
    sys.exit()

selectResponse = imap.LastResponse
print(selectResponse)

# The response to GMail "SELECT Inbox" looks like this:

# * FLAGS (\Answered \Flagged \Draft \Deleted \Seen $Forwarded $NotPhishing $Phishing $label1 $label2 $label3 $label4 $label5 Junk NonJunk)
# * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $Forwarded $NotPhishing $Phishing $label1 $label2 $label3 $label4 $label5 Junk NonJunk \*)] Flags permitted.
# * OK [UIDVALIDITY 3] UIDs valid.
# * 46 EXISTS
# * 0 RECENT
# * OK [UIDNEXT 4147] Predicted next UID.
# * OK [HIGHESTMODSEQ 403404]
# aaai OK [READ-WRITE] Inbox selected. (Success)

sequenceNum = imap.NumMessages
minSeqNum = 1
if (sequenceNum > 10):
    minSeqNum = sequenceNum - 10

while sequenceNum >= minSeqNum :
    flags = imap.FetchFlags(sequenceNum,False)
    print(str(sequenceNum) + ": " + flags)
    sequenceNum = sequenceNum - 1

# The output (for GMail) of the above loop is this:

# 	46: Junk \Seen
# 	45: NonJunk \Answered \Seen
# 	44: NonJunk \Seen
# 	43: \Answered \Seen
# 	42: \Answered \Seen
# 	41: NonJunk \Seen
# 	40: NonJunk \Answered \Seen
# 	39: NonJunk \Seen
# 	38: NonJunk \Seen
# 	37: NonJunk \Seen
# 	36: NonJunk \Seen

# As you can see, the email I marked as "Junk" using Mozilla Thunderbird has the Junk flag set.
# This concurs with GMail's list of FLAGS.  Apparently, since the IMAP server has a "Junk" flag,
# Thunderbird set it when I marked the email as Junk. 

imap.Disconnect()