The mail command has been used to send mail since Unix. Over the years, the way the command is used and the toolchain has changed a lot. Let’s start at the beginning and look at the history of the Mail command to see where it stands.

The history of mailx

The mail command appeared on the first version of Unix in 1971, when it was used only to send mail to other users on the same computer. In later versions of Unix, mail extended the ability to send remote mail.

Another Mail program, Berkeley Mail, appeared in 1978 on BSD Unix, a major branch of Unix, which also used the Mail command and later replaced the original Unix Mail.

Mailx, a derivative of Berkeley Mail, appeared on Unix versions released in 1987, using the command mailx.

Due to Unix’s long closed source status, Linux users could not use Unix’s Mailx program, so there was an open source version of Nail based on Berkeley Mail, later renamed Heirloom MailX. Heirloom MailX uses the same Mail command as Berkeley Mail and extends many features such as built-in POP3, IMAP, and SMTP support. Heirloom MailX tries to be as consistent with the mailx experience as possible, so the mailx command is also added, but it’s just a symbolic link for Mail.

The mailX packages on previous Linux distributions were mostly packaged with Heirloom MailX.

Updates to Heirloom Mailx later ground to a halt, and s-Nail, an offshoot of Heirloom Mailx, emerged. Currently, various distribution versions have replaced Heirloom Mailx with S-nail. The package name is usually S-nail, and it provides mail and Mailx commands. The user experience is basically the same as Heirloom MailX.

Future V15 versions of S-Nail will make significant changes that are not backward compatible, and S-Nail intends to change its name to S-MailX.

Install mailx

Many Linux distributions come with a package called MailUtils, but this is not the MailX we need. Gnu Mail’s software does not behave the way we describe MailX.

We also didn’t need to install Sendmail because we just submitted mail to an external mail server (like QQ Mail) instead of building our own mail server. S-nail has SMTP built in.

Red Hat is

If CentOS is used, its software package is generally old, still use the mailx package, use the following command to install:

sudo dnf install mailx
Copy the code

If you are using the latest Fedora, use the following command:

sudo dnf install s-nail
Copy the code

If you are not sure which one to install, you can use the following command to see if the software repository has the required packages.

dnf search s-nail
Copy the code

If you don’t have s-NAIL search results, install the MailX package.

Debian system

Install using the following command:

sudo apt install s-nail
Copy the code

S-nail packages in the Debian family may not provide mail and mailx commands, but instead use s-nail commands. The mail command can be created by using the following command.

sudo ln -s s-nail /usr/bin/mail
Copy the code

Arch Linux system

Use the following command to install

sudo pacman -S s-nail
Copy the code

Configuration mailx

Enable the SMTP service of the mailbox

To send emails from third-party clients, the SMTP service needs to be enabled, and authorization codes are required for many mailboxes. If your email address does not support SMTP, you cannot send emails through a third-party client. The following uses QQ mailbox as an example to describe how to enable THE SMTP service and obtain related information.

Go to the Settings page in the upper left corner of the mailbox home page and go to the Accounts TAB.

Find POP3/IMAP/SMTP Settings below, turn on the required service, and click “Generate Authorization code” in the prompt section to obtain the authorization code as required.

Click on the help section, “What is IMAP and how is it set up?” To go to the help page and view the mail server address and supported ports.

Edit the MailX configuration file

It is not recommended to modify the /etc/mail.rc file, you should create your own configuration file in the user directory!!

Create the “.mailrc” file in the user root directory

vim ~/.mailrc
Copy the code

Adding SMTP Configuration

Add the following configuration information to the. Mailrc file, for example, QQ mailbox.

set smtp-auth=login
set smtp-use-starttls
set smtp='smtp://smtp.qq.com:587'
set smtp-auth-user='[email protected]'
set smtp-auth-password='password'
Copy the code
  • set smtp-auth=login: Connects to the SMTP server in login authentication mode
  • set smtp-use-starttls: Enable TLS encryption. Otherwise, emails are submitted in plaintext
  • set smtp='smtp://smtp.qq.com:587': SMTP server and port that provides THE QQ mailbox
  • set smtp-auth-user='[email protected]': Authenticates a user, usually an email account
  • set smtp-auth-password='password': Indicates the authentication password. Generally, it is the email password, but many mail servers use the authorization code

The SMTP server can also be set to

set smtp='smtp.qq.com:587'
Copy the code

But it’s not recommended. If you want to use port 465, or SMTPS, you must specify the protocol in the first notation.

set smtp='smtps://smtp.qq.com:465'
Copy the code

If the SMTP port is omitted, port 25 is used by default. If port 465 is used, TLS encryption is enabled by default. Set smTP-use-starttls is not required.

