Manage IMAP flags

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)

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 
$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:

$keywords:=New object 
$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

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

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.