{"id":4358,"date":"2025-01-12T10:09:41","date_gmt":"2025-01-12T10:09:41","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4358"},"modified":"2025-01-12T10:12:10","modified_gmt":"2025-01-12T10:12:10","slug":"manually-interact-with-imaps-using-telnet-or-openssl","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4358","title":{"rendered":"Manually Interact with IMAP(s) Using Telnet or OpenSSL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes it can be handy to connect into an IMAP(s) server to verify it is working as expected, or to view emails via a command line interface. In this example I&#8217;m connecting to a Microsoft Exchange 2016 Server with IMAP enabled, specifically IMAPS, so running on port 993.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connect and Login<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Firstly we need to connect and login:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>openssl s_client -crlf -connect mail.example.org:993<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once connected, you can login. Notice the &#8220;a&#8221; at the front of the line, this is needed or you&#8217;ll get the following error: <strong>LOGIN BAD Command Error. 12<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a LOGIN username password<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">List Folder Structure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you are connected, you can then view a list folders by using the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a LIST \"\" \"*\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Okay, so in the list we can see our &#8220;Inbox&#8221;, it will appear like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>* LIST \"INBOX\/\" \"*\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can see the folder structure (if there is one) within &#8220;Inbox&#8221; and we can also see that there are 31 messages in the root of the &#8220;Inbox&#8221;. Let&#8217;s have a look into it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a EXAMINE \"INBOX\/\"\na OK LIST completed.\na EXAMINE INBOX\n* 31 EXISTS\n* FLAGS (\\Seen \\Answered \\Flagged \\Deleted \\Draft $MDNSent)\n* OK &#91;PERMANENTFLAGS ()] Permanent flags\n* OK &#91;UNSEEN 23] Is the first unseen message\n* OK &#91;UIDVALIDITY 14] UIDVALIDITY value\n* OK &#91;UIDNEXT 239318] The next unique identifier value\na OK &#91;READ-ONLY] EXAMINE completed.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So we can see that there are 31 emails in the &#8220;Inbox&#8221;. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Select and Fetch Messages<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s SELECT the &#8220;Inbox&#8221; with the below. EXAMINE is the same as SELECT, however the former is read-only, the latter sets the future commands to use this folder as the target.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a SELECT \"INBOX\/\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s fetch the first 5 messages in the &#8220;Inbox&#8221;, getting the subject of these messages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f fetch 1:5 (BODY&#91;HEADER.FIELDS (Subject)])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Nice, so let&#8217;s get more of the header fields, to help us see all the messages, we&#8217;ll retrieve the Subject, Date and From fields, as per the below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f fetch 1:5 (BODY&#91;HEADER.FIELDS (Subject Date From)])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We get an output like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>From: Bob Smith &lt;bob@domain.com>\nSubject: A test message 1\nDate: Fri, 21 Jun 2024 15:04:08 +0100\n\n)\n* 2 FETCH (BODY&#91;HEADER.FIELDS (Subject Date From)] {174}\nFrom: Bob Smith &lt;bob@domain.com>\nDate: Thu, 10 Oct 2024 13:17:51 +0000\nSubject: A test message 2\n &#91;EXT]\n\n)\n* 3 FETCH (BODY&#91;HEADER.FIELDS (Subject Date From)] {121}\nFrom: Bob Smith &lt;bob@domain.com>\nSubject: A test message 3\nDate: Wed, 16 Oct 2024 14:56:35 +0000\n\n)\n* 4 FETCH (BODY&#91;HEADER.FIELDS (Subject Date From)] {167}\nFrom: Bob Smith &lt;bob@domain.com>\nSubject: A test message 4\nDate: Mon, 11 Nov 2024 11:02:27 +0000\n\n)\n* 5 FETCH (BODY&#91;HEADER.FIELDS (Subject Date From)] {136}\nFrom: Bob Smith &lt;bob@domain.com>\nSubject: A test message 5\nDate: Mon, 18 Nov 2024 15:33:08 +0000\n\n)\nf OK FETCH completed.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll notice that the messages are showing up in reverse date order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One problem with using FETCH is that it marks the messages as &#8220;Seen&#8221; i.e. read. If we don&#8217;t want to do this, we can use .PEEK, such as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f fetch 1:5 (BODY.PEEK&#91;HEADER.FIELDS (Subject Date From)])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Read a Message<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So finally, let&#8217;s open and read one of the messages, there are few bits we can do here. One thing to note is we&#8217;re reading in plain text, there is no formatting (for HTML) so we&#8217;ll see lots of seemingly superfluous information, unless of course everyone who sends you email does so in plain text! We&#8217;ll use the &#8220;.PEEK&#8221; on the end, so it doesn&#8217;t mark the message as read.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a FETCH 1 BODY.PEEK&#91;TEXT]\na FETCH 1 BODY.PEEK&#91;HEADER]\na FETCH 1 BODY.PEEK&#91;]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Close\/Logout Session<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you are done:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a LOGOUT<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it can be handy to connect into an IMAP(s) server to verify it is working as expected, or to view emails via a command line interface. In this example I&#8217;m connecting to a Microsoft Exchange 2016 Server with IMAP enabled, specifically IMAPS, so running on port 993. Connect and Login Firstly we need to &#8230; <a title=\"Manually Interact with IMAP(s) Using Telnet or OpenSSL\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4358\" aria-label=\"Read more about Manually Interact with IMAP(s) Using Telnet or OpenSSL\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4361,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,20],"tags":[],"class_list":["post-4358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-exchange","category-random"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4358"}],"version-history":[{"count":4,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4358\/revisions"}],"predecessor-version":[{"id":4364,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4358\/revisions\/4364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4361"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}