Mail gateway (Mail Processing)
Mail gateway is the system component responsible for receiving, sending, and processing emails related to tickets and process execution.
Mail gateway functions
- Managing the inbound and outbound mail queue (SenderMessage)
- Sending notifications about events
- Sending outbound mail from the message queue
- Loading inbound mail into the message queue
- Storing inbound message processing rules (MailRule)
- Processing inbound messages according to configured rules (MailProcessing)
Mail channels
Mail channels are configured by the system administrator in the Administration (Admin) section.
The channel direction is determined by the specified protocol:
- SMTP — outbound message channel;
- IMAP — inbound message channel.
Attributes
- Name (Name) -
String- the channel name displayed in the system. Required attribute. - Protocol (Proto) -
ChannelProto- mail protocol. Possible values:- SMTP - outbound message channel;
- IMAP - inbound message channel.
- Encryption (Encryption) -
ChannelEncryption- encryption. Possible values:- NONE;
- SSL;
- TLS.
- Authentication type (Auth type) —
ChannelAuthType— authentication type. Possible values:- NONE — no authentication;
- LOGIN — authentication with username and password;
- XOAUTH2 — authentication using an access token from the specified OAuth2 configuration.
- OAuth2 configuration (OAuth entry name) —
String— OAuth2 configuration name. Required for the XOAUTH2 authentication type. - Host (Host) —
String— mail server hostname or IP address. - Port (Port) —
Integer— server port. Filled in automatically or can be specified manually. Automatic values based on the Protocol and Encryption fields:- SMTP, NONE = 25;
- SMTP, SSL = 465;
- SMTP, TLS = 587;
- IMAP, NONE = 143;
- IMAP, SSL = 993;
- IMAP, TLS = 143.
- Sender address (Address) —
String— the email address substituted into the From: header. Specified only for the SMTP protocol (outbound message channel). - Username (Username) —
String— username. Required for LOGIN and XOAUTH2. - Password (Password) —
String— password. Specified for the LOGIN authentication type. - Space (Workflow) —
String— space (Workflow) identifier. Selected from the list of spaces. Required for the IMAP protocol. - IMAP folder (IMAP Folder) —
String— IMAP protocol only. IMAP folder name. Required attribute. Default value: INBOX. - Main (Main) —
Boolean— SMTP protocol only. A successfully registered main channel is used to send notifications and messages where the channel is not specified. - Delete received (Delete received) —
Boolean— IMAP protocol only. Indicates that after a message is received it must be deleted from the server. - Debug (Debug) —
Boolean— enables debug output for the channel in the system logs. - Active (Active) —
Boolean— allows the channel to be registered in the application memory. - Registered (Registered) —
Boolean— a server-computed flag indicating successful channel registration in the current runtime state. The field is read-only, is not accepted from API clients, and is not stored in the database.
Key principles
Channel registration (Channel Service)
- On startup and on every update, ChannelService loads channel settings but registers only active channels (
active = true) that pass validation successfully. - For LOGIN, a username and password are required. For XOAUTH2, a username and an existing OAuth2 configuration (
oauthEntryName) are required. - OAuth2 configurations are loaded only if at least one active XOAUTH2 channel exists. An OAuth2 load error does not prevent registration of channels with LOGIN and NONE authentication types; XOAUTH2 channels without an available configuration are not registered.
- A new set of senders, receivers, channels, and OAuth2 configurations is first assembled separately and then atomically replaces the current runtime state. Therefore services never see a partially updated set of channels.
- If the initial runtime state cannot be assembled at startup, initialization fails. If a subsequent background refresh fails, the previous working set continues to be used.
- Setting changes are applied to the runtime state on the next background refresh at the interval specified by
sender.channel-init-delay-in-millis(by default — every minute), or after an application restart. - In the channel API list, the
registeredfield shows the actual result of the current registration.active = trueallows a registration attempt but does not guaranteeregistered = true.
Creating an outbound message (Sender Message Factory)
- System components and external services create messages for sending in the message queue (SenderMessage) (attribute direction = OUTBOX).
- If the message specifies
channelName, the corresponding registered channel is used. IfchannelNameis not specified, the registered channel withmain = trueis selected. If there is no registered main channel, creating or sending the message fails. - The Sender (From) field is filled using the template:
senderMessage.sender <channel@address>, where:- senderMessage.sender - the sender name specified in the message;
- channel@address - the email specified in the selected channel settings.
- If senderMessage.sender is empty, the Sender field is set to the value from channel@address.
- The number of send attempts (retries attribute) is set from the configuration parameter
sender.retries(default — 3).
Sending an outbound message (Sender Service)
- The message sending service (SenderService) checks the message queue (SenderMessage) at the interval specified by the configuration parameter
sender.send-delay-in-millis(by default — every minute). - Messages with the following attribute values are selected:
retries > 0andcompleted = false. - If sending succeeds, the system sets the attribute values:
retries = 0,completed = true. - If sending fails, the system decreases the
retriesattribute by 1 and stores the error description in theexceptionfield.
Receiving inbound messages (Receiver Service)
- The message receiving service (ReceiverService) obtains the list of registered inbound message channels at the interval specified by the configuration parameter
sender.receive-delay-in-millis(by default — every minute). - For each channel, the service connects to the IMAP server and retrieves messages.
- Retrieved messages are stored in the message queue (SenderMessage) with the following attributes:
- direction = INBOX;
- completed = false;
- raw = the received message in raw format.
- If the channel has the attribute deleteReceived = true, the message is deleted. Otherwise the message is marked as read.
Processing inbound messages (Mail Processing)
After an inbound message is received and placed in the queue (SenderMessage), the system processes it using the following algorithm:
- Selecting messages for processing
- The inbound mail processing service (
MailProcessingService) periodically (the interval is set bysender.receive-delay-in-millis, default — 1 minute) selects all messages from the queue:- with direction
INBOX; - with
retries > 0; - with
completed = false.
- with direction
Processing a message
For each selected message, the
processMessage()method is executed:
Checking for a ticket number in the email subject
If the email subject contains a ticket number (in the format
[Ticket#XXXX]):- the ticket is looked up by number;
- if the ticket is found — a comment is created with the email content and attachments;
- processing completes.
Checking a link to an existing comment
If the email headers contain
In-Reply-ToorReferencesthat point to a commentSenderMessageId:- the system finds the comment and the related ticket;
- a comment is created with the email content and attachments;
- processing completes.
Creating a new ticket
If neither a ticket by number nor a link to a comment is found:
mail processing rules (
MailRule) are selected;active rules are sorted by descending priority (
priority);for each category (
OutputType):WORKFLOW— the space (Workflow) is determined;TICKET_TYPE— the ticket type;SERVICE_ENTITY— the service;- the values are assigned to the new ticket.
a new ticket is created:
- title — email subject (
subject); - content — email body (HTML or plain text);
- attachments are added;
- the sender is determined — if it matches a person or organization, the corresponding fields are filled in.
- title — email subject (
a comment of type
INBOUNDis created with the email content and attachments.
- Processing result
On successful processing:
- the message is marked:
completed = true,retries = 0.
- the message is marked:
On a processing error:
- the
retriesvalue is decreased by 1; - the error description is stored in the
exceptionfield; - the message remains
completed = false.
- the
- Retries
- Reprocessing is performed on subsequent task runs;
- the message continues to be processed while
retries > 0; - the number of attempts is set by the
sender.retriesparameter (default = 3).
Rule matching (Mail Rule)
The rule catalog (Mail Rule) is used for automatic processing of inbound messages. Each rule defines how to process an email depending on its fields (addresses or subject).
Rule attributes:
Rule type (RuleType) — the email field the rule is applied to:
TO— the To field;FROM— the From field;DELIVERED-TO— the Delivered-To field;CC— the Cc field;SUBJECT— the email subject.
Rule value (RuleValue) — a regular expression (regexp) applied to the value of the selected field.
Output type (OutputType) — the type of object the processing result refers to:
WORKFLOW— space (Workflow);SERVICE_ENTITY— service;TICKET_TYPE— ticket type.
Output value (OutputValue) — the object identifier (UUID) that the new ticket will be bound to.
Priority (Priority) — rule priority (the higher the value, the earlier it is applied).
Active (Active) — rule activity flag.
Rule application algorithm:
- All active rules are selected from the catalog;
- For each rule,
matchesRule()is executed:
- the regular expression is applied to the selected email field;
- if a match is found — the rule is considered triggered.
- If several rules are triggered:
- For each OutputType (
WORKFLOW,TICKET_TYPE,SERVICE_ENTITY), the rule with the highest priority (priority) is selected.
- The applied rules control the fields of the ticket being created.
Rule usage examples:
| RuleType | RuleValue | OutputType | OutputValue (example) | Priority |
|---|---|---|---|---|
| TO | support@reunico.com | WORKFLOW | 4e90609b-7b62-45a4-9e61-2e06b5322fc4 | 999 |
| FROM | mmartynyuk@reunico.com | SERVICE_ENTITY | 8b73b058-8940-4eee-a3ad-bc18b9d341c5 | 99 |
| SUBJECT | .Incident. | TICKET_TYPE | 2f7adbdc-2db2-4e22-8fcb-65fffd885877 | 500 |
Priority:
- The higher the
priority, the earlier the rule is applied for that OutputType; - If several rules of the same type are triggered — the rule with the highest priority is selected.
Message queue (SenderMessage)
The message queue is an internal mechanism for storing and processing inbound and outbound emails in the system.
All email messages (inbound and outbound) are written to the message queue (SenderMessage table).
The queue is used for:
- Asynchronous sending of outbound emails;
- Receiving and storing inbound emails;
- Resending on errors;
- Storing message processing history;
- Linking messages to comments, tickets, and events.
Attributes
| Attribute | Data type | Description |
|---|---|---|
| id | UUID | Unique message identifier |
| subject | String | Email subject |
| body | String | Email body (HTML or plain text) |
| raw | String (TEXT) | Raw source email data (MIME) — inbound messages only |
| recipients | Set<String> | Email recipients (To:) |
| copy | Set<String> | Copy recipients (Cc:) |
| retries | Integer | Remaining send attempts (default — 3) |
| exception | String | Last error text (on a failed send) |
| direction | SenderMessageDirection | Message direction:OUTBOX — outboundINBOX — inbound |
| channelName | String | Name of the channel the message was sent or received through |
| sender | String | Sender address (From:) |
| completed | Boolean | Message processing flag:true — the message has been processedfalse — waiting for processing or a retry |
| messageId | String | RFC 5322 Message-ID (unique email identifier in the email system) |
| inReplyTo | String | RFC 5322 In-Reply-To (used to link to previous emails/comments) |
| attachments | Set<UUID> | Identifiers of attachments (Attachment) associated with this message |
Message lifecycle
| Stage | Description |
|---|---|
| Creating an outbound message | Written to the queue with direction = OUTBOX, completed = false |
| Send attempt | Checked by SenderService. On success → completed = true, retries = 0 |
| Send error | On error, retries is decreased and exception is recorded. When retries = 0 the message is considered unsent |
| Receiving an inbound message | Written to the queue with direction = INBOX, completed = false |
| Processing an inbound message | On successful processing by MailProcessingService → completed = true |
| Retrying inbound processing | On a processing error → retries is decreased, exception is stored |
Notes
- All inbound emails are stored in
rawform (full MIME) so they can be reprocessed. - The
attachmentsattribute contains references to files (Attachment) uploaded with the email or attached to the outbound message. - The link to comments is made by the
messageId↔senderMessageIdfield in the comment.