20

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

puk
  • 333

2 Answers2

17

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.

slm
  • 369,824
9

It really depends on which version of mail you are using.

All versions need an MTA to actually deliver the mail, but some versions are capable of using SMTP to talk to a remote MTA (the "smarthost", e.g. your ISP's mail server) and some can only pipe the message to /usr/sbin/sendmail to send the mail via a local MTA.

In the latter case, the local MTA could be a full-fledged MTA like sendmail or exim or postfix. Or it could be a simple send-only MTA like nullmailer or ssmtp. Or a slightly more sophisticated mini-MTA like msmtp that also supports queuing mail for later delivery.

In the Free Software & Open Source world, there are two main variants of the mailx package (which provides the mail command): bsd-mailx, and heirloom-mailx. The mail command is also available in GNU mailutils.

bsd-mailx does not speak SMTP and can only send mail by piping it to /usr/sbin/sendmail

heirloom-mailx can speak SMTP to a smarthost as well as the traditional 'pipe-to-sendmail' method. BTW, S-nail is probably the best derivative of heirloom-mailx - it is still being actively developed and updated.

GNU mailutils contains several mail-related tools for both end-users and systems administrators, including a version of mail and much more.

So, to answer your question "Do you need a mail server?". Yes, sort of. You need one but, depending on which version of mail you have installed, you may not have to install and configure it on your own system.

IMO, you are better off with at least a minimal MTA like msmtp (or a full-fledged one like postfix or exim) than relying solely on a smarthost with heirloom-mailx because you still need to queue mail for later delivery when your internet connection is down or your smarthost is unreachable. With a local queue, mail and crond and other programs can just send-and-forget any mail. Without a local queue, they have to deal somehow with the error condition if the smarthost is unavailable.

cas
  • 78,579
  • When you type "mail" on a fresh Ubuntu install, it says Command 'mail' not found, but can be installed with: sudo apt install mailutils -- is it better to use bsd-mailx or heirloom-mailx instead of mailutils? – Kevin Wheeler Jul 20 '22 at 13:37
  • No, unless you intend to use it as a command-line mail client then just use GNU mailutils or s-nail which is based on heirloom-mailx (and if want a text-mode mail client, use mutt instead). If you already have an MTA like postfix or exim installed, use its version of /usr/sbin/sendmail for just sending messages - that will give more control over both envelope and header From, and other headers too. If you want to send properly formed complex mime messages from the command-line, use something like mime-construct – cas Jul 21 '22 at 06:56
  • All of these should be packaged for Ubuntu - they are for Debian. – cas Jul 21 '22 at 06:57