📜 ⬆️ ⬇️

IMAP and POP Gmail via telnet or read mail is easy

I decided to write this article after I needed to write a simple mailer that will serve mail from Gmail. I hope that it became easier for someone to live - even though in general it looks like a manual from Captain Obviousness :) I found all the information needed to write a client in Google in 10 minutes - but I think that a simple manual like this is a good cheat sheet for the future: )

There is nothing difficult in this - the truth! In fact, it’s easy to write your mailer. As easy as reading mail through the console.

Next - two small articles on live examples - how to use POP3 and IMAP without an email client.
')

Read POP3



You need Linux. Well, or OpenSSL support in Windows - if you have one - then probably the article is not for you - you already know everything :)

Next - a set of commands with decoding:

openssl s_client -crlf -ign_eof -connect pop.gmail.com:995

We connect to gmail using SSL. The -crlf parameter ensures that we do not have to experience problems with the transfer of lines and our teams will be recognized as it should.
The -ign_eof parameter promises us that commands starting with the letter R will be correctly perceived by s_client and will not cause an SSL break.

In case of a successful connection, we see something like the following:
CONNECTED (00000003)
... a bunch of letters and numbers indicating SSL ...
+ OK Gpop ready for requests from 79.165.189.32 3pf3718132bwz.16


This means that at the first stage everything went fine and we joined Google's mailer.
Next we need to log in.

USER login WITHOUT gmail .com

In case of successful completion of the team, we see the line:

+ OK send PASS

We are asked to enter a password. What we will do:

PASS your_password


If the password is correct - we are allowed inside!
+ OK Welcome.


Here we are inside our mailbox.
Let's check - did new messages come to us?

STAT


I saw the following:

+ OK 15 1408449


This means that on the server I have 15 unread letters with a total size of 1,408,449 bytes.

Interesting - how much does each unread letter weigh?

Leaf


Here is a list of messages:

+ OK 15 messages (1408449 bytes)
1 3423
2 42610
3 3693
4 3693
5 445122
6 1933
7 3488
8 3760
9 3155
10 439325
11 9071
12 3125
13 3575
14 3997
15 438479


You see - so far nothing complicated :)

Let's read the fifth message - something in it is a lot of bytes - probably an interesting letter!

The TOP command is available for reading the letter.
TOP 5 0


This team, we requested the fifth letter without its text - get only the headers. In the headlines, among other things, it is shown to whom this letter came, from whom and what is the theme of the letter.

...
From: root <bender@skazkin.ru>
To: master@skazkin.ru
Subject: Cron <root @ skazkinserver> perl /home/common/parser.pl queue
...


I received a report from my server - that the parser has finished building the queue. Well, it is not very interesting for me to read - there are different service information.
Mark it for deletion

DELE 5


Gmail understood us -

+ OK marked for deletion


Gmail will delete marked for deletion letters after logging out.

Read the smallest letter -

TOP 6 0


Subject: =? KOI8-R? B? 0NLJ18XU? =
From: =? KOI8-R? B? 4c7Uz84g58HCz9c =? = <Gmail .com friend>
To: Andrew Skazkin <me gmail .com>
Content-Type: text / plain; charset = ISO-8859-1