Port 587 is recommended for mail submission. For information on how to select a port, please read my article: How mail clients select SMTP ports.


S – nail v15 configuration

The V15 version of S-Nail will deprecate the previous SMTP configuration format. A warning is given if the old configuration format is used in V14.9. V14.10 will default to the V15 configuration format.

To use the v15 configuration format in versions prior to V15, you need to add the following statement to the configuration file:

set v15-compat
Copy the code

Authentication mode set smtp-auth=login and encryption mode set smtp-use-starttls are not changed.

V15 replaces SMTP variables with MTA and puts the username, password, and SMTP server in a URL in the following format:

PROTOCOL://[USER[:PASSWORD]@]server[:port]

The following is an example:

set mta='smtp://user:[email protected]:587'
Copy the code

There is no need to add @domail to the user name, just write the first part.

Add sender

If you look closely at an email, you’ll see From, To, Subject, and so on in the header. Now add the From section, the sender, to our message.

Add the following configuration to the. Mailrc file:

set from='user@domain'
Copy the code

In addition to the email address, you can also add the sender’s name:

set from='Name<user@domain>'
Copy the code

What is the difference and relationship between the sender set here and the authenticated user set above?

Authentication user is used to authenticate the identity of the mail server and obtain the right to use the SMTP server. The information you enter is processed by the mail server and will not be sent to the recipient. The sender information from the FROM variable is sent to the recipient, indicating who sent the message, and can be used to reply or return the message on failure. In addition to the email address, you can also set the sender name.

Mail servers generally require that the user and sender information must be the same. After all, you cannot use someone else’s mailbox on the server to send emails. However, if the current user has multiple mailbox aliases, the sender and authenticating user information can be different. For example, QQ mailbox provides a digital mailbox and an English mailbox (the two names point to the same mailbox), then the sender and the authentication user can fill in these two respectively.

Add an alias

You can save contact information by adding an alias, which saves a few keystrokes when filling in recipient information.

You can add an alias as follows:

alias Zhangsan '[email protected]'
alias Lisi '[email protected]'
Copy the code

Note: the email address cannot be written as ‘Name

‘ because alias Zhangsan defaults to the recipient Name. Aliases cannot be used for senders.
@domain>

If the same alias corresponds to multiple email addresses, for example:

alias Zhangsan '[email protected]'
alias Zhangsan '[email protected]'
alias Zhangsan '[email protected]'
Copy the code

In this case, all email addresses corresponding to the alias will be sent at the same time.


The. Mailrc file is as follows:

# ~/.mailrc

set smtp-auth=login
set smtp-use-starttls

# Old format
set smtp='smtp://smtp.domain:587'
set smtp-auth-user='user@domain'
set smtp-auth-password='password'

# New format
#set v15-compat
#set mta='smtp://user:[email protected]:587'

set from='Name<user@domain>'

# Contacts
alias Name 'user@domain'
Copy the code

The use of mailx

Adding recipients

Start by executing the mail command and passing in the recipient parameters.

mail '[email protected]'
Copy the code

Multiple recipients are separated by Spaces (that is, one recipient for each parameter). Recipients can also add names and use aliases from configuration files.

mail '[email protected]' 'Lisi<[email protected]>' Zhangsan
Copy the code

Write E-mail

Mailx reads messages from standard input by default. Enter the mail command on the terminal and enter recipient parameters and press Enter to enter the mail interface. You will be prompted to enter the email subject, and press Enter to skip it. The To line, the recipient, is then automatically generated. Then you can write the text.

After the text is written, press CTRL +D on a new line to finish typing. The message header is automatically generated and asked whether to send the message. Type yes or y to send the email and press Enter to edit it again.

If the email fails to be sent, a dead.letter file containing the email body and date is generated in the user root directory.

[user@host]$ mail [email protected]
Subject: Hello
To: [email protected]
Hello, Zhangsan.
^D
-------
Envelope contains:
From: Lisi <[email protected]>
To: [email protected]
Subject: Hello
Send this message [yes/no, empty: recompose]?
Copy the code

Use pipes or redirects

If you use pipes or input redirection, mail will not go to the interactive page, but will be sent directly.

mail [email protected] < ~/letter
Copy the code
mail [email protected] <<< Hello
Copy the code
echo "$PATH" | mail [email protected]
Copy the code

Add the title

mail -s Hello [email protected]
Copy the code

Add attachments

mail -a ~/file [email protected]
Copy the code

Note: Be sure to place the option before the recipient, otherwise the option will be interpreted as the recipient.


Commonly used options explain
-v Displays detailed information about the sending process
-a Add attachments
-b Add the send
-c Add cc
-s Add the title
-S Function in the configuration filesetThe same
-h See the help

See mail’s MAN manual for more information.

man mail
Copy the code