UNIX / Linux: Shell Scripting With mail Command

How do I send e-mails from a shell script including file attachments?

The easiest solution is to send email from within shell scripts is mail command as follows. To send a message to one or more people, mail can be invoked with arguments which are the names of people to whom the mail will be sent:

mail -s 'subject' username
mail -s 'subject' vivek@nixcraft.net.in
mail -s 'Duplicate ip detected' -c vivek@nixcraft.net.in  ipadmin@nixcraft.net.in </var/log/ipscan.log
mail -s 'yum update failed' -c vivek@nixcraft.net.in -b  sysadins@groups.nixcraft.net.in </var/log/yum.log
mail -s 'Disk failure'  vivek@nixcraft.net.in < /tmp/message


  • -s ‘word1 word2’ : Specify subject on command line.
  • -c : Send carbon copies to list of users.
  • -b : Send blind carbon copies to list. List should be a comma-separated list of names.
  • < /path/to/file : Read email body using this /path/to/file.

Method #1: Sending File Attachments

The mail command will not work. You need to use uuencode command to send a binary file called reports.tar.gz:

uuencode reports.tar.gz reports.tar.gz | mail -s "Weekly Reports for $(date)" admin@groups.mgmt.nixcraft.net.in

You can email images or any file using the same format:

uuencode file1.png file1.png | mail -s "Funny" users@groups.nixcraft.net.in

Method #2: File Attachments with mutt Command

The mutt is a MUA (mail user agent) – an email client for text based session. It can be used for reading electronic mail under unix like operating systems, including support color terminals, MIME (attachments), and a threaded sorting mode. You can use mutt command as follows to send email attachments:

mutt -s "Filewall logs" -a /tmp/fw.logs.gz vivek@nixcraft.net.in < /tmp/emailmessage.txt

Tip # 1: Empty Mail Body

To use an empty line as the mail body use special file /dev/null or echo command as follows:

echo | mutt -s 'Subject' -a attachment.tar.gz vivek@nixcraft.net.in
mutt -s 'Subject' -a attachment.tar.gz vivek@nixcraft.net.in </dev/null
mail -s "Disk failed @ $(hostname)" mobilenumber@services.api.nixcraft.net.in </dev/null

Tip #2: Writing Mail Body Using Here documents

The here documents (redirection) tells the shell to read input from the current source (HERE) until a line containg only word (HERE) is seen:

mail -s "Disk Failed" vivek@nixcraft.net.in<<EOF
NAS server [ mounted at $(hostname) ] is running out of disk space!!!
Current allocation ${_SPACE} @ $(date)

Tip # 3: Conditional Mail

Use the if else if and the exit status of a command as follows:

[ $(grep -c -i "hardware error" /var/log/mcelog) -gt 0 ] && { echo "Hardware errors found on $(hostname) @ $(date). See log file for the details /var/log/mcelog." | mail -s "HW Errors" mobilephone@api.nixcraft.net.in ;}


# backup dirs using gnu/tar
/usr/bin/tar --exclude "*/proc/*" --exclude "*/dev/*" --exclude '*/cache/*' -zcvf /nas05/backup.tar.gz /var/www/html /home/vivek
# send an email if backup failed
if [ $? -ne 0 ] 
 /usr/bin/mail -s "GNU/TAR Backup Failed" vivek@nixcraft.net.in<<EOF
GNU/Tar backup failed @ $(date) for $(hostname)
 /usr/bin/logger "Backup done"        
# clean up
rm $_filename

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • Devarishi Oct 12, 2010 @ 19:43

    That’s great and very helpful!

    Thanks for introducing “uuencode”.

  • marc Oct 14, 2010 @ 13:53

    good stuff !!!

    it took me months to figure out how to send HTML,
    so here’s hoping this can save some1 else head ache…

    mail -a ‘Content-Type: text/html’ -s “$ERROR” “$EMAIL” << EOF
    NAS server [ mounted at $(hostname) ] is running out of disk space!!!
    Current allocation ${_SPACE} @ $(date)

    • Will Sep 2, 2011 @ 17:10

      I tried the -a option for specifying content-type in Fedora and it did not work. what version of mail are you using on what OS?

  • Jboydjvc Feb 18, 2014 @ 23:27

    How do i add email notification to my shell when runed

  • sreenath Feb 20, 2014 @ 14:47

    I have tried all the ways, but none of them is working for me. Its throwing error
    mail: invalid option — ‘c’
    Try `mail –help’ or `mail –usage’ for more information.
    I have tried installing mutt and mailutils. But no luck.

    Please help me in getting this error resolved.

  • Luke Watkins Aug 21, 2016 @ 11:58

    How could I send an email notification when a certain process (configures to run at startup) is not running or fails?

    • 🐧 Vivek Gite Aug 21, 2016 @ 19:48

      Check for process id using pgrep. If not found send an email. Create a script and call at @reboot cron job.

  • Andy Behrens Feb 5, 2017 @ 5:40

    It is bizarre that anyone would still suggest using uuencode for mail message attachments when there is an actual standard that specifies how attachments should be included in a mail message. That standard is MIME, and it’s been an Internet standard (RFC 2045) for over 20 years now.

  • Ashif Feb 28, 2017 @ 8:51

    i created a job in jenkins, in s single job am running two builds, one is Ant build that i run through shell script and second by triggering build.In this single build both Ant and further process runs.I am getting mail notifications for final build thats fine.but i need an mail notification for Ant build which i am running by shell script.what Commands should i use to get mail notifc.please any one guid me in that.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum