4D Blog

Home Product Manage IMAP flags

Manage IMAP flags

January 26, 2021

Product

To allow your solution to smoothly integrate with other email clients, you often need to add information such as “already seen” or “urgent”. The IMAP protocol allows you to associate a list of flags with a message to manage this additional information. 4D v18 R6 includes a set of functions for the IMAP Transporter object to add or remove IMAP flags.

With the IMAP protocol you can manage five flags :

  • \Seen: Message has been read
  • \Answered: Message has been answered
  • \Flagged: Message is marked as “flagged” for urgent/special attention
  • \Deleted: Message is marked as “to be deleted”. Note that the actual removal takes effect when the expunge() function is called, when you switch the mailbox, or close the connection. This flag is already added by the delete() function
  • \Draft: Message has not completed composition (marked as a draft).

Add flags

To manage email flags, you need to be connected to your mail server and have a mailbox selected. For the following examples, we’ll use the inbox mailbox:

var $transporter : 4D.IMAPTransporter
$transporter:=IMAP New transporter($serverInfo)
$boxInfo:=$transporter.selectBox("inbox")

To add flags in mails, like in the email object, create a keywords object that contains all of the flags you want to set, and pass them as parameter to the addFlags() function. For example, if you want to mark some emails as draft:

$mails:=$transporter.searchMails("SUBJECT \"4D\"")
$keywords:=New object 
$keywords.$draft:=True 
$transporter.addFlags($mails; $keywords)

Remove flags

In the same way, to remove flags from emails, create a keywords object, and pass it to the removeFlags() function. For example, if you want to remove all of the \Deleted flags in your mailbox to avoid the deletion of the marked emails:

$mails:=$transporter.searchMails("DELETED")
$keywords:=New object 
$keywords.$deleted:=True 
$transporter.removeFlags($mails; $keywords)

Expunge emails

In a previous blog post, we’ve shown you how to add the /Deleted flag to an email with the delete() function.

In addition to the delete() function, we’ve added the new expunge() function to allow you to apply the /Deleted flag without waiting for mailbox switching or connection closing. For example, if you want to remove all the emails in your Junk Email mailbox:

$boxInfo:=$transporter.selectBox("Junk Email")

// marks all the email in the current mail box for deletion
$status:=$transporter.delete(IMAP all)

// deletes all the marked emails
$transporter.expunge()

Check out the documentation to learn more about the new features!

Discuss

Tags IMAP, Mail, v18 R6, v19

Latest related posts

  • February 3, 2026

    4D Write Pro – Adding a margin automatically when bullets are set using standard actions

  • January 22, 2026

    Transform Static Documents into Actionable Knowledge with AIKit

  • January 22, 2026

    Deploy Fluent UI effortlessly in your 4D applications

Fabrice Mainguené
Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • AI
  • 4D Write Pro
  • Email, Microsoft 365, Gmail
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • 4D Qodly Pro
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 21 21 R2 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Google Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 21 21 R2 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Google Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor
Subscribe to 4D Newsletter

© 2026 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy