My 2 paisas

Posts Tagged ‘Telnet

It is sad that I have found this solution after so many days. It is more sad that I never tried to look for this solution earlier. I must have found it atleast 3 months ago. Anyways now that I have found it, let me tell you what exactly I want to do and how the simple script helps me do it.

I have been playing with qmail and qpsmtpd from last 3-4 months. Now to test my set-up, I used to telnet to the port and then used to enter different telnet commands and used to watch the logs simultaneously to get errors.
Now everytime I used to enter the commands manually and that too I couldnt just go to history and execute it as telnet does not support storing commands in history.
Anyway finally today I got fed up of it and I thought let me see if there is a solution so that I dont have to enter commands manually. I didnt expect a solution. But when I searched for it, I found that it was so easy to do it. You just need to execute the following command.
./smtp.sh | telnet

and the contents of smtp.sh is given below:
#!/bin/sh
host=127.0.0.1
port=25
echo open $host $port
sleep 10
echo helo deeproot.co.in
sleep 10
echo mail from:test2@domain
sleep 10
echo rcpt to:test3@domain
sleep 50
echo data
sleep 10
echo checking
echo .
sleep 10
echo quit
#end

You can customize the script to anything that will make your life easier with telnet. I found this solution here, http://www.linuxforums.org/forum/linux-programming-scripting/92466-telnet-script.html

