mailx
mailx
is a Linux command line mail client. It is typically used to send mail from shell scripts.
By default, mailx
will use a local MTA (typically sendmail or postfix) to actually transfer emails to a mail server. If your machine does not have sendmail or any other MTAs, you will need to configure mailx
to use an external mail server by setting the smtp-*
options.
Installation
Red Hat / CentOS
On Red Hat based systems, just install the mailx
package.
Debian / Ubuntu / Raspbian
The mailx package is called s-nail on Debian (including Raspberry Pi OS). All the binaries and configuration file locations are also different. Keep in mind the following differences:
- To install, you will need to run
apt install s-nail
. - The main configuration is read from
/etc/s-nail.rc
or$HOME/.mailrc
. - The mailx program will be called
s-nail
.
Alpine Linux
Install the s-nail package with apk add s-nail
.
Usage
The email message is piped to mailx
. Specify -s
for subject. Any options are passed as variables set using -S key=value
. Recipients are specified at the end.
For example, to send a test message using an external SMTP server:
$ echo "Hello there." | mail -s "test" -S smtp=smtp://mail.steamr.com:25 "recipient@steamr.com"
Rather than specifying multiple values with multiple -S
parameters, values can also be defined in /etc/mail.rc
using set key=value
. For example, to use an external mail server with SMTP authentication:
set smtp=smtp://mail.steamr.com:587
set smtp-auth=login
set smtp-auth-user="home@steamr.com"
set smtp-auth-password="mein-passwort"
set smtp-use-starttls
set from="Home Robot <home@steamr.com>"
To verify whether mail is being sent successfully, use -v
for verbose outputs.
root@nas:~# echo "testing" | mail -v -s testing xyz@xyz.xyz
Resolving host mail.steamr.com . . . done.
Connecting to 107.189.11.145:587 . . . connected.
220 mail.steamr.com ESMTP ready
>>> EHLO nas
250-mail.steamr.com
250 STARTTLS
>>> STARTTLS
220 2.0.0 Start TLS
Comparing DNS name: "mail.steamr.com"
>>> EHLO nas
250-mail.steamr.com
250 AUTH PLAIN
>>> AUTH LOGIN
334
>>> Vh
334
>>> VU
235 2.0.0 OK
>>> MAIL FROM:<xyz@xyz.xyz>
250 2.1.0 Ok
>>> RCPT TO:<xyz@xyz.xyz>
250 2.1.5 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as 25661254B6
>>> QUIT
221 2.0.0 Bye
Troubleshooting
Peer's certificate issuer is not recognized
When trying to send an email using starttls via Office365 with this command:
# echo hi | mail -s test -S smtp=smtp://smtp.office365.com:587 \
-S smtp-auth=login \
-S smtp-auth-user="demo@ucalgary.ca" \
-S smtp-auth-password="??" \
-S from="demo <demo@ucalgary.ca>" \
-S smtp-use-starttls\
-S nss-config-dir="/etc/pki/nssdb/" \
"to-me@ucalgary.ca"
I got this error:
Error in certificate: Peer's certificate issuer is not recognized.
The fix is to add the certificate's CA to the system. Do so by obtaining the certificates from the mail server.
# openssl s_client -showcerts -connect smtp.office365.com:587 -starttls smtp
0 s:/C=US/ST=Washington/L=Redmond/O=Microsoft Corporation/CN=outlook.com
i:/C=US/O=DigiCert Inc/CN=DigiCert Cloud Services CA-1
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
1 s:/C=US/O=DigiCert Inc/CN=DigiCert Cloud Services CA-1
i:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert Global Root CA
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
Hit Ctrl-C to quit.
Copy the certificate that signed the mail server (include the BEGIN and END certificate markers). The certificate you're after is typically the last one in the chain. Save the certificate to a separate file and then install the certificate into the NSS database by running the following command. The name should be the certificate's CN=
name.
# certutil -A -t "C,," -n "DigiCert Cloud Services CA-1" -d /etc/pki/nssdb/ -i office365.pem
Verify that the certificate is installed:
# certutil -L -d /etc/pki/nssdb/
mail: unrecognized option: S
When trying to set options with the -S
parameter, you get:
/ # echo hi | mail -v -s test -S smtp=smtp://smtp:25 test@example.com
mail: unrecognized option: S
You need to install s-nail
rather than mailx
.