Skip to main content

Triggers

The ballerina/email connector supports event-driven email processing through IMAP and POP3 listeners. The listeners poll the mail server at configurable intervals and invoke your service callbacks when new emails arrive, errors occur, or the connection closes.

Three components work together:

ComponentRole
email:ImapListenerPolls an IMAP server at a configurable interval and dispatches new emails to the attached service.
email:PopListenerPolls a POP3 server at a configurable interval and dispatches new emails to the attached service.
email:ServiceDefines the onMessage, onError, and onClose callbacks invoked by the listener.
email:MessageThe email message payload passed to the onMessage callback.

For action-based operations, see the Action Reference.


Listener

The email listeners (email:ImapListener and email:PopListener) establish the connection and manage event subscriptions.

Configuration

The listener supports the following connection strategies:

Config TypeDescription
ImapListenerConfigurationConfiguration for the IMAP listener that polls for new emails.
PopListenerConfigurationConfiguration for the POP3 listener that polls for new emails.

ImapListenerConfiguration fields:

FieldTypeDefaultDescription
hoststringRequiredHost address of the IMAP server.
usernamestringRequiredUsername for IMAP authentication.
passwordstringRequiredPassword for IMAP authentication.
pollingIntervaldecimal30Time interval in seconds between email polling attempts.
portint993Port number of the IMAP server.
securitySecuritySSLType of security channel (SSL, START_TLS_AUTO, START_TLS_ALWAYS, or START_TLS_NEVER).
secureSocketSecureSocket()SSL/TLS secure socket configuration.

PopListenerConfiguration fields:

FieldTypeDefaultDescription
hoststringRequiredHost address of the POP3 server.
usernamestringRequiredUsername for POP3 authentication.
passwordstringRequiredPassword for POP3 authentication.
pollingIntervaldecimal30Time interval in seconds between email polling attempts.
portint995Port number of the POP3 server.
securitySecuritySSLType of security channel (SSL, START_TLS_AUTO, START_TLS_ALWAYS, or START_TLS_NEVER).
secureSocketSecureSocket()SSL/TLS secure socket configuration.

Initializing the listener

Using an IMAP listener:

import ballerina/email;

configurable string imapHost = ?;
configurable string username = ?;
configurable string password = ?;

listener email:ImapListener imapListener = check new ({
host: imapHost,
username: username,
password: password,
pollingInterval: 10
});

Using a POP3 listener:

import ballerina/email;

configurable string popHost = ?;
configurable string username = ?;
configurable string password = ?;

listener email:PopListener popListener = check new ({
host: popHost,
username: username,
password: password,
pollingInterval: 10
});

Service

An email:Service is a Ballerina service attached to an email:ImapListener or email:PopListener. It implements callbacks that are invoked when new emails arrive, errors occur, or the listener connection closes.

Callback signatures

FunctionSignatureDescription
onMessageremote function onMessage(email:Message emailMessage)Invoked when a new email is received. This callback is mandatory.
onErrorremote function onError(email:Error emailError)Invoked when an error occurs during email polling. Optional.
onCloseremote function onClose(email:Error? closeError)Invoked when the listener connection is closed. Optional.
note

You must implement the onMessage callback. The onError and onClose callbacks are optional: implement only the ones relevant to your use case.

Full usage example

import ballerina/email;
import ballerina/log;

configurable string imapHost = ?;
configurable string username = ?;
configurable string password = ?;

listener email:ImapListener imapListener = check new ({
host: imapHost,
username: username,
password: password,
pollingInterval: 10
});

service "emailObserver" on imapListener {
remote function onMessage(email:Message emailMessage) {
log:printInfo("New email received",
subject = emailMessage.subject,
'from = emailMessage.'from,
to = emailMessage.to
);
}

remote function onError(email:Error emailError) {
log:printError("Error while polling for emails",
'error = emailError
);
}

remote function onClose(email:Error? closeError) {
log:printInfo("Listener connection closed");
}
}
note

Both email:ImapListener and email:PopListener use the same service callback interface. You can switch between IMAP and POP3 by changing only the listener type and configuration.


Supporting types

Message

FieldTypeDescription
tostring|string[]Recipient email address(es).
subjectstringSubject of the email.
'fromstring?Sender's email address.
bodystring?Plain text body of the email.
htmlBodystring?HTML body of the email.
ccstring|string[]?CC recipient address(es).
bccstring|string[]?BCC recipient address(es).
replyTostring|string[]?Reply-To address(es).
contentTypestring?Content type of the email body (e.g., "text/plain").
headersmap<string>?Custom email headers as key-value pairs.
senderstring?Sender address (may differ from from).
attachmentsmime:Entity|Attachment|(mime:Entity|Attachment)[]?Email attachments as file references or MIME entities.

Attachment

FieldTypeDescription
filePathstringFile path of the attachment.
contentTypestringMIME content type of the attachment (e.g., "application/pdf").