How to use tsNet to delete an e-mail message from an IMAP account

Introduction

There are many other tasks that you may need to perform when working with IMAP accounts.

This lesson explains how you can delete an e-mail from an IMAP folder using the tsNetCustomSync function.

Lay out the Stack

To begin with, let's retrieve a list of all the messages currently in the Inbox.

Create a new stack then drag a button and a scrolling list field onto it.

Set the name of this field to "Message List" and change the name of the button to "Get Message List".

Save this stack.

Create the Script

Edit the script of the button that you placed on the stack by right clicking on the button and selecting "edit script".

Add the following code.

on mouseUp
   -- This is the IMAP server that you want to connect to, use "imaps://" to specify an SSL connection is to be made
   put "imaps://imap.example.com" into tURL
   
   -- Add the IMAP server username and password into the tSettings array to be passed to the tsNet calls
   put "username" into tSettings["username"]
   put "password" into tSettings["password"]
   
   -- Send a command to the IMAP server to tell it we want to perform operations against the INBOX
   put "SELECT INBOX" into tCommand
   put tsNetCustomSync(tURL, tCommand, tHeaders, tRecvHeaders, tResult, tBytes, tSettings) into tData
   
   if tResult is 0 then
      -- Send a command to the IMAP server to tell it to list all messages that are not deleted
      put "SEARCH NOT DELETED" into tCommand
      put tsNetCustomSync(tURL, tCommand, tHeaders, tRecvHeaders, tResult, tBytes, tSettings) into tData
      
      if tResult is 0 then
         -- The request is successful, so tData will now contain a space separate listed of message UIDs
         -- preceded by "* SEARCH"
         
         -- Delete "* SEARCH" from the response
         delete word 1 to 2 of tData
         
         -- Convert the space delimited list of message UIDs to a return delimited list
         replace space with cr in tData
         
         -- Display the list to the user
         put tData into field "Message List"
      else
         -- Otherwise an error has occurred, advise the user
         answer "Error reading message list:" && tResult
      end if
   else
      -- Otherwise an error has occurred, advise the user
      answer "Error connecting to server:" && tResult
   end if
end mouseUp
Click to copy

What this first script is going to do

The code above will look familiar to those who have seen the lesson on reading an e-mail message from an IMAP server,.  This script utilises the tsNetCustomSync function in order to send custom commands to the IMAP server.

In this particular case, we are performing two tasks:

  1. "SELECT INBOX" - This tells the IMAP server that the next command being sent is going to be performed on the user's Inbox.
  2. "SEARCH NOT DELETED" - This command returns a space separated list of message UIDs that are currently in the selected mailbox folder (in this case, the Inbox) that have not been marked for deletion.

Test

Switch to Run mode and click the button.

Now for the next part...

As you can see in the screenshot above, the data returned in the field is a list of message UIDs.  These UIDs are used when requesting the contents of a particular message or interacting with it in any other way (marking it as read, deleting it, etc...).

Now that we have a list of messages and a way for the user to select one, let's delete the message that they select.

Drag another button and a field onto the stack that you have already created with the previous steps.

Set the name of this field to "Message Contents" and change the name of the newly added button to "Delete Message".

Save this stack.

Create the Script

Edit the script of new button that you placed on the stack by right clicking it and selecting "edit script".

Add the following code.

on mouseUp
   -- This is the IMAP server that you want to connect to, use "imaps://" to specify an SSL connection is to be made.
   -- Note that in this lesson, you must include the folder that the message is currently in that you wish to delete
   put "imaps://imap.example.com/INBOX" into tURL
   
   -- Add the IMAP server username and password into the tSettings array to be passed to the tsNet calls
   put "username" into tSettings["username"]
   put "password" into tSettings["password"]
   
   -- Make sure the user has selected a message to view
   if the selectedText of field "Message List" is empty then
      answer "You must select a message to display"
      exit mouseUp
   end if
   
   -- Use tsNetCommandSync to send a command to the IMAP server to copy the message to the "Trash" folder
   put "COPY" && the selectedText of field "Message List" && "Trash" into tCommand
   put tsNetCustomSync(tURL, tCommand, tHeaders, tRecvHeaders, tResult, tBytes, tSettings) into tData
   
   if tResult is 0 then
      -- If the copy is successful, issue another command to flag the message in the INBOX as "read" and "deleted"
      put "STORE" && the selectedText of field "Message List" && "+Flags (\Seen \Deleted)" into tCommand
      put tsNetCustomSync(tURL, tCommand, tHeaders, tRecvHeaders, tResult, tBytes, tSettings) into tData
      
      if tResult is 0 then
         -- Display the message to the user
         put tsNetCustomSync(tURL, "EXPUNGE", tHeaders, tRecvHeaders, tResult, tBytes, tSettings) into tData
         put "Message deleted" into field "Message Contents"
      else
         -- Otherwise an error has occurred, inform the user
         put "Could not expunge message from INBOX" && tResult into field "Message Contents"
      end if
   else
      -- Otherwise an error has occurred, inform the user
      put "Could not copy message to the Trash folder" && tResult into field "Message Contents"
   end if
end mouseUp
Click to copy

Test

Switch to Run mode and click the button.

Summary

This completes the code needed to delete an e-mail from an IMAP account.

As you can see, the entire process is performed by making calls to the tsNetCustomSync command.

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.