Linux Mail command Send email from Command Line utility

Sending email from Linux command line would he more helpful when we are trying to automate from programs / applications. Linux Mail command will help us in send email command line utility. Let’s see in detailed information about mail utility. 

Linux Mail command is dependent on sendmail / postfix email services, because without any mail services email can’t be routed.

Advantages Linux Mail Command Line utility

  • Easy to Automate Tasks
  • Complex tasks can also be achieved along with programs
  • Send emails in bulk can be possible

Install mail utilities

#yum install mailx*
$apt-get install mailutils

Sending simple Email message from Command line

To send an email from command line simply use Linux mail command as mentioned below

[root@ArkitServer ~]# mail -s "Testing Message" admin@arkit.co.in <<< "Testing Email"
Or
[root@ArkitServer ~]# echo "Testing Email" | mail -s "Testing Message" admin@arkit.co.in

Here -s to provide email subject 

File content as Email body

Now situation is file has content i would like to send an file content as email body using mail command. Below command send’s email and email body as /etc/passwd content

[root@ArkitServer ~]# mail -s "Email Subject" admin@arkit.co.in < /etc/passwd

or

[root@localhost ~]# cat anaconda-ks.cfg | mail -s "Kickstart content" admin@arkit.co.in

Specify CC (Carbon Copy) and BCC (Blind Copy) recipients

A simple example to send CC and BCC

-c  Email Address

-b  Email Address

[root@localhost ~]# echo "Email Body" | mail -s "Testing Email" admin@arkit.co.in -c sendcond@gmail.com -b third@gmail.com

Custom From Address

specify from custom email address. When you send an email from command it will take logged in user@serverName but we can specify required address

[root@localhost ~]# echo "Email Body Message" | mail -s "subject" someone@email.com -r admin@arkit.co.in

Sending Email to multiple recipients

We can send email to multiple recipients using single mail command. specify multiple email addresses using comma separated.

[root@localhost ~]# mail -s "Testing Email subject" some@email.com,someone@gmail.com < anaconda-ks.cfg

Send attachment to email

Linux mail command will not allow sending email attachments directly we required to use uuencode command to send attachment

yum install sharutils*
uuencode file.txt | mail -s "Email As Attachment" admin@arkit.co.in < EmailBody.txt

 

Linux command to read email in Inbox

[root@localhost ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Fri Mar 17 21:40 18/630 "Testing Email"
& 1
Message 1:
From root@localhost.localdomain Fri Mar 17 21:40:03 2017
Return-Path: <root@localhost.localdomain>
X-Original-To: root@localhost
Delivered-To: root@localhost.localdomain
Date: Fri, 17 Mar 2017 21:40:03 +0530
To: root@localhost.localdomain
Subject: Testing Email
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R
Testing Body
& q
Held 1 message in /var/spool/mail/root

Mail command controls to manage mailbox

  • :n All new messages.
  • 😮 All old messages (any not in state read or new).
  • :u All unread messages.
  • :d All deleted messages (for the undelete command).
  • :r All read messages.
  • :f All `flagged’ messages.
  • :a All answered messages (cf. the markanswered variable).
  • :t All messages marked as draft.
  • :k All `killed’ messages.
  • :j All messages classified as junk.
  • . The current message.
  • , That is the message with the Message-ID given in the `In-Reply-To:’
  • – The next previous undeleted message, or the next previous deleted message for the undelete command. In sorted/threaded mode, the next previous such message in the sorted/threaded order.
  • + The next undeleted message, or the next deleted message for the undelete command. In sorted/threaded mode, the next such message in the sorted/threaded order.
  • ^ The first undeleted message, or the first deleted message for the undelete command. In sorted/threaded mode, the first such message in the sorted/threaded order.
  • $ In sorted/threaded mode

Conclusion

Linux Mail Command is very useful to integrate with shell scripts to automate many tasks.

Related Articles

SSH server installation and configuration step by step guide

YUM server installation and configuration guide

Kick start operating system installation

Thanks for your wonderful Support and Encouragement

ARK

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

You may also like...

1 Response

  1. Murteza says:

    Mail from terminal is a game changer for many companies an dmaybe even individuals. Thanks for the article.

    I have a request that is not related to “Linuc mail command” article and that is adding Grouvi share button among other social media buttons.
    Thanks for the article once again.

Leave a Reply