Mail basics

Mail Transport Agent (MTA)

A program which transfers an E-mail message from one mailbox to another, usually over a network.

Mail User Agent (MUA)

A program which allows the user to enter a new E-mail message, and read received E-mail

Examples: ...

Most MUAs have only a very simple MTA built in. They can send outgoing mail using SMTP to one remote machine only (a smarthost) which in turn has to take responsibility for relaying it to the remote destination, or returning it if delivery failed. Hence one of the services you need to provide as an ISP is a "Smarthost" or "Outgoing SMTP Relay".


      User
        |
        v
       MUA ---------> MTA --------- - - - - ---------->MTA
                   (smarthost)                     destination

The destination machine must be connected to the Internet 24x7, so that it is always able to receive mail. However MUAs are usually designed for disconnected operation; they expect the final MTA is always connected to the Internet, and collect mail from this using POP3 or IMAP. Hence the other service you need to provide as an ISP is a POP3/IMAP server.

What happens if we want to send a message to user@example.com ?

We have to decide where to send it next.

We can do the same lookups by hand. Example:

Recipient is anybody@nsrc.org

$ nslookup -q=mx nsrc.org.

nsrc.org        preference = 10, mail exchanger = psg.com

(i.e. send mail for anbody@nsrc.org to machine 'psg.com'. Note that this will require a second DNS lookup to get the A record for psg.com, so that we can open an SMTP connection to it.)

Recipient is anybody@pobox.com

$ nslookup -q=mx pobox.com.

pobox.com       preference = 10, mail exchanger = mxa1.pobox.com
pobox.com       preference = 10, mail exchanger = mxa2.pobox.com
pobox.com       preference = 10, mail exchanger = mxa3.pobox.com
pobox.com       preference = 10, mail exchanger = mxa4.pobox.com
pobox.com       preference = 30, mail exchanger = mxb.pobox.com

Recipient is anybody@noc.ws.afnog.org

# nslookup -q=mx noc.ws.afnog.org.

ws.afnog.org
        origin = ns.ws.afnog.org
        mail addr = ops.rent-a-geek.org
        serial = 2001050800
        refresh = 3600 (1H)
        retry   = 1800 (30M)
        expire  = 604800 (1W)
        minimum ttl = 3600 (1H)

(i.e. No MX records found)

# nslookup -q=a noc.ws.afnog.org.

Name:    noc.ws.afnog.org
Address:  213.172.132.193

Now we know where to sending it, try sending it to each of the hosts in turn. If we are using MX records, start with the lowest preference value first.

SMTP

SMTP is a text-based protocol, and you can talk SMTP directly using the keyboard. This is an extremely useful debugging tool. A machine which accepts SMTP mail listens on TCP port 25.

There are only five SMTP commands you need to know:


HELO            -- announce my own hostname (this is usually ignored)
MAIL FROM:<..>  -- return address ("envelope sender")
RCPT TO:<..>    -- recipient address, can be repeated multiple times
DATA            -- start of the actual message. Send the headers, 
                   a blank line, the message, and end with a period on
                   a line of its own
QUIT            -- end the SMTP session

The responses are three-digit numbers (so that they are easily understood by other mail systems), with some optional text displayed for humans to read.

2xx      -- OK
3xx      -- Go ahead
4xx      -- Temporary failure, retry later
5xx      -- Permanent failure

Exercise: delivering mail via SMTP by hand

Now use SMTP to deliver a message to your own machine:

$ telnet pc1.t1.ws.afnog.org 25
Trying 213.172.132.1...
Connected to pc1.t1.ws.afnog.org.
Escape character is '^]'.
220 pc1.t1.ws.afnog.org ESMTP Sendmail 8.11.1/8.11.1; Wed, 9 May 2001 10:43:38 GMT
HELO dontcare
250 pc1.t1.ws.afnog.org Hello pc1.t1.ws.afnog.org [213.172.132.1], pleased to meet you
MAIL FROM:<yourhome@email.address>
250 2.1.0. <yourhome@email.address>... Sender ok
RCPT TO:<user@pc1.t1.ws.afnog.org
250 2.1.5 <user@pc1.t1.ws.afnog.org>... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
Subject: Test message
From: president@whitehouse.gov
To: fred@flintstone.org

This is a test message
.
250 2.0.0 l43AtfS00230 Message accepted for delivery
QUIT
221 2.0.0 pc1.t1.ws.afnog.org closing connection
Connection closed by foreign host.

NOTE: If you need to drop out of a telnet session, hit ctrl and ] then type close

Message envelope

The MAIL FROM and RCPT TO commands together form the message "envelope": that is, the delivery instructions for the message (where to send it to, and where to return it if delivery fails). Notice that these could be completely different from the message headers, including To: and From:

This is often the case for messages which have been distributed via mailing lists. The message headers may say:

From: someone@somewhere.net
To: afnog@afnog.org

But once the mailing list has mailed out separate copies of this message to all the subscribers, these copies will have their own envelope:

MAIL FROM:<owner-afnog@afnog.org>
RCPT TO:<you@yourdomain.com>

The subscriber's address will be in RCPT TO, so they receive it, and usually the envelope sender is changed to the list administrator. If there is a problem with this subscriber's address, the person who needs to know is the list administrator, not the person who originally posted the message.

Standards

SMTP is defined in RFC 2821. The format of an E-mail message is defined in RFC 2822. (The older versions of these documents were RFC 821 and RFC 822, and you still may find people referring to "RFC822 headers")