How to Install and Configure mailx on Linux for SMTP Email Sending

linux mailxsmtp configurationsend mail linuxssl tls mailxshell email notification
Published·Modified·

The mail command is a tool for sending and receiving emails in the Linux environment. By default, mail calls sendmail to send messages, but sendmail is often flagged as spam. It is recommended to use an SMTP service instead of sendmail for more stable delivery.

SMTP Configuration

Install mailx

The mail command may not be installed by default. You can install it using the following commands:

# CentOS
yum install -y mailx
# Debian or Ubuntu (untested)
apt-get -y install mailx

Configure SMTP

Without any configuration changes, the mail command will use the system's built-in sendmail. To replace this with an SMTP service, edit the configuration file:

# Edit configuration file
vi /etc/mail.rc
# Append the following content to the end and save
set from=service@xiaoz.me
set smtp=smtp.exmail.qq.com
set smtp-auth-user=service@xiaoz.me
set smtp-auth-password=35******3N
set smtp-auth=login
  • set from: Set the sender's email address.
  • set smtp: Set the external SMTP server address.
  • set smtp-auth-user: Set the SMTP username (usually the full email address).
  • set smtp-auth-password: Set the SMTP password.

Test Sending Email

echo "this is my test mail" | mail -s 'mail test' xxx@qq.com
# Or
mail -s 'mail test' xxx@qq.com < test.txt

Test Result

Use SSL/TLS

The configuration above sends email content in plaintext, which poses security risks. Additionally, email providers like Gmail and Zoho require SSL/TLS encryption. To improve security, it is recommended to use SSL or TLS when sending emails.

First, find the local NSS database certificate path by running:

[root@ultravps ~]# find / -name "cert*.db"
/etc/pki/nssdb/cert8.db
/etc/pki/nssdb/cert9.db

Modify the mail configuration file to use SSL encryption:

# Edit configuration file
vi /etc/mail.rc
# Append the following content to the end
set from=service@xiaoz.org
set smtp=smtps://smtp.zoho.com:465
set nss-config-dir=/etc/pki/nssdb/
set ssl-verify=ignore
set smtp-auth-user=service@xiaoz.org
set smtp-auth-password=htH*****T8
set smtp-auth=login
  • set nss-config-dir: Specify the local certificate path.
  • set smtp=smtps://smtp.zoho.com:465: Set the SMTP address and port. Note that smtps indicates SSL encryption is enabled.
  • set ssl-verify: Ignore certificate errors.

If you want to enable TLS encryption, append the following line and set the correct TLS port:

# Set the correct address and port, note that this uses smtp instead of smtps
set smtp=smtp://smtp.zoho.com:587
# Append a line to enable TLS
set smtp-use-starttls=yes

Summary

Configuring the mail command to use an external SMTP email service helps automate task notifications in shell scripts and offers a much higher delivery rate compared to the built-in sendmail.

Some content in this article is referenced from: CentOS 7 Configure mailx to Send Zoho Mail Emails Mailx and Gmail nss config dir