Advertisements
Tags: ,
  • Coutsey to Bob Peers>
  • Sending emails using telnet.

    The basics.

    Before starting it’s important to know a few things:

    SMTP address.

    The SMTP server address of a provider you are permitted to use. Usually this will be you ISP’s SMTP server but could be any other, if you are not sure of the address
    then look at the settings in your email client. Otherwise the E-Eeasy.com site has a long
    list of SMTP servers.

    Port blocking.

    To prevent spamming it’s possible that your ISP blocks the default SMTP port, port 25. If this is the case and you try to connect to a different SMTP server (other
    than your ISP’s smtp server) then you will probably just get a timeout when you try to connect. Some email providers allow you to use an alternate port, in my
    case I use Fastmail.fm and they allow you to use port 26, but you will have to check with your
    account provider first.

    Relaying.

    If you are able to connect to another SMTP that’s not your ISP’s then you will (or should) only be able to send an email to a user of that email provider. For example I can
    connect using telnet to my email provider at Fastmail.fm and send an email to another Fastmail user since they have to accept mail to their users (otherwise they would
    not be able to receive any mail), but if I try to send an email to another email address, say someone@hotmail.com then I will be denied since then I’m using Fastmail.fm’s
    SMTP server as a mail relay which is not allowed without authentication.

    Using telnet.

    If you make a mistake you cannot use backspace to delete the entry, you may have to press enter to get an error and then re-type the command or quit and start again.

    Connecting to the host.

    With this in mind it’s very easy to send a basic text email just using telnet which is installed on most computers. First open up a terminal and type the following, of
    course replacing mail.yourserver.dk with the address of your SMTP server:

    telnet mail.yoursever.dk 25

    This should return something like:

    telnet mail.yoursever.dk 25
    Trying 172.16.0.2...
    Connected to mail.yoursever.dk (172.16.0.2).
    Escape character is '^]'.
    220 cirkeline.yoursever.dk ESMTP Postfix (Debian/GNU)

    HELO command.

    Next we need to introduce ourselves using the helo command, in reality this can be anything and the mail will still be sent OK (although a false helo command
    will result in a spam score from spamassasin for example).

    helo mail.localhost
    250 mail.yoursever.dk

    MAIL FROM command.

    Next we type mail from: followed by your email address, if you use a name as well then you need to put angle brackets around you address,
    like Bob Peers <email@domain.dk>, but the name is not necessary

    mail from:me@domain.dk
    250 Ok

    RCPT TO command.

    Now type rcpt to: followed by who you are sending the mail to, following the same rules as above. Note that this IS NOT the address that appears on the To: line in
    your email client or webmail, the two are completely unrelated which is why you can receive spam when your email address is not listed on the To: line.

    rcpt to:you@domain.com
    250 Ok

    DATA command.

    Now for the actual mail body, type the word ‘data’ and press enter:

    data
    354 End data with <CR><LF>.<CR><LF>

    Your SMTP server might not display the extra help here but it simply means that we type our mail body now and to end the input press enter then type a single .
    followed by pressing enter again. At this point we can add extra headers if we wish but they are not required. This just shows that the To and From fields are just
    part of the message body and have nothing to do with the delivery of the email. However if you do not add them then the message will arrive with
    ‘To: undisclosed-recipients:; in the To field, which again will probably cause your message to be assigned a spam score. If you do decide to add these extra
    headers make sure you leave a blank line between them and the ‘real’ text you wish to appear in the body of the message.

    to:Joe Bloggs<you@domain.com>
    from: Bob Peers<me@domain.dk>
    subject:Telnet test

    Hope you are following along OK...
    .
    250 Ok: queued as 4DDFB180CDA

    To actually send the mail we need to quit the telnet session by typing ‘quit’.

    quit
    221 Bye
    Connection closed by foreign host.

    Summary

    If all goes well you should get the email without problems, that’s basically all there is to it. Many of the extra lines are not really required, as long as you type
    the ‘helo’, ‘mail from:’ and ‘rcpt to:’ commands along with ‘data’, terminating ‘.’ and ‘quit’ the mail should be sent.

    Relay access denied error.

    If you try to send an email to an external address when you are not permitted you will get some output like below (this is using my Fastmail account).

    telnet mail.messagingengine.com 25
    Trying 66.111.4.160...
    Connected to mail.messagingengine.com (66.111.4.160).
    Escape character is '^]'.
    220 frontend2.messagingengine.com ESMTP . No UCE permitted.
    helo mail.localhost
    250 frontend2.messagingengine.com
    mail from:someone@somewhere.dk
    250 Ok
    rcpt to:someoneelse@hotmail.com
    554 <someoneelse@hotmail.com>: Relay access denied
    quit
    221 Bye
    Connection closed by foreign host.
    Tags: ,
  • Coutsey to Bob Peers
  • The basics.

    About POP.

    POP is simply a protocol for retrieving emails from a remote server. The vast majority of mail
    providers use this protocol and it has been around for many years now. The protocol, in it’s
    simplest form, is just a few text commands sent from your computer over the internet which when received by the mail server prompt it to respond with the
    requested information. There’s only 4 or 5 basic commands we need to know to get started so it’s very simple.

    Before starting it’s important to know a few things:

    Mail server address.

    The address of your mail server, this will usually be of the form mail.domain.com. You should look at the settings in your email client or documentation about your
    email account to get this information.

    Security.

    In this demonstration we will be sending our account username and password unencrypted over the internet, if this is a major concern to you then you should not follow
    this exercise.

    Using telnet.

    If you make a mistake in a telnet session you cannot use backspace to delete the entry, you may have to press enter to get an error and then re-type the command or quit and start again.

    Connecting to the host.

    First open up a terminal and type the following, of course replacing mail.yourserver.dk with the address of your POP server:

    telnet mail.myserver.dk 110

    This should return something like:

    telnet mail.myserver.dk 110
    Trying 172.16.0.2...
    Connected to mail.myserver.dk (172.16.0.2).
    Escape character is '^]'.
    +OK Hello there.

    Logging in.

    Next we need to log in using the user command.

    user myusername
    +OK Password required.

    Now give our password using the pass command.

    pass ***********
    +OK logged in.

    The list command.

    To see a list of all the messages on the server and their sizes in bytes use the list command. If you specify a message number then only that message will be shown,
    i.e list 7.

    list
    +OK POP3 clients that break here, they violate STD53.
    1 22683
    2 19870
    3 785
    4 4475
    5 3221
    6 2972
    7 2412

    This just shows that I have 7 emails on the server, they are listed in order from oldest first to newest last.

    Reading the messages using retr.

    To read one of the messages use the retr command followed
    by the message number returned from the list command. It will return the whole message including all headers.

    retr 7
    +OK 2412 octets follow.
    Return-Path: <bounce@domain.com>
    X-Original-To: me@domain.dk
    Delivered-To: me@domain.dk
    Received: from mail-in1.inet.tele.dk (mail-in1.inet.tele.dk [194.182.148.158])
    by cirkeline.ingolf.dk (Postfix) with ESMTP id 554DA180D71
    for <me@domain.dk>; Wed, 1 Feb 2006 10:09:54 +0100 (CET)
    Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [66.111.4.28])
    by mail-in1.inet.tele.dk (Postfix) with ESMTP id 2984C7049
    for <me@domain.dk>; Tue, 31 Jan 2006 18:37:01 +0100 (CET)
    Received: from frontend1.internal (mysql-sessions.internal [10.202.2.149])
    by frontend1.messagingengine.com (Postfix) with ESMTP id 2069DD335A7
    for <me@domain.dk>; Tue, 31 Jan 2006 12:36:18 -0500 (EST)
    Received: from web2.messagingengine.com ([10.202.2.211])
    by frontend1.internal (MEProxy); Tue, 31 Jan 2006 12:36:18 -0500
    Received: by web2.messagingengine.com (Postfix, from userid 99)
    id 1148513964; Tue, 31 Jan 2006 12:36:12 -0500 (EST)
    Content-Disposition: inline
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain
    MIME-Version: 1.0
    X-Mailer: MIME::Lite 5022 (F2.73; T1.15; A1.64; B3.05; Q3.03)
    Date: Tue, 31 Jan 2006 17:36:12 UT
    From: "Email Administrator" <bounce@domain.com>
    Reply-To: "Email Administrator" <webmaster@domain.com>
    To: me@domain.dk
    Subject: Your www.domain.com account activation
    Message-Id: <20060131173612.1148513964@web2.messagingengine.com>

    ..............................

    Reading the top n lines with top.

    The top command is used to read the top n lines of a message, the syntax is ‘top message n’ where n is the number of lines you wish to read. Note
    that the full headers will be returned along with a new line and then the n lines of the message body.

    top 3 5
    Headers appear here.....

    This is a multi-part message in MIME format.

    ------=_NextPart_000_0295_01C5A03F.3E4B4030
    Content-Type: text/plain;
    charset="iso-8859-1"

    The stat command.

    To see the total number and size of all messages issue the stat command.

    stat 
    +OK 7 56418

    Deleting messages using dele.

    To delete a message use dele followed by the message number, note that messages are not immediately deleted, they will only be deleted after quitting the session.

    dele 7
    +OK Deleted.

    The stat command again shows the message is gone.

    stat
    +OK 6 54006

    Resetting the account using rset.

    Since the message is only marked for deletion we can undelete the message (and any other marked for deletion) by using rset (for reset).

    rset
    +OK Resurrected.

    Again, stat shows the message is now back.

    stat
    +OK 7 56418

    To end the session use quit, this will now permanently delete the messages you deleted during this session.

    quit
    +OK Bye-bye.

    Summary

    It’s very simple to read your emails from a POP account using telnet by just remembering a few simple commands, what we have done here is basically exactly
    the same as an email client, like Thunderbird or Outlook Express, does when it retrieves your mail. Of course on a daily basis it’s much nicer to use a web interface
    or email client but in those situations where all you have is an internet connection then this is a great fall-back especially since nearly every computer will
    have telnet already installed.

    Tags: , ,
  • Coutsey to Bob Peers>
  • The basics.

    About IMAP.

    IMAP is an email protocol for organizing, storing and retrieving emails on a remote server. It was
    developed after POP and is a much more advanced system, one of the main differences being that all the mail is stored on the server so it remains accessible from many
    different locations. With POP you have to download the mail to your local computer in order to read it and therefore you cannot synchronize your mail across many
    different machines.It may be more complex than POP but there are still only a few core commands we need to know in order to access our mail on an IMAP server.

    Before starting it’s important to know a few things:

    IMAP command syntax.

    Before the actual command is typed into the terminal we need to type a command tag, this could be anything (without spaces) and the server will tag its response
    with the tag we give it. This seems to be because IMAP allows multiple connections and so multiple commands, by tagging you know which response refers to which command.

    In our case we have only 1 connection and we send single commands so it’s not really relevant, however we need to type something as a tag. I usually just use a period
    ‘.’ but you could use a number or whatever suits you. To demonstrate the command tag see the two server responses here with the tag (don’t worry about the command itself, it
    will be explained soon), in the first one we send ‘. fetch’ and the second one ‘a01a fetch’ getting the same tag back to identify the response:

    . fetch 1 fast
    * 1 FETCH (FLAGS (\Seen hasatt) INTERNALDATE " 1-Feb-2006 08:37:23 -0500" RFC822.SIZE 15013)
    . OK Completed (0.000 sec)

    ao1a fetch 1 fast
    * 1 FETCH (FLAGS (\Seen hasatt) INTERNALDATE " 1-Feb-2006 08:37:23 -0500" RFC822.SIZE 15013)
    a01a OK Completed (0.000 sec)

    Finally, the IMAP commands are not case sensitive, so ‘SELECT inbox’ will work just as well as ‘select INBOX’. For clarity in the code I have typed the commands in
    uppercase and the word INBOX in uppercase also.

    Mail server address.

    The address of your mail server, this will usually be of the form mail.domain.com. You should look at the settings in your email client or documentation about your
    email account to get this information.

    Security.

    In this demonstration we will be sending our account username and password unencrypted over the internet, if this is a major concern to you then you should not follow
    this exercise.

    Another alternative, if your email provider supports SSL, is to use OpenSSL (which most if not all Linux computers will have), see the ‘Connecting to the host’ section
    below for the syntax.

    Using telnet.

    If you make a mistake in a telnet session you cannot use backspace to delete the entry, you may have to press enter to get an error and then re-type the command or quit and start again.

    Connecting to the host.

    Insecure login – login using telnet.

    By insecure I just mean that your username and password are sent unencrypted over the internet so potentially could be intercepted on the route between your computer and the mail server.
    First open up a terminal and type the following, of course replacing mail.myserver.com with the address of your IMAP server, note that the IMAP port used is 143:

    telnet mail.myserver.com 143

    This should return something like:

    telnet mail.myserver.com 143
    Trying 66.111.4.160...
    Connected to mail.myserver.com (66.111.4.160).
    Escape character is '^]'.
    * OK IMAP4 ready

    Secure login – login using OpenSSL.

    To open an SSL session that encrypts all data sent between your computer and the mail server open a teminal and follow these steps, note that we use port 993 here:

    openssl s_client -connect mail.myserver.com:993
    CONNECTED(00000003)
    depth=0 /C=AU/ST=New South Wales/L=Crows Nest/O=Optimal Decisions Group Pty Ltd/CN=mail.messagingengine.com
    verify error:num=20:unable to get local issuer certificate
    verify return:1
    depth=0 /C=AU/ST=New South Wales/L=Crows Nest/O=Optimal Decisions Group Pty Ltd/CN=mail.messagingengine.com
    verify error:num=27:certificate not trusted
    verify return:1
    depth=0 /C=AU/ST=New South Wales/L=Crows Nest/O=Optimal Decisions Group Pty Ltd/CN=mail.messagingengine.com
    verify error:num=21:unable to verify the first certificate
    verify return:1
    ---
    Certificate chain
    0 s:/C=AU/ST=New South Wales/L=Crows Nest/O=Optimal Decisions Group Pty Ltd/CN=mail.messagingengine .com
    i:/C=ZA/ST=Western Cape/L=Cape Town/O=Thawte Consulting cc/OU=Certification Services Division/CN= Thawte Premium Server CA/emailAddress=premium-server@thawte.com
    ---
    Server certificate
    -----BEGIN CERTIFICATE-----
    MIIDeDCCAuGgAwIBAgIDQBYSMA0GCSqGSIb3DQEBBAUAMIHOMQswCQYDVQQGEwJa
    ..........................
    -----END CERTIFICATE-----
    subject=/C=AU/ST=New South Wales/L=Crows Nest/O=Optimal Decisions Group Pty Ltd/CN=mail.messagingeng ine.com
    issuer=/C=ZA/ST=Western Cape/L=Cape Town/O=Thawte Consulting cc/OU=Certification Services Division/C N=Thawte Premium Server CA/emailAddress=premium-server@thawte.com
    ---
    No client certificate CA names sent
    ---
    SSL handshake has read 1054 bytes and written 340 bytes
    ---
    New, TLSv1/SSLv3, Cipher is AES256-SHA
    Server public key is 1024 bit
    SSL-Session:
    Protocol : TLSv1
    Cipher : AES256-SHA
    Session-ID: Session ID
    Session-ID-ctx:
    Master-Key: Key
    Key-Arg : None
    Krb5 Principal: None
    Start Time: 1140271254
    Timeout : 300 (sec)
    Verify return code: 21 (unable to verify the first certificate)
    ---
    * OK IMAP4 ready

    Once this step is carried out the IMAP commands are identical to those for a normal telnet session.

    Logging in.

    Next we need to log in using the login command. Type ‘. login’ followed by your username and password separated by spaces.

    . login accountname@myserver.com *********
    . OK User logged in

    LIST command.

    To see a list of all the mailboxes on the server we use the list command. The arguments “” “*” simply get all the mailboxes including sub folders.

    . list "" "*"
    * LIST (\HasChildren) "." "INBOX"
    * LIST (\HasNoChildren) "." "INBOX.Drafts"
    * LIST (\HasNoChildren) "." "INBOX.Sent Items"
    * LIST (\HasNoChildren) "." "INBOX.Trash"
    * LIST (\HasNoChildren) "." "INBOX.test1"
    * LIST (\HasNoChildren) "." "INBOX.test2"
    . OK Completed (0.460 secs 7 calls)

    We can see from this output how the mailboxes are arranged like a tree with INBOX being the ‘trunk’. My IMAP provider uses a period (.) as a separator between parent and
    child folders so INBOX.Drafts is a child of the INBOX. The \HasChildren simply tells us that this folder has sub folders whereas the other folders do not.
    The way IMAP works means that all folders are created as subfolders of the INBOX even if your email client is configured not to show it that way.

    STATUS command.

    This command return some basic information on the folder without selecting the folder, it takes arguments depending on what information you would like returned.
    Here are 3 example showing total messages, recent messages and unseen messages.

    . status INBOX (messages)
    * STATUS INBOX (MESSAGES 2)
    . OK Completed

    . status INBOX (recent)
    * STATUS INBOX (RECENT 0)
    . OK Completed

    . status INBOX (unseen)
    * STATUS INBOX (UNSEEN 0)
    . OK Completed

    EXAMINE and SELECT commands.

    These two commands basically do the same thing, they return information on the folder chosen and then allow us to access the mails stored inside the folder. The
    main difference is that EXAMINE returns a read-only reference whereas SELECT is read-write.

    . examine INBOX.test2
    * FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
    * OK [PERMANENTFLAGS ()]
    * 0 EXISTS
    * 0 RECENT
    * OK [UIDVALIDITY 1138801117]
    * OK [UIDNEXT 1]
    . OK [READ-ONLY] Completed

    . select INBOX.test2
    * FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
    * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen \*)]
    * 0 EXISTS
    * 0 RECENT
    * OK [UIDVALIDITY 1138801117]
    * OK [UIDNEXT 1]
    . OK [READ-WRITE] Completed

    Note that the only difference in response is the [READ-ONLY] and [READ-WRITE] text. Basically this command just tells us the possible IMAP flags we can set,
    EXIST is how many mails are in the folder, RECENT is how many recent mails there are (the SELECT command will remove the RECENT flag since the folder has now been
    viewed, this is not the same as the \Seen IMAP flag, also note that the EXAMINE command will not reset the RECENT flag).

    The RECENT data is what tells an IMAP email client if you have new mails, by clicking on the folder the client sends the SELECT command and the new mail icon
    disappears even though the mails are still unread.

    CREATE, DELETE and RENAME folders.

    It’s very easy to create and delete folders, just make sure you create them as subfolders of the INBOX. For example to create a top level folder called test3 do
    do the following.

    . create INBOX.test3
    . OK Completed
    . list "" "*"
    * LIST (\HasChildren) "." "INBOX"
    * LIST (\HasNoChildren) "." "INBOX.Drafts"
    * LIST (\HasNoChildren) "." "INBOX.Sent Items"
    * LIST (\HasNoChildren) "." "INBOX.Trash"
    * LIST (\HasNoChildren) "." "INBOX.test1"
    * LIST (\HasNoChildren) "." "INBOX.test2"
    * LIST (\HasNoChildren) "." "INBOX.test3" #we created this
    . OK Completed (0.420 secs 8 calls)

    Conversely we can delete our new folder using the DELETE command. Note that you cannot delete a folder that had subfolders without first deleting the subfolders, also
    deleting a folder containing mails will delete all the mails inside so beware!

    . delete INBOX.test3
    . OK Completed
    . list "" "*"
    * LIST (\HasChildren) "." "INBOX"
    * LIST (\HasNoChildren) "." "INBOX.Drafts"
    * LIST (\HasNoChildren) "." "INBOX.Sent Items"
    * LIST (\HasNoChildren) "." "INBOX.Trash"
    * LIST (\HasNoChildren) "." "INBOX.test1"
    * LIST (\HasNoChildren) "." "INBOX.test2"
    . OK Completed (0.430 secs 7 calls)

    Renaming a folder is just as easy, just type RENAME [current name] [new name]. This will not delete mails as they will just exist in the new folder. Here we rename
    folder test1 to linux.

    . rename INBOX.test1 INBOX.test3
    * OK rename user.accountname.test1 user.accountname.test3
    . OK Completed
    . list "" "*"
    * LIST (\HasChildren) "." "INBOX"
    * LIST (\HasNoChildren) "." "INBOX.Drafts"
    * LIST (\HasNoChildren) "." "INBOX.Sent Items"
    * LIST (\HasNoChildren) "." "INBOX.Trash"
    * LIST (\HasNoChildren) "." "INBOX.linux" #this was test1
    * LIST (\HasNoChildren) "." "INBOX.test2"
    . OK Completed (0.410 secs 7 calls)

    FETCH command.

    This command is the main command we use to actually access our emails. It has many possible options depending in what you wish to see, message flags, email headers,
    text of the body etc. Here we select the INBOX and fetch the emails in a few different ways.

    . select INBOX
    * FLAGS (\Answered \Flagged \Draft \DeleteCLOSE and EXPUNGE commands.d \Seen hasatt)
    * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt \*)]
    * 2 EXISTS
    * 0 RECENT
    * OK [UIDVALIDITY 1138801043]
    * OK [UIDNEXT 3]
    . OK [READ-WRITE] Completed
    . fetch 1 flags
    * 1 FETCH (FLAGS (\Seen hasatt))
    . OK Completed (0.000 sec)

    First we shall fetch the message IMAP flags for all the messages in the folder.

    . fetch 1:2 flags
    * 1 FETCH (FLAGS (\Seen hasatt))
    * 2 FETCH (FLAGS (\Seen hasatt))
    . OK Completed

    Note that with all the commands that act upon messages we can select either 1 message by using the message number as in ‘fetch 1 command’ or we can select a range
    of messages in the format ‘fetch first:last command’ or all the messages ‘fetch 1:last command’. Also note that we can use ‘*’ to indicate all messages so fetch 1:* will get
    all the messages from the first to the last without us knowing how many messages are in the folder.

    First we shall fetch using fast, all and full options (these refer to the headers).

    . fetch 1 fast
    * 1 FETCH (FLAGS (\Seen hasatt) INTERNALDATE " 1-Feb-2006 08:37:23 -0500" RFC822.SIZE 15013)
    . OK Completed (0.000 sec)

    . fetch 1 all
    * 1 FETCH (FLAGS (\Seen hasatt) INTERNALDATE " 1-Feb-2006 08:37:23 -0500" RFC822.SIZE 15013 ENVELOPE ("Wed, 1 Feb 2006 13:37:19 UT" "IMPORTANT: Click here to begin using your account" (("Email Administrator" NIL "bounce" "myserver.com")) (("Email Administrator" NIL "bounce" "myserver.com")) ((NIL NIL "webmaster" "myserver.com")) (("Joe Bloggs" NIL "accountname" "myserver.com")) NIL NIL NIL "<cmu-lmtpd-28871-1138801043-0@server2.messagingengine.com>"))
    . OK Completed (0.000 sec)

    . fetch 1 full
    * 1 FETCH (FLAGS (\Seen hasatt) INTERNALDATE " 1-Feb-2006 08:37:23 -0500" RFC822.SIZE 15013 ENVELOPE ("Wed, 1 Feb 2006 13:37:19 UT" "IMPORTANT: Click here to begin using your account" (("Email Administrator" NIL "bounce" "myserver.com")) (("Email Administrator" NIL "bounce" "myserver.com")) ((NIL NIL "webmaster" "myserver.com")) (("Joe Bloggs" NIL "accountname" "myserver.com")) NIL NIL NIL "<cmu-lmtpd-28871-1138801043-0@server2.messagingengine.com>") BODY ((("TEXT" "PLAIN" NIL NIL NIL "8BIT" 5599 137)("TEXT" "HTML" NIL NIL NIL "8BIT" 7434 141) "ALTERNATIVE")("TEXT" "PLAIN" ("NAME" "This_is_how_attachments_appear.txt") NIL NIL "8BIT" 247 6) "MIXED"))
    . OK Completed (0.000 sec)

    As you can see this returns differing amounts of data about the IMAP flags, size and ENVELOPE information. It’s maybe more informative to use either ‘fetch message body[header]’
    or ‘fetch message rfc822.header’ both of which return the data below.

    . fetch 1 rfc822.header
    * 1 FETCH (RFC822.HEADER {824}
    Return-Path: <nobody@server2.messagingengine.com>
    Received: from web2.internal (web2.internal [10.202.2.211])
    by server2.messagingengine.com (Cyrus v2.3-alpha) with LMTPA;
    Wed, 01 Feb 2006 08:37:23 -0500
    X-Sieve: CMU Sieve 2.3
    X-Attached: This_is_how_attachments_appear.txt
    X-Resolved-to: accountname
    X-Mail-from: nobody
    Content-Transfer-Encoding: 8bit
    Content-Type: multipart/mixed; boundary="_----------=_1138801039165120"
    MIME-Version: 1.0
    X-Mailer: MIME::Lite 5022 (F2.73; T1.15; A1.64; B3.05; Q3.03)
    Date: Wed, 1 Feb 2006 13:37:19 UT
    From: "Email Administrator" <bounce@myserver.com>
    Reply-To: webmaster@myserver.com
    To: "Joe Bloggs" <accountname@myserver.com>
    Subject: IMPORTANT: Click here to begin using your account
    Message-ID: <cmu-lmtpd-28871-1138801043-0@server2.messagingengine.com>

    )
    . OK Completed (0.000 sec)

    To fetch only some headers we can select the header fields we wish to see.

    . fetch 1 (body[header.fields (from to subject date)])
    * 1 FETCH (BODY[HEADER.FIELDS (from to subject date)] {195}
    Date: Wed, 1 Feb 2006 13:37:19 UT
    From: "Email Administrator" <bounce@myserver.com>
    To: "Joe Bloggs" <accountname@myserver.co>
    Subject: IMPORTANT: Click here to begin using your account

    )
    . OK Completed (0.000 sec)

    To read the body of the email message we can use either ‘fetch message body[text]’ or ‘fetch message rfc822.text’ as shown here.

    . fetch 2 rfc822.text
    * 2 FETCH (RFC822.TEXT {11658}
    This is a multi-part message in MIME format.

    --_----------=_1138865560223950
    Content-Disposition: inline
    Content-Length: 5194
    Content-Transfer-Encoding: binary
    Content-Type: text/plain

    more text here.............

    . OK Completed (0.000 sec)

    STORE command.

    This command allows us to add, remove or replace the IMAP flags on the messages. These are flags that denote a message as replied to, deleted, seen etc. and allow
    the message information, as well as the message itself, to be synchronized across different computers. Note that the STORE command causes an automatic FETCH
    command of the message flags so we can see the change immediately. There are 3 ways to use STORE:

    • STORE message +flags [flag list] – this adds the [flag list] flags to the chosen messages.
    • STORE message -flags [flag list] – this removes the [flag list] flags from the chosen messages.
    • STORE message flags [flag list] – resets the flags to [flag list] on the chosen messages (the same as removing all flags and then adding [flag list].

    The list of flags to add include \Answered \Flagged \Draft \Deleted \Seen and many more. All the IMAP flags used as part of the standard installation have the
    backslash in front of them. However some email clients (Thunderbird is one) also allow you to set labels or mark a message as junk, if you add labels do not use
    the backslash. First we shall mark all the messages as deleted.

    . store 1:2 flags \Deleted
    * 1 FETCH (FLAGS (\Recent \Deleted))
    * 2 FETCH (FLAGS (\Recent \Deleted))
    . OK Completed

    Next replace the flags with $label1

    . store 1:* flags $label1
    * FLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt Junk label1)
    * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt Junk $label1 \*)]
    * 1 FETCH (FLAGS ($label1))
    * 2 FETCH (FLAGS ($label1))
    . OK Completed

    Finally we can add the flag NonJunk so that Thunderbird recognises them as not being junk mail.

    . store 1:* +flags NonJunk
    * FLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt NonJunk Junk label1)
    * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt NonJunk Junk $label1 \*)]
    * 1 FETCH (FLAGS ($label1 NonJunk))
    * 2 FETCH (FLAGS ($label1 NonJunk))
    . OK Completed

    Note that the \Deleted flag is used by an IMAP server to mark an email ready for deletion, it is not actually deleted until the server receives either the CLOSE
    or EXPUNGE command shown below.

    CLOSE and EXPUNGE commands.

    Both these commands have the effect of permanently deleting any messages in the current folder marked for deletion with the \Deleted flag. EXPUNGE just deletes the
    messages but does nothing else (this command is the equivalent of compacting folders in Thunderbird), while CLOSE deletes the messages and deselects the current
    folder (you cannot carry out more action on messages until you select a new folder). Assuming the two messages in our INBOX had the \Deleted flag set then the output
    looks like the following.

    . expunge
    * 1 EXPUNGE
    * 1 EXPUNGE
    * 0 EXISTS
    * 0 RECENT
    . OK Completed

    COPY command.

    IMAP has no built in move command, when you move a message you actually copy it to another folder and then delete the original. We can easily copy any number of
    messages using the COPY message [destination] format. Here I copy both messages from the INBOX (that I already have selected) to INBOX.test2 folder, after that I
    select INBOX.test2 to confirm the messages are there. Note that after copying the RECENT flag is reset.

    . copy 1:2 inbox.test2
    . OK [COPYUID 1138801117 1:2 1:2] Completed

    . select inbox.test2
    * FLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt)
    * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen hasatt \*)]
    * 2 EXISTS
    * 2 RECENT
    * OK [UIDVALIDITY 1138801117]
    * OK [UIDNEXT 3]
    . OK [READ-WRITE] Completed

    IDLE command.

    IDLE allows us to constantly monitor a folder so that we will be instantly be notified if a new message arrives in the current folder. This is of little use
    while in a telnet session but I’ll show it here just so you know how it works. First start IDLE on the folder.

    . idle
    + idling

    The server responds with +idling and will stay this way until either a message is received, we stop idling or carry out another command to break the idle. If non
    of these things happen then the connection will eventually time out after a pre-set period depending on your IMAP provider (30 minutes in my case). To stop the IDLE
    command use DONE (note this is the only command WITHOUT the preceding command tag).

    done
    . OK Completed

    LSUB, SUBSCRIBE and UNSUBSCRIBE commands.

    These are more commands that only really apply to email clients since they involve subscribing to folders, however they are shown here for completeness. First LSUB
    works like LIST with the same arguments but returns a list of the currently subscribed folders.

    . lsub "" "*"
    * LSUB (\HasChildren) "." "INBOX"
    * LSUB () "." "INBOX.Drafts"
    * LSUB () "." "INBOX.Sent Items"
    * LSUB () "." "INBOX.Trash"
    * LSUB () "." "INBOX.test2"
    . OK Completed (0.000 secs 6 calls)

    This shows that all folders except INBOX.test3 are currently subscribed, to subscribe a new folder use SUBSCRIBE [foldername].

    . subscribe INBOX.test3
    . OK Completed

    To unsubscribe from this folder use UNSUBSCRIBE [foldername].

    . unsubscribe INBOX.test3
    . OK Completed

    LOGOUT command.

    Of course we need to log out of the server, we do this with the LOGOUT command.

    . logout
    * BYE LOGOUT received
    . OK Completed

    That’s the main commands covered however there are a few more just 3 of which I’ll mention here as they could be useful.

    CAPABILITY, GETQUOTAROOT AND GETACL commands.

    These 3 commands return general information on the server environment and your account information. CAPABILITY returns a long list of the mail servers option most of
    which are not very exciting, the most important one listed is probably IDLE letting you know that your provider supports the IDLE command. The CHILDREN entry we saw
    returned when we did a LIST command (\HasChildren or \HasNoChildren depending on whether a folder has subfolders).

    . capability
    * CAPABILITY IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ MAILBOX-REFERRALS NAMESPACE UIDPLUS ID NO_ATOMIC_RENAME UNSELECT CHILDREN MULTIAPPEND BINARY SORT THREAD=ORDEREDSUBJECT THREAD=REFERENCES ANNOTATEMORE CATENATE IDLE LOGINDISABLED
    . OK Completed

    GETQUOTAROOT return the amount of space you are using and the amount you have available.

    . getquotaroot inbox
    * QUOTAROOT inbox user.accountname
    * QUOTA user.accountname (STORAGE 31306 2048000)

    As you can see I’m using 31Mb but have 2Gb capacity, so plenty to spare! Finally GETACL returns the access control list, basically a list of permission you have on
    your mail folders.

    . getacl inbox
    * ACL inbox accountname lrswipcd admin lrswipcda anyone p
    . OK Completed

    These letters each refer to a different permission, the letters after the user are that users rights, the full list is explained here:

    • l – lookup_flag: mailbox is visible to LIST/LSUB commands
    • r – read_flag: SELECT the mailbox, perform CHECK, FETCH, PARTIAL SEARCH, COPY from mailbox
    • s – seen_flag: keep seen/unseen information across session
    • w – write_flag: STORE flags other than SEEN and DELETED
    • i – insert_flag: perform APPEND, COPY into mailbox
    • p – post_flag: send mail to submission address for mailbox
    • c – create_flag: CREATE new sub-mailboxes in any implementation defined hierarchy
    • d – delete_flag: STORE DELETED flag perform EXPUNGE
    • a – administer_flag: perform SETACL

    Links that I liked to save

    Blog Stats

    • 44,673 hits