Wow - can not be read: (
This is already solved by email clients — the subject of the letter — encoded in KOI8-R and encrypted with BASE64.

Well, the mail is read and it's time to go.

I thought so - I will not delete the letter from Cron.

RSET

This command clears the checkboxes for deletion from letters that we have marked for deletion.

+ OK


Such an answer - if successful. Now you can exit.

Quit


Answer us -

+ OK Farewell.
read: errno = 0


That's all. With POP3, we figured it out, right? It's time for IMAP.

Playing with IMAP



The procedure is not much different - only by teams.

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


Again gave us a bunch of letters and numbers, and the last line -

+ OK Gpop ready for requests from 79.165.189.32 1pf4035116fxm.33


Entering your account:

. login i gmail .com my password


Successfully!

* CAPABILITY IMAP4rev1 UNSELECT LITERAL + IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS = DEFLATE
. OK i am gmail .com authenticated (Success)


Please note - all commands are preceded by a period and a space!

Get a list of our folders in your account:

. list "" "*"


We have a list of folders:

* LIST (\ HasNoChildren) "/" "Cron"
* LIST (\ HasNoChildren) "/" "INBOX"
* LIST (\ HasNoChildren) "/" "LJ"
* LIST (\ HasNoChildren) "/" "Proxies"
* LIST (\ HasNoChildren) "/" "WordPress"
* LIST (\ Noselect \ HasChildren) "/" "[Gmail]"
* LIST (\ HasNoChildren) "/" "[Gmail] / & BBIEQQRP- & BD8EPgRHBEIEMA-"
* LIST (\ HasNoChildren) "/" "[Gmail] / & BBoEPgRABDcEOAQ9BDA-"
* LIST (\ HasNoChildren) "/" "[Gmail] / & BB4EQgQ, BEAEMAQyBDsENQQ9BD0ESwQ1-"
* LIST (\ HasNoChildren) "/" "[Gmail] / & BB8EPgQ8BDUERwQ1BD0EPQRLBDU-"
* LIST (\ HasNoChildren) "/" "[Gmail] / & BCEEPwQwBDw-"
* LIST (\ HasNoChildren) "/" "[Gmail] / & BCcENQRABD0EPgQyBDgEOgQ4-"
. Ok success


The fact that with incomprehensible characters - folders, called in Russian.

How many letters do we have in the INBOX folder?

. status INBOX (messages)


The answer is
* STATUS "INBOX" (MESSAGES 8)
. Ok success


Wow! There is a new mail! Honor her:

. select inbox


Sampling successful:
* FLAGS (\ Answered \ Flagged \ Draft \ Deleted \ Seen)
* OK [PERMANENTFLAGS (\ Answered \ Flagged \ Draft \ Deleted \ Seen \ *)]
* OK [UIDVALIDITY 2]
* 8 EXISTS
* 0 RECENT
* OK [UIDNEXT 68967]
. OK [READ-WRITE] inbox selected. (Success)


Let's see - which of the eight letters has not yet been read:

. fetch 1: * flags


This command selects flags for all letters, starting with the first.

* 1 FETCH (FLAGS (\ Seen))
* 2 FETCH (FLAGS (\ Seen))
* 3 FETCH (FLAGS (\ Seen))
* 4 FETCH (FLAGS (\ Seen))
* 5 FETCH (FLAGS (\ Seen))
* 6 FETCH (FLAGS ())
* 7 FETCH (FLAGS ())
* 8 FETCH (FLAGS ())
. Ok success


Read the seventh letter - his headlines

. fetch 7 full


* 7 FETCH (ENVELOPE ("Fri, 20 Nov 2009 09:58:35 +0000" "= = KOI8-R? B? 79TXxdTJ1NggzsEg08 / Pwt3FzsnFICfSwcLP1MXOy8EgzsEgMSDCwcvTJw ==? =" =)). S1c0gzyDQz8nTy8 / X2cgg08nT1MXNwcg =? = "NIL" root "" searchengines.ru ")) ((" =? KOI8-R? B? 5s / S1c0gzyDQz8nTy8 / X2cgg08NT1MXNwcg =), which is used by the program for the application program, which is also used by the group, which is used by the program in the same program, which is used by the program, which is used by the program, and is used by the same program, which is used by the same program, which is used by the program, which is used by a group of applications, which are used by the progams, which are used by the prosecutors, and that they are used by the progr. ("=? KOI8-R? B? 5s / S1c0gzyDQz8nTy8 / X2cgg08nT1MXNwcg =? =" NIL "root" "searchengines.ru") ((NIL NIL "master" "skazkin.ru")) NIL NIL NIL "<200911200935 .b8f41f512510 @ forum.searchengines.ru> ") FLAGS () INTERNALDATE" 20-Nov-2009 09:58:38 +0000 "RFC822.SIZE 4045)
. Ok success


In general, of course it is clear - but I would like to be even clearer

. fetch 7 (body [header.fields (from to subject date)])


We choose only interesting headlines:

* 7 FETCH (BODY [HEADER.FIELDS (from to subject date)] {334}
Date: Fri, 20 Nov 2009 09:58:35 +0000
To: master@skazkin.ru
From: =? Windows-1251? Q? = D4 = EE = F0 = F3 = EC_ = EE_ = EF = EE = E8 = F1 = EA = EE = E2 = FB = F5_ = F1 = E8 = F1 = F2 = E5 = EC = E0 = F5? =
<root@searchengines.ru>
Subject: =? Windows-1251? Q? = CE = F2 = E2 = E5 = F2 = E8 = F2 = FC_ = ED = E0_ = F1 = EE = EE = E1 = F9 = E5 = ED = E8 = E5_ = 27 = F0 = E0 = E1 = EE = F2 = E5 = ED = EA = E0_ = ED = E0_1_ = E1 = E0 = EA = F1 = 27? =


Let's read the body of the letter:

. fetch 7 rfc822.text

Here it is:

* 7 FETCH (RFC822.TEXT {1299}
... the message is in windows-1251 encoding
. Ok success


In general, as you can see, there is nothing difficult to manage with console POP3 and IMAP pins.

The only difference is that IMAP is more sophisticated, and POP3 has a very reduced functionality - and allows you to read only mail that is in the INBOX folder.

Auxiliary information:
Good IMAP manual
IMAP RFC

Source: https://habr.com/ru/post/75875/


All Articles