I‘d like to send an email with the content of a text file using mail command in Unix / Linux operating system. How do I do it?

mail command (also mailx command) is an intelligent mail processing system under Unix and Linux. You need to use the following syntax to send an email using mail command:

mail -s 'Subject-Here' you@cyberciti.biz < input.file
mail -s 'Uptime Report' you@cyberciti.biz < /tmp/output.txt


  • -s ‘Subject’ : Specify subject on command line.
  • you@cyberciti.biz: To email user.
  • /tmp/output.txt : Send the content of /tmp/output.txt file using mail command.

Send an email as attachment from Unix command line

Either can use the mutt command or uuencode command as described below:

### Attach /tmp/filelist.tar.gz and read the content of a text using /tmp/body.txt ###
mutt -s "Backup status" -a /tmp/filelist.tar.gz you@cyberciti.biz < /tmp/body.txt


### Attach /tmp/list.tar.gz and and send it  ###
uuencode /tmp/list.tar.gz /tmp/list.tar.gz | mailx -s "Reports" user@eu.corp.cyberciti.biz
### Email photo.png along with a text message read from body.txt ##
(cat body.txt; uuencode photo.png photo.png) | mail -s "Subject" user@cyberciti.biz

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

🐧 22 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
22 comments… add one
  • pubudu Apr 24, 2012 @ 11:28

    Thanks nixCraft…you guys are doing a awesome job..keep it up

    • faizul Feb 10, 2014 @ 9:07

      you don’t know any thing in unix like CASe

  • Yogesh May 4, 2012 @ 3:39

    Use mail with from and to email IDs
    cat /home/user/input.file | mail -s ‘Subject-Here’ -v notify-to@email-id.com — -f from.email-id.com
    I wonder if we can specify from email ID while using mutt too?

  • Imran May 14, 2012 @ 7:24

    Hi, thank you for this great tutorial. Just wondering if there’s a pre-requirement for the above to work? I have tried on my Debian Linux box but my emails have not received the sent message. My Linux box is connected to the Internet. It’s just a web server.

    • 🐧 nixCraft May 14, 2012 @ 7:44

      You need local smtp (such as Postfix, sendmail) installed. On debian you need to start exim email service.

  • Minh Phu Mar 18, 2013 @ 1:34

    i need to send mail with multi attach file by commands line, can you help me

  • Suresh Podishetti Aug 8, 2013 @ 10:45

    Try this, It worked:
    (echo “$mail_body_text” ; uuencode “attachment1.out” “attachment1.out” )| mailx -s “$mail_Subject” $EmailUser

  • harsha Sep 10, 2013 @ 10:18

    I see issue with uuencode not showing the file attachment when opened in webmail

    begin 640 t1.txt

    above content is coming in body of the mail in place of attachment. Please let me know how to fix this.

  • Ali Sep 22, 2013 @ 22:52

    My attachment is blank. I’m having a similar problem as harsha above.

    Any clues? I can’t seem to send this file without it getting messed up. Is there another way to send text from a file to another email?

  • abc Nov 18, 2013 @ 8:51

    Is it possible to send the output by doing cat on 2 files. Say like

    cat {fil1} {fil2} | mailx

  • Yogesh Nov 22, 2013 @ 22:01


    Great one! I have a question for you.

    mailx is not working properly when sending the contents of the text file. Received mail as the content in one line even though the text file contains line by line.

    Please help me out on this issue.


    • Stijn Jan 6, 2015 @ 13:50

      Are you reading the mail on a Windows computer?

      Then it might be related to the difference in line-endings used in *nix systems and Windows.
      If you want to send a file created on a *nix system to a Windows system, first do unix2dos which will convert the file to match with the DOS (Windows) standards

  • PJeet Nov 26, 2013 @ 0:14

    I am trying below command
    mail -s ‘unix test’ xyz@gmail.com < body.txt
    where "unix test" is my subject, xyz@gmail.com is my gmail id and body.txt is my text file from which i want to copy content into my email body.

    I am getting below error message:
    postdrop: warning: unable to look up public/pickup: No such file or directory

    Is something missing in my command or is there any pre-requisit i need to follow to use this command.
    I am new to UNIX


    • Laura Edell Jun 11, 2015 @ 10:42

      Hi PJeet,

      Saw no one had replied so thought I would.

      I had the same issue. I reran the same command line a 2nd time, and it worked. No changes to the command line were made. Just entered it twice. The 1st time failed with your error. The 2nd time successfully ran. It clearly needed to create the mailbox drop location which it did after the 1st attempt. It took about 7 minutes to receive the email.

  • sandeep Dec 17, 2013 @ 11:52

    Thanks for the information. When we use mail for display content of a file as body of an email – we’re getting an email in not understandable language. Can you help me out here?

  • Turnip Feb 26, 2015 @ 19:43

    When i send

    mail -s “hello World” emal@gmail.com < /var/scripts/output.txt

    I get the email but the email is attached not outputed in to the body of the email. The file is generated by a shell script which is only a few lines.

    (using ubuntu and exim4)

    • miggy Jun 15, 2016 @ 22:06

      I am having the same issue. Anyone know why?

      • satish Oct 21, 2016 @ 13:59

        Hi Turnip or Miggy…

        Am too facing the the same issue as yours….

        Any soln. to the prob will be highly appreciated.

        Thanks in advance,

  • ashish Jul 10, 2015 @ 16:28

    hey Geniuses !

    what if I want to read subject (for the email) from a file ??

  • Dilshad Syed Jan 19, 2016 @ 13:51

    Below command for multi emails, attachments:
    cat $filename | (echo $"body" ; uuencode $filename) | mailx -s "$subject" "$mail_list"

    • Joy Corales Fajardo Feb 2, 2017 @ 6:08

      cat perfmon.log | (echo $”body” ; uuencode perfmon.log | mail -s “Monitor” -v xxxxxxxx@gmail.com

      can yoo help me format this line

  • Meenu Malik Jun 20, 2016 @ 10:35


    I have two variables M1 and M2 in my script. For M1 output is previous month name and M2 is current month name.
    I want to use the output of these variables in my subject of email using mailx command.
    Is it possible? How it can be done?
    If not, can i get these month name using unix commands ? but how to use commands in subject ?

    Meenu Malik

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