Python IMAP email - including Microsoft 365 and OAuth2

After creating an IMAP connection in Dashboard, messages can be fetched, mark as seen and deleted, as in the examples below.

The same code works with classic IMAP as well as with Microsoft 365 and Azure (OAuth2) connections. It is only a matter of providing the correct credentials in your Dashboard and Python code is the same as below - no knowledge of Microsoft 365 or OAuth2 is required.

Receiving e-mails

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.email.imap.get('My Connection').conn

        for msg_id, msg in conn.get():

            # Access the message
            self.logger.info(msg.data)

Marking messages seen

Call .mark_seen() on a message object to mark it as seen.

from zato.server.service import Service

# -*- coding: utf-8 -*-

# Zato
class MyService(Service):

    def handle(self):
        conn = self.email.imap.get('My Connection').conn

        for msg_id, msg in conn.get():

            # Access the message
            self.logger.info(msg.data)

            # To mark the message seen
            msg.mark_seen()

Deleting messages

Call .delete() on a message object to mark it as deleted.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.email.imap.get('My Connection').conn

        for msg_id, msg in conn.get():

            # Access the message
            self.logger.info(msg.data)

            # To delete the message
            msg.delete()


Schedule a meaningful demo

Book a demo with an expert who will help you build meaningful systems that match your ambitions

"For me, Zato Source is the only technology partner to help with operational improvements."

— John Adams
Program Manager of Channel Enablement at Keysight