Linux: mailx command (en)

From OnnoWiki
Jump to navigation Jump to search

Send emails from the command line

The mail command is essential and should be available on any Linux server so that various services and other web applications can generate and transmit emails.

In a previous post on the mail command, we saw how the mail command can be used to send emails from the command line on your Linux server.

In this tutorial, we will be using an enhanced version of the mail command called mailx (or just mail when installed), which can do many more things than the older mail command from the GNU mailutils package. How it works

The mail/mailx command requires a local SMTP server (MTA) running to deliver the emails. The email route is somewhat like this:

mail -> sendmail -> local MTA -> recipient MTA [Inbox]

The recipient MTA would be Gmail's SMTP server if your recipient is someone at gmail.com, for example. For the local MTA, you need to install an SMTP server like Postfix. A basic installation of Postfix with minimal configuration would work in most cases.

Install the mailx command

On Ubuntu/Debian based systems, the mailx command is available from two different packages:

  1. heirloom-mailx
  2. bsd-mailx

We shall be using the heirloom-mailx package because it has more features and options. On CentOS/Fedora based systems, there is only one package named "mailx," which is the heirloom package.

To find out which mailx package is installed on your system, check the "man mailx" output and scroll down to the end, and you should see some useful information.

# ubuntu/debian
$ sudo apt-get install heirloom-mailx

# fedora/centos
$ sudo yum install mailx

Using the mailx command

Once installed, the mailx command can be directly referenced with the name mail, so you just type that in the command line.

1. Simple mail

Run the following command, and then mailx would wait for you to enter the email message. You can hit enter for new lines. When done typing the message, press Ctrl+D, and mailx would display EOT. After that, mailx automatically delivers the email to the destination.

$ mail -s "This is the subject" someone@example.com
Hi someone,
How are you?
I am fine.
Bye
EOT

2. Take message from a file

The message body of the email can also be taken from a file:

$ mail -s "This is the Subject" someone@example.com < /path/to/file

The message can also be piped using the echo command:

$ echo "This is the message body" | mail -s "This is the Subject" someone@example.com

3. Multiple recipients

To send the mail to multiple recipients, specify all the emails separated by a comma:

$ echo "This is the message body" | mail -s "This is the Subject" 
someone@example.com,someone2@example.com

4. CC and BCC

The "-c" and "-b" options can be used to add CC and BCC addresses respectively:

$ echo "This is the message body" | mail -s "This is the Subject" -c ccuser@example.com someone@example.com

5. Specify From name and address

To specify a "FROM" name and address, use the "-r" option. The name should be followed by the address wrapped in "<>":

$ echo "This is the message body" | mail -s "This is the Subject" -r 
"Harry<harry@gmail.com>" someone@example.com

6. Specify "Reply-To" address

The reply-to address is set with the internal option variable "replyto" using the "-S" option:

$ echo "This is the message" | mail -s "Testing replyto" -S replyto="mark@gmail.com" 
someone@example.com


7. Attachments

Attachments can be added with the "-a" option:

$ echo "This is the message body" | mail -s "This is the Subject" -r 
"Harry<harry@gmail.com>" -a /path/to/file someone@example.com

8. Use external SMTP server

This is an exclusive feature, available only with heirloom mailx and not with bsd mailx, the mail command from GNU mailutils, or the mutt command.

The mailx command can use an external SMTP server to relay the message. The syntax is a bit lengthy but makes sense:

$ echo "This is the message" | mailx -v -r "someone@example.com" -s "This is the subject" -S smtp=mail.telkom.net onno@indo.net.id

For Gmail specifically, you would need to enable less secure apps settings before you can send mail like this.

9. Verbose - watch SMTP communication

When using external SMTP servers, you can choose to watch the entire SMTP communication done in the background. This is useful especially when testing or debugging SMTP servers.

$ echo "This is the message body and contains the message from heirloom mailx" | mailx -v -s "This is the subject" -S smtp="smtp.gmail.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="mygmail@gmail.com" -S smtp-auth-password="mypassword" -S ssl-verify=ignore someone@example.com

References