I don't get any errors when I step through using the debugger and if I let it run my logs show it completes but then I open up the web based mail client and the total number of messages has only gone down by whatever I set the initial fetch to.
I was simply going in and deleting the old messages manually but this is an ongoing process and I wanted to add a delete function to the main script so after it has read the recent new messages it can delete some of the older messages and keep the overall mailbox size down to a reasonable level. The other reason I want to have the script clear the old mail is that I got lazy and let it grow until my available mailbox space got too low and now I've got over 20,000 messages to delete, a slow process when each page of the web client only shows 200 max.
In the end I want to keep several months of messages in the mailbox and between 75 and 125 new messages come in daily so each day when the script runs it would be great to have it keeping the mailbox up to date which I suspect will be around 3000 messages.
Here is the current script (after the EmailConnectIMAP script and followed by EmailDisconnect)
Go to Layout [ “EmailSettings” (EmailSettings) ]
Set Variable [ $ViewedMessages; Value:EmailGetMessageCount( "Viewed:True" ) ]
Set Variable [ $MessageBufferCount; Value:EmailSettings::BufferMessageCount ] (set to 3000)
Set Variable [ $MaxFetch; Value:EmailSettings::Max Fetch ] (set to 200)
Set Variable [ $MessagesLeftToDelete; Value:$ViewedMessages - $MessageBufferCount ]
Set Field [ EmailSettings::_result; EmailReadMessages( "progress=" & EmailSettings::Progress Bar; "mailbox=INBOX" ; "Max=" & $MaxFetch; "attachments=false" ; "viewed=true" ; "deleted=false" ) ]
#pulled the following from Max= & EmailSettings::Max Fetch for testing
Commit Records/Requests [ No dialog ]
If [ EmailSettings::_result = "ERROR" ]
Set Field [ EmailSettings::_dialog; "Could not read messages: " & EmailLastError ]
Go to Layout [ “EmailLog” (EmailLog) ]
New Record/Request
Set Field [ EmailLog::Desc; "An error occurred: " & EmailLastError & "¶¶" & "Version: " & EmailVersion ]
Halt Script
End If
#loop through messages pulling in groups of maxfetch
Loop
#Loop over the messages, mark each as deleted
Loop
Exit Loop If [ $$NewMessagesCount > $MaxFetch ]
Set Variable [ $Result; Value:EmailGetNextMessage ]
Set Variable [ $Result; Value:EmailMessageSetFlag("deleted") ]
Set Variable [ $$NewMessagesCount; Value:$$NewMessagesCount + 1 ]
End Loop
Set Variable [ $MessagesDeleted; Value:$MessagesDeleted + $MaxFetch ]
Set Variable [ $MessagesLeftToDelete; Value:$MessagesLeftToDelete - $MaxFetch ]
Exit Loop If [ $MessagesLeftToDelete < 1 ]
Set Field [ EmailSettings::_result; EmailReadMessages( "progress=" & EmailSettings::Progress Bar; "mailbox=INBOX" ; "Max=" & $MaxFetch; "attachments=false" ; "viewed=true" ; "deleted=false" ) ]
End Loop
Set Field [ EmailSettings::Skip; EmailSettings::Skip - $MessagesDeleted ]
Go to Record/Request/Page [ First ]
Commit Records/Requests [ No dialog ]