Next: , Previous: Setting Up, Up: Using Networking   [Contents][Index]

2.6 Reading Email

The distribution of email is usually done by dedicated email servers that communicate with your machine using special protocols. To receive email, we will use the Post Office Protocol (POP). Sending can be done with the much older Simple Mail Transfer Protocol (SMTP).

When you type in the following program, replace the emailhost by the name of your local email server. Ask your administrator if the server has a POP service, and then use its name or number in the program below. Now the program is ready to connect to your email server, but it will not succeed in retrieving your mail because it does not yet know your login name or password. Replace them in the program and it shows you the first email the server has in store:

  POPService  = "/inet/tcp/0/emailhost/pop3"
  RS = ORS = "\r\n"
  print "user name"            |& POPService
  POPService                    |& getline
  print "pass password"         |& POPService
  POPService                    |& getline
  print "retr 1"                |& POPService
  POPService                    |& getline
  if ($1 != "+OK") exit
  print "quit"                  |& POPService
  RS = "\r\n\\.\r\n"
  POPService |& getline
  print $0

The record separators RS and ORS are redefined because the protocol (POP) requires CR-LF to separate lines. After identifying yourself to the email service, the command ‘retr 1’ instructs the service to send the first of all your email messages in line. If the service replies with something other than ‘+OK’, the program exits; maybe there is no email. Otherwise, the program first announces that it intends to finish reading email, and then redefines RS in order to read the entire email as multiline input in one record. From the POP RFC, we know that the body of the email always ends with a single line containing a single dot. The program looks for this using ‘RS = "\r\n\\.\r\n"’. When it finds this sequence in the mail message, it quits. You can invoke this program as often as you like; it does not delete the message it reads, but instead leaves it on the server.

Next: , Previous: Setting Up, Up: Using Networking   [Contents][Index]