Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a message over imap using curl

I am working on a project where I need to read messages in an inbox on an imap server, process it and then delete the email from the inbox.

I can successufully get the email without any issue, the problem I am having is the delete.

I can fetch the email using the following:

curl --url "imaps://imap.gmail.com:993/inbox;UID=1" --user "user:password"

This works perfectly fine for getting the email, I successfully process it without issue so now when I try and delete it I use the following:

curl --url "imaps://imap.gmail.com:993/inbox;UID=1" --user "user:password" -X 'UID STORE 1 +Flags \Deleted'

But I then get the following response:

curl: (21) Quote command returned error
curl: (6) Could not resolve host: STORE
curl: (6) Could not resolve host: 1
curl: (6) Could not resolve host: +Flags
curl: (6) Could not resolve host: \Deleted'
like image 586
Boardy Avatar asked Oct 26 '18 18:10

Boardy


People also ask

How do I delete messages from IMAP server?

IMAP accounts provide several options to delete messages that aren't available in POP accounts. Tools -> Account Settings -> Account Name -> Server Settings -> "When I delete a message" has choices for "Move it to the Trash folder", "Mark it as deleted" and "Remove it immediately".

Does IMAP remove messages from server?

Yes, the IMAP sync deleted messages from server. It means, if you delete a message, it will get removed from the server. This article will guide you about IMAP synchronization.


2 Answers

I finally found the answer, it seems like gmail is slightly different to every other example but found an example that works:

The following works:

curl --url "imaps://imap.gmail.com:993/Inbox;UID=1" --user "user:password" -X "STORE 1 +Flags \Deleted"

Followed by

curl --url "imaps://imap.gmail.com:993/Inbox;UID=1" --user "user:password" -X "EXPUNGE"
like image 163
Boardy Avatar answered Oct 19 '22 16:10

Boardy


I wonder whether you should be using double quotes instead of single quotes?

curl --url "imaps://imap.gmail.com:993/inbox;UID=1" --user "user:password" -X 'UID STORE 1 +Flags \Deleted'

Should be:

curl --url "imaps://imap.gmail.com:993/inbox;UID=1" --user "user:password" -X "UID STORE 1 +Flags \Deleted"

It looks like "STORE" etc. are being interpreted by curl as separate arguments that it's trying to treat as URLs.

like image 31
ᴇʟᴇvᴀтᴇ Avatar answered Oct 19 '22 16:10

ᴇʟᴇvᴀтᴇ