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" firstname.lastname@example.org <<< "Testing Email" Or [root@ArkitServer ~]# echo "Testing Email" | mail -s "Testing Message" email@example.com
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" firstname.lastname@example.org < /etc/passwd
[root@localhost ~]# cat anaconda-ks.cfg | mail -s "Kickstart content" email@example.com
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" firstname.lastname@example.org -c email@example.com -b firstname.lastname@example.org
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" email@example.com -r firstname.lastname@example.org
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" email@example.com,firstname.lastname@example.org < 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" email@example.com < 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 firstname.lastname@example.org Fri Mar 17 21:40:03 2017 Return-Path: <email@example.com> X-Original-To: root@localhost Delivered-To: firstname.lastname@example.org Date: Fri, 17 Mar 2017 21:40:03 +0530 To: email@example.com Subject: Testing Email User-Agent: Heirloom mailx 12.5 7/5/10 Content-Type: text/plain; charset=us-ascii From: firstname.lastname@example.org (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
Linux Mail Command is very useful to integrate with shell scripts to automate many tasks.
Thanks for your wonderful Support and Encouragement
Stay Connected With Us. Learn More and Earn More