Tips & Tricks - Mail connection with IMAP
This workshop demonstrates how emails can be read from an inbox and processed via IMAP. This is done by creating a process with an IMAP event source an IMAP event handler. The email is written to a data group via a Groovy script action.
Please note that emails in HTML can lead to security flaws (by allowing JavaScript to be infiltrated, for example).
Example application
Download the sample application here. Import the download file "tipps-tricks-mailanbindung-mit-imap.zip" and select the contained application and the contained process for the import. Then switch to the "Processes" module, open the "Tips & Tricks - Mail connection with IMAP" process and activate it. The following functionalities are covered by the application:
-
Identify the sender
-
Identify the recipient (including CC)
-
Identify the arrival time
-
Identify the subject
-
Convert email's content from HTML to plaint text
-
Convert email's content from plaint text to HTML
-
Present the email in HTML
-
Present the email in plaint text
-
Identify attachments
-
Identify / Separate inline attachments
-
Add the email to an Intrexx data group
-
Add the attachments to an Intrexx data group
The display of e-mails (HTML / plain text) can be changed in the application on the "Administration" page.
IMAP event source
The IMAP event source is used to establish a connection to the email server in the process. In the properties dialog, all connection parameters can be specified on the "Email server" tab. Information on the folder path can be found here.
For a connection via SSL, a corresponding certificate must be stored in the Intrexx certificate store.
The "Options" tab is used to specify the interval at which the e-mail server is queried and which e-mails are to be retrieved.
Groovy Script Action
Depending on the application case, you need to decide how to process an email. The ""SimpleMailParser"" in Groovy simplifies the email processing.
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. creates a new instance and parses the email. The "pm" object offers a variety of methods to determine, for example, the subject, content (as HTML or plain text), sender or attachments of the mail. Some 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
Subsequently, we can add the information and attachments from the email to Intrexx data groups using INSERT statements.