There are many different versions of mail
(see Mail vs. mail what is the difference and the Heirloom project's write-up on the different versions of mail
).
If you want to keep your sanity, I recommend avoiding any tool whose name is too close to mail
. Mutt is a lean text mode mail client which is often available, very flexible, and behaves the same everywhere.
mutt -H - "$2" <<EOF
From: $1
To: $2
Subject: $3
Importance: high
$4
EOF
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.
Best Answer
Historically, there have been many incompatible extensions of the original
mail
commnad.Mail
came from BSD, and took the nameMail
rather thanmail
because it was shipped alongside the incompatiblemail
program. Later the same story happened withmailx
. For more details, read the Heirloom project's write-up on the different versions ofmail
. Linux distributions have variously provided one or more of the mail utilities under various names.For portability, even between installations of the same Linux distribution sometimes, you can't rely on
mail
. Themailx
command is standardized (not with all the options that might exist on a particular system).If you want a utility that always behaves in the same way provided it's present, and you don't mind that it's often not installed by default, you can use
mutt
.