Thursday, 22 June 2017
UNIX

UNIX mailx Command to send Email with Example

“Mailx is an intelligent mail processing system, which has a command syntax reminiscent of ed(1) with lines replaced by messages.  It is based on Berkeley Mail 8.1 is intended to provide the functionality of the POSIX mailx command, and offers extensions for MIME, IMAP, POP3, SMTP, and S/MIME.  Mailx provides enhanced features for interactive use, such as caching and disconnected operation for IMAP, message threading, scoring and filtering.  It is also usable as a mail batch language, both for sending and receiving mail” ……………Source UNIX mailx manual.

Servers widely use Unix as an Operating System. It is secure and provides good features even it is a command based system.

In this article, I will cover basics of mailx command with some working 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
Description : Mailx is an enhanced mail command, which provides the functionality
: of the POSIX mailx command, as well as SysV mail and Berkeley Mail
: (from which it is derived).
:
: Additionally to the POSIX features, mailx can work with Maildir/ e-mail
: storage format (as well as mailboxes), supports IMAP, POP3 and SMTP
: procotols (including over SSL) to operate with remote hosts, handles mime
: types and different charsets. There are a lot of other useful features,
: see mailx(1).
:
: And as its ancient analogues, mailx can be used as a mail script language,
: both for sending and receiving mail.
:
: Besides the "mailx" command, this package provides "mail" and "Mail"
: (which should be compatible with its predecessors from the mailx-8.x source),
: as well as "nail" (the initial name of this project).

Utility not there? Don’t worry, Use below command to install it.



yum install heirloom-mailx

You can refer http://heirloom.sourceforge.net/mailx.html details information about this utility and also refer UNIX man mailx page. Here I am going to mention minimum required options you should know to use it in a practical world.

mailx – send and receive Internet mail

Below is simple command syntax, for detail I would sggest to refern manual page for mailx.

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.se

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

%d bloggers like this: