2

I am trying to find a way to connect to a mail server and find out how many emails there are in a specific directory. In this specific case, it is the Spam directory in my Gmail account. I am not trying to download the emails in this directory but just count or query the number. I also need to be able to do this from the Terminal.

I remember, a number of years ago, reading a way to do this by using the POP3 protocol to query a specific directory and get a few statistics about that it, including the number of emails. I just tried to find them again but haven't been able to.

1

Connecting to gmail is tricky as google tries to discourage people from using other mail clients than web browsers. First you need to visit google account and check the setting that allows "apps & devices to access your account in a less secure way". Google then will frequently spam your mailbox asking to uncheck this.

Now, to the point. To access gmail via imap (google discourages pop protocol and so do I) you need to use 993 port and add control line feed to openssl option. Spam folder is [Gmail]/Spam and, as I understood, you want to get its next unique ID, so the typical session should look like this:

$ openssl s_client -connect imap.gmail.com:993 -crlf

CONNECTED(00000003)
depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = imap.gmail.com
verify return:1
---

[...]

---
* OK Gimap ready for requests from 10.0.0.1 n9mb133f65154lxl

a1 LOGIN <user> <password>
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584
a1 OK user@gmail.com authenticated (Success)

a2 STATUS "[Gmail]/Spam" (UIDNEXT)
* STATUS "[Gmail]/Spam" (UIDNEXT 2127)
a2 OK Success

a3 LOGOUT
* BYE LOGOUT Requested
a3 OK 73 good day (Success)
read:errno=0

The commands actually typed by you are those started with tags aN. Tags are obligatory by imap protocol, so don't forget about them; they don't need to be in aN form, any string is fine. Obviously change <user> and <password> to yours.

0

You can try it on this way:

telnet mailhost 110 >/tmp/outfile <<EOF
USER username
PASS password
LIST
QUIT
EOF
NMSG=$(tail -3 /tmp/outfile|awk '{print $1;exit}')

And you will get in variable NMSG the number of the messages. I am not sure pop can deal with mail folders. It just report messages in mailbox. In case of SSL/TLS POP3 implementation you can use code like this:

openssl s_client -connect mailhost:995 >/tmp/outfile <<EOF
USER username
PASS password
LIST
QUIT
EOF
NMSG=$(tail -3 /tmp/outfile|awk '{print $1;exit}')

If your mail delivery software mark SPAM messages adding "SPAM" word in subject you can try as last line in script something like:

NMSG=$(grep SPAM /tmp/outfile|wc -l)
0

I managed to find another easier way of doing what I need using Curl and the IMAP protocol:

curl --url "imaps://imap.gmail.com" --user "<email address>":"<password>" -X 'STATUS [Gmail]/Spam (MESSAGES)'

Stdout for this command will be the following (with X being the number of messages present, both read and unread):

* STATUS "[Gmail]/Spam" (MESSAGES X)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.