UNIX/Linux 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 by example.  We will use Heirloom mailx utility.

Use below command to check if, mailx available on your Unix system.

yum info mailx

You can use the command to install it.

yum install heirloom-mailx

Refer heirloom mailx 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.

mailx 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

Below example will send an email with no message body.

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

Example 3: – mail with file content as message body

You can use below command to send the content of the file as the message body. Generally, this method is used to email log file.

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

Example 4: – mail with file as attachment

Below command send a file as an attachment.

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

Example 5: – mail with CC and BCC

Use CC and BCC option to send email to CC and BCC address.

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

 


Advertisement

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *