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 thoughts on “How to send email from the Linux command line”

  1. Hi

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

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

    Jens Arnfelt

  2. 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

  3. 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.

  4. Pingback: How to quickly add Contacts to your Address Book from Mail | TuxWire : The Linux Blog

  5. 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.

  6. 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!

  7. 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

  8. 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

  9. 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.

  10. Pingback: how to set up sending mail from the command line

  11. 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.

  12. Pingback: Send webmail from CommandLIne

  13. Pingback: Bmon, mail & other Linux goodies

  14. Pingback: : Mike Mindel: How to send email from the Linux command line – Simple Help | MiloRiano: Computers news, tips, guides...

  15. Pingback: Delicious Bookmarks for July 2nd through July 3rd « Lâmôlabs

  16. 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

  17. Pingback: Cron Job Nightly Email Question

  18. Pingback: cron job

  19. 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

  20. 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?

  21. Pingback: Send email from command line « The Ubuntu Incident

  22. 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….

  23. 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

  24. Pingback: Les commandes Linux qui vous feront gagner du temps

  25. Pingback: Sending mail from command line.

  26. Pingback: Mémo commandes Linux ( 2 ) | Mémos RT

  27. 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.

  28. Pingback: Sending mail from shell console | Computing

  29. Pingback: Sending mail from command line or shell program | Computing

    1. 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

  30. Pingback: How do I send email from the linux command line? |

  31. Pingback: How to send log files to a particular e-mail address?

  32. Pingback: How is mail actually sent when I use the Linux “mail” command? | M

  33. Frank Lister

    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???

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

  35. Pingback: Postfix Relay via Gmail | Natural Born Coder

  36. HI

    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


  37. Pingback: Les fichiers journaux | Ma Vie de Linuxien, dans les Nuages!

Leave a Comment

Your email address will not be published. Required fields are marked *