Friday, 18 August 2017
UNIX

UNIX mailx Command to send Email with Example

Mailx is an intelligent mail processing system with good features to send and receive email available on Unix OS.

In this article, I will cover basics of mailx command with example.  We will use Heirloom mailx utility. Use yum info mailx to check if mailx available on your Unix system.

bash-4.1# yum info mailx
Loaded plugins: refresh-packagekit, ulninfo
Installed Packages
Name : mailx
Arch : x86_64
Version : 12.4
Release : 8.el6_6
Size : 452 k
Repo : installed
From repo : public_ol6_latest
Summary : Enhanced implementation of the mailx command
URL : http://heirloom.sourceforge.net/mailx.html
License : BSD with advertising and MPLv1.1

You can use below command to install it.

yum install heirloom-mailx

Refer http://heirloom.sourceforge.net/mailx.html  for detail information about this utility. You can also refer UNIX man mailx page. Below are minimum required options you should know to use it in a practical world.

Below is syntax,



mailx [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr

OPTIONS

  • -s – subject of an email. Mention it in double quotes if it contains space
  • -a – attach the given filename to message
  • -b – send blind carbon copies to list of users
  • -c – send carbon copies to list of users
  • -r – set the from address
  • -v – Verbose mode.  The details of delivery are displayed on the user’s terminal.

Example 1: – Simple mail with subject and body

Just log in to Unix console and run below command and hit enter. Then enter the message body and click on Ctrl+D to complete message. It displays EOT and exist the message body and sends the mail.

mailx -s "Test Email" [email protected]
Hi
How are you?
This is a test email.
EOT

You can also use ‘|’ pipe to pass message body to mailx command as shown below.

 echo "Hi How are you" | mailx -s "Test Email" [email protected]

Below is email received.

mailx sample received email

Example 2: – mail with no message body

mailx -s "Test Email" [email protected] < /dev/null

Example 3: – mail with file content as message body

mailx -s "Test Email" [email protected] < filename

Example 4: – mail with file as attachment

mailx -s "Test Email" -a filename [email protected]

Example 5: – mail with CC and BCC

mailx -s "Test Email" -c [email protected] -b [email protected] [email protected]

Leave a Reply