How to send email from the Linux command line

The Linux command line can be very powerful once you know how to use it. You can parse data, monitor processes, automate backups and do a lot of other useful and cool things using it. There often comes a need to generate a report and mail it out. It could be as simple a requirement as a notification that the day’s backup went through fine, or did not. I’ll help you get started with sending mails from the Linux command line and in shell scripts. We will also cover sending attachments from the command line. We will begin with the “mail” command.


First run a quick test to make sure the “sendmail” application is installed and working correctly. Execute the following command, replacing “” with your e-mail address.

# mail -s “Hello world”

Hit the return key and you will come to a new line. Enter the text “This is a test from my server”. Follow up the text by hitting the return key again. Then hit the key combination of Control+D to continue. The command prompt will ask you if you want to mark a copy of the mail to any other address, hit Control+D again. Check your mailbox. This command will send out a mail to the email id mentioned with the subject, “Hello world”.

To add content to the body of the mail while running the command you can use the following options. If you want to add text on your own:

# echo “This will go into the body of the mail.” | mail -s “Hello world”

And if you want mail to read the content from a file:

# mail -s “Hello world” < /home/calvin/application.log

Some other useful options in the mail command are:

-s subject (The subject of the mail)
-c email-address (Mark a copy to this “email-address”, or CC)
-b email-address (Mark a blind carbon copy to this “email-address”, or BCC)

Here’s how you might use these options:

# echo “Welcome to the world of Calvin n Hobbes” | mail -s “Hello world” -c -b


One of major drawbacks of using the mail command is that it does not support the sending of attachments. mutt, on the other hand, does support it. I’ve found this feature particularly useful for scripts that generate non-textual reports or backups which are relatively small in size which I’d like to backup elsewhere. Of course, mutt allows you to do a lot more than just send attachments. It is a much more complete command line mail client than the “mail” command. Right now we’ll just explore the basic stuff we might need often. Here’s how you would attach a file to a mail:

# echo “Sending an attachment.” | mutt -a -s “attachment”

This command will send a mail to with the subject (-s) “attachment”, the body text “Sending an attachment.”, containing the attachment (-a) Like with the mail command you can use the “-c” option to mark a copy to another mail id.


Now, with the basics covered you can send mails from your shell scripts. Here’s a simple shell script that gives you a reading of the usage of space on your partitions and mails the data to you.

df -h | mail -s “disk space report”

Save these lines in a file on your Linux server and run it. You should receive a mail containing the results of the command. If, however, you need to send more data than just this you will need to write the data to a text file and enter it into the mail body while composing the mail. Here’s and example of a shell script that gets the disk usage as well as the memory usage, writes the data into a temporary file, and then enters it all into the body of the mail being sent out:

df -h > /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s “disk and RAM report” < /tmp/mail_report.log

Now here’s a more complicated problem. You have to take a backup of a few files and mail then out. First the directory to be mailed out is archived. Then it is sent as an email attachment using mutt. Here’s a script to do just that:

tar -zcf /tmp/backup.tar.gz /home/calvin/files
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data”

The echo at the start of the last line adds a blank into the body of the mail being set out.

This should get you started with sending mails form the Linux command line and from shell scripts. Read up the “man page” for both mail and mutt for more options (enter the commands “man mail” and “man mutt” for the full manual on each).

{ 84 comments… add one }
  • Michal December 8, 2008, 3:20 am

    You may want to have a look at Check out the usage examples. I’d say it’s the ultimate command line smtp client, but I’m a little biased ;-)

  • Matt January 21, 2009, 11:24 pm

    echo | mutt -a -s /tmp/backup.tar.gz “daily backup of data”

    should be:

    echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data”

  • Ross McKillop January 22, 2009, 12:47 am

    Matt –

    Thanks very much for catching that! I’ve made the correction. Cheers!

  • Jens Arnfelt March 13, 2009, 1:52 am


    You can actually send binary attachment via “mail” the following way :

    uuencode /etc/hosts /etc/hosts | mail -s “mail with binary attachment”

    Jens Arnfelt

  • joviano May 11, 2009, 2:05 am

    Basically have a test script, whose output comes by mail to me with the use of the command taken from this article:

    echo `my test script` | mail -s “Test Result”

    the problem here is that when i do this.. it works correctly on the mailing front, however the mail that i then get ignores the line breaks in the output of the scripts. I really need the line breaks for readability of the output results..

    any pointers on how i could achieve this ?? .. would really like some help here

  • martin July 10, 2009, 10:51 am

    I’m looking for a program that will run from a script and will send mail via gmail smtp server. Any ideas?

  • Michal July 10, 2009, 4:54 pm

    martin: have a look at – it is designed to run from a script or a cron job and send email through a specified server. For example: --server --from --to some@body.else --subject Blah --attach /path/to/a/picture.png

    It can also authenticate with a Gmail username and password among other things.

  • martin July 22, 2009, 1:50 pm

    Thanks Michal. I’ll check that out.

  • Sujith John August 1, 2009, 7:33 am

    An excellent tutorial !! Thanks Much

  • Vasanth October 14, 2009, 1:35 am

    Thank You. i exactly needed this.

    Thank you :-)

  • John Britto.J October 20, 2009, 2:06 am

    Friends I tried the procedure what is given for sending mail through command line.
    I was able to type subject , the message and even cc. But when I checked in my account there was no mail received like this.

  • hendro October 31, 2009, 7:34 pm

    how to get mail using command line, sent through proxy.


  • hendro October 31, 2009, 7:35 pm

    i am sorry, i made a mistake, my question is:
    how to SEND mail using command line, sent through proxy.


  • Erik January 4, 2010, 12:57 pm

    Is there a way to use variables for the body of a message like in Mailx instead of reading from a file?

  • Robin January 9, 2010, 10:54 pm

    Thanks for the nice tutorial..
    @John Britto: I had the same problem on my Debian – was getting no email whatsoever. I had to install sendmail. After installing sendmail, it just started working!

  • Keisial January 25, 2010, 1:21 pm

    Joviano, instead of
    echo `my test script` | mail -s “Test Result”
    simply do
    my test script | mail -s “Test Result”

    Equally, the df + free reports can also be done as:
    ( df -h; free -m ) | mail -s “disk and RAM report”

  • Daniel February 2, 2010, 8:11 am

    well done and thanks. How would you add a carraige return to it. I tiried \n and \r\n and had no luck

  • Keisial February 3, 2010, 3:58 pm

    You can simply do:
    echo -e “Hello\nWorld” | mail your@email

    However, not all systems support -e option on echo so it’s more portable to use:
    printf “Hello\nWorld” | mail your@email

  • Nige March 17, 2010, 4:34 pm

    Hey There

    Been working on this all day (im a big linux noob) and finally got a solution that lets me set the “from” part, with line breaks, all piped into one line. Im going to use this with the exec command from within php to spawn off emails but not loose application speed (hopefully)

    printf “This is the main body of the mail\n with line break” | mail -s “Subject of the Email” -f

    hope this help someone else

  • Nige March 17, 2010, 4:48 pm

    just to add to previous comment

    exec(“printf \”This is the main body of the mail\n with line break\” | mail -s \”Subject of the Email\” -f > /dev/null 2>&1 &”);

    will tell php to spawn off a separate thread so as not to hold up the scripts execution time.

  • Joseph March 28, 2010, 10:26 am

    Here’s a good solution: SendEmail
    An Email Program for Sending SMTP Mail from a Command Line

    It’s a single perl script, and it doesn’t make any [bad] assumptions such as sendmail running and configured properly. It supports attachments.

  • deepak March 30, 2010, 3:18 am

    how to send mials in linux

  • littlebearz July 16, 2010, 7:46 pm

    Wow, thanks, this is quite useful, well indented.

  • Murray Crane July 29, 2010, 12:18 pm

    All of you that had to install sendmail (or postfix, or any of the other bloated MTAs), if all you want is to get emails out of a machine and on to a mail smart host, take a look at ssmtp (it’s in APT with that name if you are a Debian/Ubuntu user). It’s one and only function is to “forward” the mails it receives to a mail smart host.

  • Tony October 6, 2010, 5:21 pm

    Need to send with Cc: and the -c does not work, please refer to below:
    Trying: mail -s subject-blah -c < my_body_file.txt

  • Kogychan March 8, 2011, 11:27 am

    thanks… perfect tutorial, exactly what I was looking for. BSBS (Be smart, Be Simple :))

  • queenet July 29, 2011, 7:08 pm

    18 Nov 2008 – Whenever I go to my email I have a 100 junk emails – I signed up for website builders that were only scam forwarders. I tried emailing the senders, but they use fake accounts so I can never get rid of i

  • Ron August 21, 2011, 1:30 pm

    very interesting and important + a very good tutorial

  • Juan September 20, 2011, 4:17 pm

    Thanks finally could test if my SMTP was installed correctly… now to connect it to the rails app…. ( :D )

  • tosiara January 5, 2012, 10:16 am

    I was getting “No recipients specified.” when trying your examples. Then I looked into man:

    -a […] — attach file(s) to the message
    the list of files must be terminated with the “–” sequence

    You missed “–” after filename in your exampl

  • Carlos January 11, 2012, 10:12 am

    I have a problem: I can’t visualise the mail content in webmail or thunderbird when I send a mail with a script from a cron schedule:
    mail -s “avisobckmensual”

    I receive the mail but instead of having content, I receive an empty mail with an attachment named “subjet”.octet-stream

    BUT, if I run the script directly from the terminal… it works flawlessly.

    Can you help me?

  • Raghav February 18, 2012, 1:12 pm

    Hi All,

    i want to add from field to mail command…can any1 help me……..?

  • rebelk iran March 18, 2012, 10:14 am

    hi chintu how are you i hope sam with you same

  • Abhi August 4, 2012, 3:13 pm

    hi, I am unable to receive mails….can some one tell me what is the reason???
    i installed mutt, ssmtp as well but no luck….

  • Toddb August 9, 2012, 1:59 pm

    none of these commands work,  there must be more to it that a simple command

  • Rajeev Sharma August 23, 2012, 11:49 am

    None of the above commands are working.. :(

  • Ross McKillop August 23, 2012, 1:37 pm

    @fd18635dd98030e12d472dfd452aa4e7:disqus – which Linux/Unix/BSD etc are you running?

  • ukcjmad September 11, 2012, 8:10 am

    According to other websites you can send attachments using “mail” if you pipe the attachment through uuencode into the mail command line. Examples:

    uuencode surfing.jpeg surfing.jpeg | mail
    (cat mailtext; uuencode surfing.jpeg surfing.jpeg) | mail

  • malini September 14, 2012, 2:50 am

    none of these working

  • Naresh September 26, 2012, 5:57 pm


  • Saikat October 3, 2012, 5:43 pm

    Malini can you please post your problem.

  • Hans Stam December 24, 2012, 12:15 am

    You sir, are awesome

  • Isamaer January 9, 2013, 2:03 pm

    the -a attachment on Mutt may need to go after the recipient not before.

  • jayanth April 26, 2013, 8:26 am

    Hi, i am facing one problem i have written one script for disk space usagi shown the script below.

    a=`df -h /home/jayantc | tail -1 | cut -b40,41,42`

    if test $a -ge 60


    echo “Hi jayanth You have used $a% of diskspace please delete some files from your Home directory to avoid space issue” | mutt -s “Disk_SPace details”


    echo “You used less than 60% No problem” | mutt -s “Disk_SPace details”


    script will execute successfully but mails are not triggereing please help me out on this if i made any mistake.

  • Kimble June 6, 2013, 6:05 pm

    What is the largest file you can attach and send using mutt?

  • Andrea June 24, 2013, 9:44 am

    thanks 1k

  • George Brooke August 22, 2013, 2:55 am

    i get this when not doing from shell and just sending plain mail. what is goin on?

  • Sergio Francisco September 5, 2013, 4:49 pm

    Very useful! Thanks.

  • Daniel September 11, 2013, 9:43 pm

    He doesn’t have mutt or senmail installed…

  • ashish September 27, 2013, 12:09 am

    Helped me. cheers..!

  • James October 11, 2013, 6:51 pm


    how to tweak the mail command, so that it only emails if the log file is not empty.

  • Sai November 11, 2013, 12:00 pm


  • Jon January 20, 2014, 8:56 am

    you’d need to wrap it in a if statement, I doubt this could be done just with mail. Something like:
    if [ `du /path/to/log.log` -gt 0 ]
    mail -s “file de log” < /path/to/log.log

  • Frank Lister May 5, 2014, 7:51 pm

    using mail
    mail -s “My Subjet” < /home/tmp/body.txt

    sometimes I get an email and sometimes I don't. Sometimes they appear fast in the inbox, sometimes it takes minutes/hours. Usually, the first email sent (of the day) goes fast, the others, who knows??

    cant the "mail" service be restarted? or anyone have an idea as to why/how this happens???

  • okau May 6, 2014, 3:19 am

    How to do NTLM authentication

  • Adhip July 27, 2014, 6:28 am


  • Bharath B S January 23, 2015, 3:44 am

    Helped me thanks… :)

  • Vitaly S April 24, 2015, 10:32 am

    :( It works in Ubuntu without configuration, but doesn’t work for me on Debian. Does something would be configured?

  • arar July 21, 2015, 9:30 am

    u must install mailutils package in debian

  • Peter September 14, 2015, 7:40 am

    I noticed that if the body contains weird characters then the mail fails, if the body is plain english then it is ok.

  • Joey Arkenstat September 19, 2015, 3:02 pm

    Worked great, many thanks!

  • Veerayya December 23, 2015, 4:14 am


    How to send CC to the user using TCl and mql command

    where it should look like

    TO: Myemail_id

    CC: List of other user id’s

    I have below small code used to send mail but some please help me to add cc to the mail

    set sUserList {}

    set sMessage “Hi,nnPlease find the attached report for the list of parts for Title $llTitle nnRegards,nReport Admin”

    foreach sUserMail $sUserList {

    exec echo $sMessage | mail -s “SEW PATTERN Parts with Relationship EBOM – [clock format [clock seconds] -format “%Y-%m-%d”]” -a $fName $sUserMail


  • long boy November 8, 2016, 4:34 am


  • fisioterapia pamplona April 11, 2019, 2:21 am

    Muy bien, gracias,muchas gracias

  • architects in coimbatore June 22, 2019, 7:23 pm

    Hi there mates, how is the whole thing, and what you would like to
    say about this article, in my view its truly remarkable designed for

Leave a Comment