Deleting Emails From a Mail Server [IMAP]Last Updated: 07/20/2016 Introduced in Verision: 2.0
Flows can download, list and delete email messages from either an IMAP or POP mail server.
You can configure the Delete component to delete emails that meet certain criteria. A flow can delete all emails sent by one sender, the first or last email in an account, a set number of emails, unread emails, or even all emails for the account on the server.
The Delete component uses the unique id for an email as input, then sends the command to a mail server for the messages that meet the criteria to be deleted.
To delete emails from a mail server, in the Toolbox panel, in the category Communication > IMAP, drag a Delete component to the workspace, and configure it in the Mapping Editor.
Our example flow will delete the first unread email from an existing Gmail account. The email that will be deleted is sent from Ajay Pawar, and has the subject “Test.” The process for deleting many hundreds of messages according to carefully constructed rule sets is not much different from the one in our example.
Begin by navigating to a project folder and clicking the Create Flow button.
The first step will be to add the Get Headers component, which will retrieve the message headers from the inbox of an email account. These headers tell our Delete command which email we want to delete.
Set values in the Mapping Editor to tell the Get Headers component how to access the email server.
Select the Get Headers 1 step in the workspace, and, in the Properties panel, click the Show Mapping Editor link.
To collect our message headers, the Get Headers component must know how to access the mail server. Configure Get Headers the same way that we would configure an email client like Outlook, Thunderbird or Apple Mail. We can also configure Get Headers to retrieve only a specified number of messages or messages that are unread.
Enter information about the email account and server in the Value fields in the Server, Port, Username, and Password sections. Because we only want to delete one email, we enter “1” in the Value field in the Count section.
Also select the Value checkbox in the Secure Connection section, indicating that the flow should access the email server using a secure connection.
Finally, by selecting the Value checkbox in the Unreaded Messages Only section, we set the flow to delete only a message that is unread.
To save these settings and close the Mapping Editor, we click OK.
Once the Get Headers step has retrieved the messages from our Gmail account, our next step is to delete the first unread email message.
Our next step is to configure Delete 1 to communicate with the mail server, just as we did in the Get Headers component.
Enter information about the email account and server in the Value fields in the Server, Port, Username, and Password sections.
Select the Value checkbox in the Secure Connection section, indicating that the flow should access the email server using a secure connection.
One additional field that’s required for the Delete component is the Unique Id. This unique id may refer to one message or many messages, but it is the key which will be used to determine which messages are deleted.
When we expand the outputs available from Headers, we can see a wide variety of options, each of which offers some refinement of the entire collection. Whichever one we map to Unique Id will determine what messages are deleted on our server.
If we were to map the output All Sender to our input Unique Id and then enter “firstname.lastname@example.org” in the Value field, our step would be configured to command the mail server to delete all messages which were sent by “email@example.com.”
If we were to map the output All IsDeleted to our input Unique Id, our step would be configured to command the mail server to delete all messages that have been deleted (i.e. messages that have been moved to the Trash folder).
In this example, we want to delete the first unread message in the email account, so we will expand the input All UniqueId. This input correlates to all messages in all folders.
If we knew the unique identifier for the message we wanted to delete, as it is represented on the mail server, we could map the output All UniqueId directly to our input Unique Id and enter that unique identifier into the Value field, where it might look something like: “Qnrz3VhGe232h2SX092bAdL”.
Because we probably won’t have this information, All UniqueId offers three preset inputs to select the most common range of messages, even when the unique identifier is unknown. First will select the first message in our collection of headers. Last will select the last. Count will select a number of messages from the top of the stack.
Our requirements specify that we will delete the first unread message, so we will map the output Headers > All UniqueId > First to the input Unique Id.
With our mapping completed, we’ll click OK to save the changes and close the Mapping Editor.
Our flow is complete, so we’ll save it and run it in the Debugger.
Because our flow interacts with external systems, we can see the lag time between our flow and Gmail’s servers reflected in real-time in our debugger’s events and on the Diagram tab. This view is by no means a perfect indicator of network health, but if we were to see a significant lag between steps, it might be an indicator of a problem worth exploring. The lag-time we see debugging our flow is perfectly normal, so let’s see if our flow did what we asked of it.
To confirm the flow deleted the first unread email. we’ll access the account. The account contains six unread messages. The first unread message received in the account displays at the bottom of the list with the subject “Test.”
Our email was open when the flow ran, so we click Refresh . . .
. . . and see that the flow deleted the first unread email message. The Inbox now indicates that there are 5 unread emails, and the email with the subject “Test” no longer displays.
By using the Get Headers and Delete components in sequence, our flow has successfully deleted the first unread message from our email account.