Protocol basis: use telnet to learn IMAP protocol

Time:2019-12-7

IMAP introduction

IMAPThe full name is Internet Mail Access Protocol, or Interactive Mail Access ProtocolPOP3Similar to one of the mail access standard protocols. The difference is, it’s onIMAPAfter that, the e-mail you received from the e-mail client remains on the server, and the operations on the client will be fed back to the server, such as deleting the e-mail, marking the e-mail as read, etc., and the e-mail on the server will also take corresponding actions. So whether you log in the mailbox from the browser or the client software, you will see the same email and status.

The difference with POP3 can be seen here

Mail protocols includeSMTPPOP3IMAP。 BecauseSMTPandPOP3There are many introductions on these two protocols, including the use of commands andPHPThe use of code can be found, so this article mainly explains how to use commands to operateIMAP

Want to know how to operate with commandsSMTPandPOP3Our friends can move to the following two addresses

  1. Email implementation details (2) — manual experience of SMTP and POP3 protocol
  2. Protocol basis: SMTP: learning SMTP protocol by telnet

IMAP command details

  • Port 143 / 993 (telnet / SSL)

    Port 143 (plain / no SSL) // non SSL
     telnet <IP> 143
     ....
     Port 993 (SSL)             // SSL
     openssl s_client -connect <IP>:993
     CONNECTED(00000003)
     depth=2 /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
     verify error:num=20:unable to get local issuer certificate
     verify return:0
     ---
     Certificate chain
     ...
  • Command format

    Command input:
      < random string ID > command
    Response:
      < random string ID > OK < answer detail >
      
    For example:
      A1 login [email protected] password
      A1 OK login successful

    A1That is< random string ID >, can be any string you define, and the returned text also has this string.

    wrongSSLExample

    $ telnet imap.qq.com 143
     \* OK [CAPABILITY IMAP4 IMAP4rev1 ID AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready 
     A1 LOGIN [email protected] password
     A1 OK Success login ok
     A1 LIST "" "*"
     \* LIST (\NoSelect \HasChildren) "/" "&UXZO1mWHTvZZOQ-"
     \* LIST (\HasNoChildren) "/" "INBOX"
     \* LIST (\HasNoChildren) "/" "Sent Messages"
     \* LIST (\HasNoChildren) "/" "Drafts"
     \* LIST (\HasNoChildren) "/" "Deleted Messages"
     \* LIST (\HasNoChildren) "/" "Junk"
     \* LIST (\HasNoChildren) "/" "&UXZO1mWHTvZZOQ-/QQ&kK5O9ouilgU-"
     \* LIST (\HasNoChildren) "/" "&UXZO1mWHTvZZOQ-/&jURukA-"
     \* LIST (\HasNoChildren) "/" "&UXZO1mWHTvZZOQ-/&kK5O9l9SaGM-"
     \* LIST (\HasNoChildren) "/" "&g0l6Pw-"

    SSLExample

    $ openssl s_client -crlf -connect imap.qq.com:993
      ...
      \* OK [CAPABILITY IMAP4 IMAP4rev1 ID AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready
  • Login authentication
    ID LOGIN <EMAIL> <PASSWORD>

    $ A1 LOGIN [email protected] password
      A1 OK Success login ok
  • List mail folders
    LIST "<mailbox path>" "<search argument>"

    1. <mailbox path>Mailbox path, if ”, all folders of the root directory are listed
    2. <search argument>Is case sensitive and can be“”Or “%”“”Match all, ‘%’ only matches the current layer

      $ A1 LIST "" "*"
  • Select a folder

    $ A1 SELECT INBOX
      \*1254 exists // 1254 messages in total
      \*0 recent // latest message
      \*OK [unseen 75] // unread
      \* OK [UIDVALIDITY 1429146575] UID validity status  
      \* OK [UIDNEXT 2475] Predicted next UID
      \* FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
      \* OK [PERMANENTFLAGS (\* \Answered \Flagged \Deleted \Draft \Seen)] Permanent flags
      A1 OK [READ-WRITE] SELECT complete

It should be noted that:

  1. IMAPCommand usage andPOP3Similarly, the main difference is login authentication.
  2. In usePHPWhen doing mailbox detection, you can use the above methods to achieve.PHPIn the detection part, I have written the code, which can be viewed on GitHub.
  3. You can also get all the messages in the server