Get number of unread emails for ALL users [solved]

Hello everyone.

I’m trying to come up with an answer so I can get the number of unread emails for every user.

Currently I’ve tried using cURl (recorded the login process with F12 and used the generated cURL command), this works fine, for one user only…

Is there any other way that I can possibly achieve this?

Thanks in advance.

By unread, do you mean when using a client it appears as unread?

I have observed that if a client has not logged in to check mail for an account, the mail will be in /home/user-data/mail/mailboxes/example.com/username/new/. However, if a client has logged in the mail will be moved out of /new/, even though it may be still indicate unread in the mail client.

I also observed that. But only works partly as you mentioned.

But if the user logs in, and does not click/open the email, the mail will still be moved out (even tho the unread count on the webpage does not change, as expected).

Unless someone can chime in that knows more, this is a maildir file format or dovecot thing, and you might try searching on those.

If you find your answer, we’ve had similar questions here in the past so posting your solution here may help others in the future.

Well funny enough I got the solution with that help of maildir word.

With a command like this: ls /home/user-data/mail/mailboxes/*/*/cur/ | egrep '^.+,$'| wc -l
it’s possible to get the exact number of unread emails of every user. Or actually just one user.

The magic is on the name of the email file, explained on this StackOverflow post.

Problem solved :wink:

2 Likes

Interestingly, if I mark an already read message as unread, it won’t show up in the output of that command.

I also have to run as root user. For whatever reason, sudo wasn’t good enough.