Get number of emails in specific directory on mail server












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.










share|improve this question





























    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.










    share|improve this question



























      2












      2








      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.










      share|improve this question
















      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.







      email gmail






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 17 at 14:01









      jimmij

      32.1k874108




      32.1k874108










      asked Feb 16 at 18:39









      XJDHDRXJDHDR

      1614




      1614






















          3 Answers
          3






          active

          oldest

          votes


















          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.






          share|improve this answer

































            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)





            share|improve this answer

































              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)





              share|improve this answer























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "106"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501077%2fget-number-of-emails-in-specific-directory-on-mail-server%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                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.






                share|improve this answer






























                  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.






                  share|improve this answer




























                    1












                    1








                    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.






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 17 at 13:54

























                    answered Feb 16 at 20:05









                    jimmijjimmij

                    32.1k874108




                    32.1k874108

























                        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)





                        share|improve this answer






























                          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)





                          share|improve this answer




























                            0












                            0








                            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)





                            share|improve this answer















                            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)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 16 at 19:30

























                            answered Feb 16 at 19:17









                            Romeo NinovRomeo Ninov

                            6,54132028




                            6,54132028























                                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)





                                share|improve this answer




























                                  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)





                                  share|improve this answer


























                                    0












                                    0








                                    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)





                                    share|improve this answer













                                    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)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 2 at 2:47









                                    XJDHDRXJDHDR

                                    1614




                                    1614






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501077%2fget-number-of-emails-in-specific-directory-on-mail-server%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        How to make a Squid Proxy server?

                                        Is this a new Fibonacci Identity?

                                        19世紀