Tips & Tricks - Email Setup with IMAP

In this article, you'll learn how to retrieve and process emails from a mailbox using IMAP. This creates a process with an IMAP event source and an IMAP event handler. A Groovy script action writes the email to a data group.

Please note that emails in HTML format may pose a security risk (injection of JavaScript).

Sample Application

Download the sample application here. Import the download file "tipps-tricks-mailanbindung-mit-imap.zip" and select the included application and process for the import. Then go to the "Processes" module, open the "Tips & Tricks - Email Integration with IMAP" process, and enable it. The application provides the following features:

  • Identifying the Sender

  • Identifying the recipients (including CC)

  • Determining the Date of Receipt

  • Determining the Subject Line

  • Convert the content of an email from HTML to plain text

  • Convert the content of an email from plain text to HTML

  • Display of the Email in HTML

  • Displaying the Email in Plain Text

  • Identifying the Attachments

  • Identifying / Resolving Inline Attachments

  • Adding the Email to an Intrexx Data Group

  • Adding Attachments to an Intrexx Data Group

The display format for emails (HTML / Plain Text) can be changed in the application on the "Administration" page.

IMAP Event Source

The IMAP event source establishes a connection to the email server within the process. In the Properties dialog box, you can specify all connection parameters on the "Email Server" tab. You can find information about the folder path here.

For an SSL connection, the appropriate certificate must be stored in the Intrexx certificate store.

On the "Options" tab, you can specify the time interval at which the email server is checked and which emails should be retrieved.

When retrieving emails, Intrexx can only see which emails are on the server and whether they are unread or read. So if you want to process unread emails (e.g., to extract information from them and save it to a data group), it's a good idea to mark the processed emails as "read" afterward, since otherwise they will be retrieved again. To do this, on the "Options" tab, select the settings "Perform final actions on the email server" and "Mark as read."

Groovy Event

When processing an email, you must decide how to handle it based on the specific situation. Processing the email can be easily controlled using the SimpleMailParser in Groovy.

            import de.uplanet.lucy.server.mail.SimpleMailParser

def msg = g_event.message
def path = g_dirWorkflowTmp.toPath()
def smp = SimpleMailParser.newInstance()
def pm = smp.parseMessage(msg, path)

        

The first step is to import the class. A new instance is created and the email is parsed. The "pm" object offers a variety of methods for determining, for example, the subject, body (as HTML or plain text), sender, or attachments of an email. Here are a few examples:

            def strFrom = pm.sender.address //Determine the sender
def strTo = TextUtil.listToString(pm.recipients*.address) //Determine the recipients
def strCC = TextUtil.listToString(pm.recipientsCc*.address) //Determine the recipients in CC
def attachments = pm.getAttachments() //Determine the attachments
def strHTMLContent = pm.getRawHTMLContent() //Determine the content as HTML
def strPlainTextContent = pm.getPlainTextContent() //Determine the content as plain text

        

The information and attachments from the email can then be added to application data sets using INSERT statements.