gawkinet: Email

1 
1 2.6 Reading Email
1 =================
1 
1 The distribution of email is usually done by dedicated email servers
1 that communicate with your machine using special protocols.  To receive
1 email, we will use the Post Office Protocol (POP). Sending can be done
1 with the much older Simple Mail Transfer Protocol (SMTP).
1 
1    When you type in the following program, replace the EMAILHOST by the
1 name of your local email server.  Ask your administrator if the server
1 has a POP service, and then use its name or number in the program below.
1 Now the program is ready to connect to your email server, but it will
1 not succeed in retrieving your mail because it does not yet know your
1 login name or password.  Replace them in the program and it shows you
1 the first email the server has in store:
1 
1      BEGIN {
1        POPService  = "/inet/tcp/0/EMAILHOST/pop3"
1        RS = ORS = "\r\n"
1        print "user NAME"            |& POPService
1        POPService                    |& getline
1        print "pass PASSWORD"         |& POPService
1        POPService                    |& getline
1        print "retr 1"                |& POPService
1        POPService                    |& getline
1        if ($1 != "+OK") exit
1        print "quit"                  |& POPService
1        RS = "\r\n\\.\r\n"
1        POPService |& getline
1        print $0
1        close(POPService)
1      }
1 
1    The record separators 'RS' and 'ORS' are redefined because the
1 protocol (POP) requires CR-LF to separate lines.  After identifying
1 yourself to the email service, the command 'retr 1' instructs the
1 service to send the first of all your email messages in line.  If the
1 service replies with something other than '+OK', the program exits;
1 maybe there is no email.  Otherwise, the program first announces that it
1 intends to finish reading email, and then redefines 'RS' in order to
1 read the entire email as multiline input in one record.  From the POP
1 RFC, we know that the body of the email always ends with a single line
1 containing a single dot.  The program looks for this using 'RS =
1 "\r\n\\.\r\n"'.  When it finds this sequence in the mail message, it
1 quits.  You can invoke this program as often as you like; it does not
1 delete the message it reads, but instead leaves it on the server.
1