Does the mail command require a mail server

mail-command

Does one need to set up a mail server before being able to use the linux mail command?

Best Answer

No you do not require a mail server to send mail. I'm most familiar with Sendmail and there are 3 classifications of functionality that fulfill email as a service. MDA (Mail Delivery Agents) is 1, and MTA (Mail Transfer Agents) is 2, and 3 is MUA (Mail User Agents).

The terminology get's confusing but you do not require an MTA to be running all the time. The MTA will be called each time the MUA (mail) wants to "send" mail.

When you run mail and you specify an address to send mail to, sam@example.com. The mail client will summon the MTA (/usr/bin/sendmail) which will then query DNS for that host/domain (example.com), and find out what value is designated for its MX record. MX stands for Mail Exchanger.

Example

You can use the dig command to see this:

$ dig gnu.org mx

; <<>> DiG 9.7.4-P1-RedHat-9.7.4-2.P1.fc14 <<>> gnu.org mx
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21053
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2

;; QUESTION SECTION:
;gnu.org.           IN  MX

;; ANSWER SECTION:
gnu.org.        299 IN  MX  10 eggs.gnu.org.

;; ADDITIONAL SECTION:
eggs.gnu.org.       299 IN  A   208.118.235.92
eggs.gnu.org.       299 IN  AAAA    2001:4830:134:3::10

;; Query time: 218 msec
;; SERVER: 192.168.1.8#53(192.168.1.8)
;; WHEN: Thu Oct  3 17:27:22 2013
;; MSG SIZE  rcvd: 90

So the client will attempt to connect to eggs.gnu.org on port 25 to deliver this email.

DNS server?

@puk asked the following follow-up question:

Is this DNS on my local machine?

To which I replied:

@puk - it can be in the same manner that the mail server can be, but typically it's not. Look in your /etc/resolv.conf file and also when you run the dig command you'll notice the SERVER: ... line at the bottom. That's the DNS server servicing your request.

Example

My /etc/resolv.conf file contains the following:

nameserver 192.168.1.8

And queries such as this one, using dig:

$ dig gnu.org mx

Result in this at the bottom:

;; Query time: 259 msec
;; SERVER: 192.168.1.8#53(192.168.1.8)
;; WHEN: Thu Oct  3 17:46:13 2013
;; MSG SIZE  rcvd: 90

And for the astute reader, one my ask, how is this configured? The answer is the /etc/nsswitch.conf file. Specifically this line:

hosts:      files mdns4_minimal [NOTFOUND=return] dns

That says, use files first (/etc/hosts), followed by mdns4_minimal. That's a multicast DNS. It's basically a cache of previous look ups. Lastly it uses dns which is the IP address of the nameserver designated in the /etc/resolv.conf file.

Related Question