Deleting a mail using IMAP

imap = Net::IMAP.new(‘IP’)
imap.login(‘username’, ‘password’)
imap.select(‘INBOX’)

if not imap.list(”, ‘TRASH’) #Create a TRASH Folder if it doesn’t exist
imap.create(‘TRASH’)
end

imap.copy(message_id, “TRASH”) #copy the message to the TRASH
imap.store(message_id, “+FLAGS”, [:Deleted]) #Mark Deleted in INBOX

imap.expunge #Remove from INBOX

The above moves the mail to the TRASH….
To permanently delete, you can avoid copying the message to trash or mark it as deleted later and expunge!

Advertisements