# This program uses the IMAPClient and pyzmail36 modules to retrieve emails
#
# Note:
# - imap_info file has each item on separate line
# - email address used is specially created for this chapter
# - use input() for password to prevent storing in unencrypted file

# Connecting to an IMAP Server
import imapclient

with open('imap_info') as config:
    # imap_cfg = [email, password, imap server, port]
    imap_cfg = config.read().splitlines()

imap_obj = imapclient.IMAPClient(imap_cfg[2], ssl=True)

# Logging in to the IMAP Server
print(imap_obj.login(imap_cfg[0], imap_cfg[1]))

# Searching for Email
import pprint  # Don't do this, imports should be at the top of the file
pprint.pprint(imap_obj.list_folders())

imap_obj.select_folder('INBOX', readonly=True)

uids = imap_obj.search(['SINCE', '01-Jan-2015', 'NOT', 'FROM', 'alice@exmaple.com'])

print(uids)

# Increase size limit from 10,000 bytes to 10,000,000 bytes
import imaplib
imaplib._MAXLINE = 10000000

# Fetching an Email and Marking it as Read
raw_messages = imap_obj.fetch(uids, ['BODY[]'])
pprint.pprint(raw_messages)

#imap_obj.select_folder('INBOX', readonly=False)  # Allows marking as read when using fetch()

# Getting Email Addresses from a Raw Message
import pyzmail
message = pyzmail.PyzMessage.factory(raw_messages[uids[0]][b'BODY[]'])

print(message.get_subject())
print(message.get_addresses('from'))
print(message.get_addresses('to'))
print(message.get_addresses('cc'))
print(message.get_addresses('bcc'))

# Getting the Body from a Raw Message
if message.text_part is not None:
    print(message.text_part.get_payload().decode(message.text_part.charset))
if message.html_part is not None:
    print(message.html_part.get_payload().decode(message.html_part.charset))

# Deleting Emails
imap_obj.select_folder('INBOX', readonly=False)  # Allows deleting of emails
uids = imap_obj.search(['ON', '15-Sep-2018'])
print(uids)
print(imap_obj.delete_messages(uids))
print(imap_obj.expunge())

# Disconnecting from the IMAP Server
imap_obj.logout()

@@ -351,22 +351,8 @@
     
-# imap_cfg = %5B
 emai
@@ -363,21 +363,16 @@
 assword,
- imap
  server,
@@ -380,22 +380,8 @@
 port
-%5D%0A    imap_cfg
  = c
@@ -444,19 +444,14 @@
 ent(
-imap_cfg%5B2%5D
+server
 , ss
@@ -516,32 +516,23 @@
 gin(
-imap_cfg%5B0%5D, imap_cfg%5B1%5D
+email, password
 ))%0A%0A

Update P02_receivingEmail multiple assignment for credentials