I'm trying to send an email using bash script.

I tried using the following in the bash script with no success as I execute the script it asks for CC:

mail -s "This is a test mail" "$recipient"

but nothing seems to work as I want it to be automated.

Also I want to send a message with the mail I went through different tutorials but in every tutorial a message is written in a text file then the file is sent but can't I write a message with the mail command itself and not save it to any file and send it. Please Help.


I just want the script to do the following

If a particular operation is complete then it sends an email to the recipient with a subject and a message saying operation is completed successfully.

  • Could you include the output of ls -l /etc/alternatives/mail in your question? It's possible you have installed a different mail client than I have, so this makes it hard to verify answers. (I can provide an answer how to send a message with it from within the script, btw.) – gertvdijk Jun 20 '13 at 8:21
  • It is like this ls -l /etc/alternatives/mailx lrwxrwxrwx 1 root root 13 Jun 20 13:15 /etc/alternatives/mailx -> /usr/bin/mail – Tarun Jun 20 '13 at 8:25
  • I didn't ask for mailx, but for mail, the command you're running. See also this answer: mail alternatives and the comments on it. Please edit your question for providing more information - this is not a discussion forum, but a Q&A site, you see. – gertvdijk Jun 20 '13 at 8:29
  • There's nothing like /etc/alternatives/mail its /etc/alternatives/mailx – Tarun Jun 20 '13 at 8:33
  • Are you sure you've installed it using Ubuntu packages? Please provide the output of which mail | xargs dpkg -S. – gertvdijk Jun 20 '13 at 8:39

Sending a body with GNU mailutils in a script should work like this:

echo -e "body of message \nAnd use newlines for new lines." | mail -s "the subject" recipient@domain.tld

It should not ask for a CC address interactively in this mode (reading from stdin using the pipe |).

| improve this answer | |
  • Thanks I just needed the heirloom-mailx package as well. @ Oli: Thanks alot :) – Tarun Jun 20 '13 at 10:14
  • bsd-mailx or the regular GNU mailutils will also work, as far as I know. – gertvdijk Jun 20 '13 at 11:17
  • For new lines to work, add -e to the echo command: echo -e "This will\nsuccessfully break\nlines – Nicolas Aug 21 '14 at 21:14
  • @Nicolas true, thanks. :) Next time, feel free to edit a post. – gertvdijk Aug 22 '14 at 11:11

mail expects a body either from stdin or after the command runs (in interactive mode). The simple way around this problem is to just echo into the command like so:

echo | mail -s "Test subject" "oli@example.com"

Note that's using mail from the heirloom-mailx package.

| improve this answer | |
    echo "To: 
    echo "From: system <root>"
    echo "Subject: Rails Energy Valut Failure on `hostname` (`hostname --ip-address`) - `date` server restart"
 ) 2>&1 | /usr/lib/sendmail -t
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.