Modern users expect email notifications appear instantly when a new email arrives. 4D 21 R3 introduces support for IMAP IDLE notifications, bringing real-time email awareness directly into your applications. Instead of relying solely on periodic synchronization, you can now react instantly when a mailbox changes.
With IMAP IDLE support in IMAPTransporter, your 4D application can subscribe to mailbox notifications. Instead of asking: “Did something change?”, the server tells you: “Something just changed.” And not just new messages. You can react to:
- A new email being created
- A message being deleted
- A flag being modified
- A change in mailbox state
To support mail notifications, we extend the IMAP capabilities of 4D by enhancing the IMAP New transporter command with a new listener parameter. This new listener object allows developers to register callback functions.
Supported Notification Events
The parameter.listener object supports the following callbacks:
- listener.onMailCreated(): Triggered when a new message is detected in the current mailbox.
- listener.onMailDeleted(): Triggered when a message is permanently deleted.
- listener.onFlagsModified(): Triggered when message flags change, for example, when a message is marked as seen.
Start / End Notifications
The 4D.IMAPTransporter object exposes a notifier property with lifecycle control methods:
- notifier.start(): Subscribes to server notifications and activates your IMAP notifier.
- notifier.stop(): Stops the subscription to the server and your IMAP notifier.
For example, you can implement a class that is responsible for managing your email listeners centrally:
// Class IMAPListener
// Triggered when a new email is created on the server
Function onMailCreated($transporter : 4D.IMAPTransporter; $event : Object)
ALERT("You have a new mail!")
// Triggered when an email is deleted
Function onMailDeleted($transporter : 4D.IMAPTransporter; $event : Object)
ALERT("Message deleted")
// Triggered when message flags change (e.g., read/unread)
Function onFlagsModified($transporter : 4D.IMAPTransporter; $event : Object)
ALERT("Flag modified")
And you can start the notifier when it’s needed in your code:
var $parameter:={}
$parameter.authenticationMode:=IMAP authentication OAUTH2 // Using OAuth2 for authentication
$parameter.host:="Outlook.office365.com" // IMAP server host
$parameter.port:=993 // IMAP SSL port
$parameter.accessTokenOAuth2 := $myToken // Token received from the OAuth server
$parameter.user:="myadress@email.com" // User email address
// Listener Intialization
$parameter.listener :=cs.IMAPListener.new()
var $myTransporter:=IMAP New transporter($parameter)
// Start the notifier
$myTransporter.notifier.start()
Conclusion
With IMAP IDLE notifications integrated into IMAP New transporter, 4D applications can adopt a real-time, event-driven approach to email synchronization. Users already expect instant updates from their messaging tools, and this enhancement enables your 4D solutions to deliver the same seamless experience.
Comments are not currently available for this post.