Skip to content

Commit

Permalink
fixed, updated docu
Browse files Browse the repository at this point in the history
Issue #201
  • Loading branch information
rsoika committed Jan 8, 2024
1 parent 2f98d26 commit e1c28ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
16 changes: 10 additions & 6 deletions imixs-archive-importer/MAIL_IMPORTER.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# The Mail IMAP Importer

The CDI Bean *IMAPIImporterService* is a concrete implementation to import documents form a IMAP server. This module provides also services to convert Java Mail Message objects into HTML or PDF.
The CDI Bean `IMAPIImporterService` is a concrete implementation to import documents form a IMAP server. This module provides also services to convert Java Mail Message objects into HTML or PDF.

## The Import Folder

With the 'Selector' attribute you can define the IMAP folder from which the Mail Importer imports mails. If no selector is set the default IMAP Folder 'INBOX' is scanned. This is typically the default folder for most IMAP servers.

## The Archive Folder

After the *IMAPIImporterService* has imported a single message successfully the message will be moved into a IMAP archive folder named 'imixs-archive'. This folder will be automatically created if it does not yet exist on the IMAP server.
You can change the name of the archive folder by setting the option 'archive.folder'
After the `IMAPIImporterService` has imported a single message successfully the message will be moved into a IMAP archive folder named 'imixs-archive'. This folder will be automatically created if it does not yet exist on the IMAP server.
You can change the name of the archive folder by setting the option `archive.folder`

archive.folder=Invoice-Archive


## Filtering Mails by subject using regular expressions

It is possible to filter emails by a subject. Therefor a regular expression can be added by the option property named "subejct.regex" - e.g.:
It is possible to filter emails by a subject. Therefor a regular expression can be added by the option property named `subejct.regex` - e.g.:

filter.subject=(order|offer|invoice*)
subject.regex=(order|offer|invoice*)

In this example only messages with the text 'order', 'offer' or starting with 'invoice' will be imported.

## Detachment Mode

The *IMAPIImporterService* provides the ability to detach files from an mail message object. The behaviour can be configured by the option property named "detach.mode". The following detach modes are supported:
The *IMAPIImporterService* provides the ability to detach files from an mail message object. The behaviour can be configured by the option property named `detach.mode`. The following detach modes are supported:

- PDF - only PDF files attached will be detached together with the origin .eml file to the workitem. This is the default mode.
- ALL - all files will be detached and the email content will be attached as a HTML file to the workitem.
Expand Down Expand Up @@ -72,7 +72,11 @@ For Outlook365 the IMAP Authenticator `org.imixs.archive.importer.mail.IMAPOutlo

**Note:** In case of using the IMAPOutlookAuthenticator the server name is ignored. The Server names to generate a OAuth Token and to open the Message Store are resolved internally by the Authenticator implementation.

## Debug Mode

You can activate a debug mode to get more insides of the import processing by setting the option `debug`

debug=true

## Gotenberg HTML PDF Converter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class IMAPImportService {
*
*/
public void onEvent(@Observes DocumentImportEvent event) {

IMAPFolder inboxFolder = null;
// check if source is already completed
if (event.getResult() == DocumentImportEvent.PROCESSING_COMPLETED) {
logger.finest("...... import source already completed - no processing will be performed.");
Expand All @@ -151,10 +151,8 @@ public void onEvent(@Observes DocumentImportEvent event) {
}

String imapServer = event.getSource().getItemValueString(DocumentImportService.SOURCE_ITEM_SERVER);
String imapFolder = event.getSource().getItemValueString(DocumentImportService.SOURCE_ITEM_SELECTOR);

String importFolderName = event.getSource().getItemValueString(DocumentImportService.SOURCE_ITEM_SELECTOR);
Properties sourceOptions = documentImportService.getOptionsProperties(event.getSource());

Pattern subjectPattern = null;
String subjectRegex = sourceOptions.getProperty(OPTION_SUBJECT_REGEX, "");
boolean debug = Boolean.getBoolean(sourceOptions.getProperty(OPTION_DEBUG, "false"));
Expand All @@ -164,19 +162,15 @@ public void onEvent(@Observes DocumentImportEvent event) {
subjectPattern = Pattern.compile(subjectRegex);
documentImportService.logMessage("...subject.regex = " + subjectRegex, event);
} catch (PatternSyntaxException e) {
documentImportService.logMessage("Invalid IMAP regex filter: " + e.getMessage(), event);
documentImportService.logMessage("Error - invalid subject regex: " + e.getMessage(), event);
return;
}
}

if (imapFolder.isEmpty()) {
imapFolder = "INBOX";
}
try {
Store store = null;
// depending on the option "imap.authenticator" we use the corresponding
// IMAPAuthenticator
// to open the mail store
// IMAPAuthenticator to open the mail store
IMAPAuthenticator imapAuthenticator = null;
String authenticatorClass = sourceOptions.getProperty(OPTION_IMAP_AUTHENTICATOR,
"org.imixs.archive.importer.mail.IMAPBasicAuthenticator");
Expand All @@ -189,22 +183,35 @@ public void onEvent(@Observes DocumentImportEvent event) {
}
}
store = imapAuthenticator.openMessageStore(event.getSource(), sourceOptions);
documentImportService.logMessage("...connected to IMAP server: " + imapServer + " / " + importFolderName,
event);

// first we need to open the INBOX...
inboxFolder = (IMAPFolder) store.getFolder("INBOX");
// next open the Import Folder
IMAPFolder inportFolder = null;
// do we have a custom import folder?
if (!importFolderName.isEmpty()) {
// in case a folder is specified this filder need to be opend from the INBOX
// Default folder!
inportFolder = (IMAPFolder) inboxFolder.getFolder(importFolderName);
} else {
// if not specified it is always the INBOX
inportFolder = inboxFolder;
}

documentImportService.logMessage("...connected to IMAP server: " + imapServer + " /" + imapFolder, event);

IMAPFolder inbox = (IMAPFolder) store.getFolder(imapFolder);
inbox.open(Folder.READ_WRITE);
inportFolder.open(Folder.READ_WRITE);

// Depending on the option 'detach.mode' attachments will be added to the new
// workitem.
String detachOption = sourceOptions.getProperty(OPTION_DETACH_MODE, DETACH_MODE_PDF);
documentImportService.logMessage("...detach.mode = " + detachOption, event);

// open archive folder...
IMAPFolder archiveFolder = openImapArchive(store, inbox, sourceOptions, event);
IMAPFolder archiveFolder = openImapArchive(store, inboxFolder, sourceOptions, event);

// fetches all messages from the INBOX...
Message[] messages = inbox.getMessages();
Message[] messages = inportFolder.getMessages();
documentImportService.logMessage("..." + messages.length + " new messages found", event);

for (Message message : messages) {
Expand Down Expand Up @@ -354,15 +361,14 @@ public void onEvent(@Observes DocumentImportEvent event) {

// move message into the archive-folder
Message[] messageList = { message };
inbox.moveMessages(messageList, archiveFolder);
inportFolder.moveMessages(messageList, archiveFolder);
}

logger.finest("...completed");
} catch (AccessDeniedException | ProcessingErrorException | PluginException | ModelException e) {
documentImportService.logMessage("IMAP import failed: " + e.getMessage(), event);
event.setResult(DocumentImportEvent.PROCESSING_ERROR);
return;

} catch (MessagingException | IOException e) {
documentImportService.logMessage("IMAP import failed: " + e.getMessage(), event);
event.setResult(DocumentImportEvent.PROCESSING_ERROR);
Expand Down

0 comments on commit e1c28ae

Please sign in to comment.