Copy, move, and delete emails with IMAP

Automatically translated from English

Sometimes you need to save certain emails in dedicated mailboxes and other times you want to delete certain emails to keep up your INBOX tidy. The three new functions we’ve added to the IMAP transporter, copy(), move(), and delete() make this a breeze!

HDI IMAPMailCopy

move or copy emails

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

$transporter:=IMAP New transporter($serverInfo)
$boxInfo:=$transporter.selectBox("inbox")

To move all of the emails whose subject contains “4D new feature:” from your current mailbox (“inbox”) to a mailbox named “New features”, use the move() function:

$mailIds:=$transporter.searchMails("subject \"4D new feature:\"")
// Move emails from selected mailbox to destination mailbox
$status:=$transporter.move($mailIds; "New features")

The mails are no longer displayed in the inbox mailbox but rather, in the New features mailbox. Of course, you can also keep them in both mailboxes with the copy() function:

// Copy emails from selected mailbox to destination mailbox
$status:=$transporter.copy($mailIds; "New features")

Delete emails

If you want to delete all of the emails in your “Junk Email” mailbox, just select this mailbox and use the IMAP all keyword with the delete() function:

$boxInfo:=$transporter.selectBox("Junk Email")
// delete all the email in the current mail box
$status:=$transporter.delete(IMAP all)

Note that the delete() function only flags the message as deleted. It won’t be deleted from the IMAP server until you switch the mailbox or close the connection.

Download the HDI above to see the new functions in action!

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.