0

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:

#!/bin/bash
recipient="[email protected]"
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.

P.S.

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.

6
  • 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, 2013 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, 2013 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, 2013 at 8:29
  • There's nothing like /etc/alternatives/mail its /etc/alternatives/mailx
    – Tarun
    Jun 20, 2013 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, 2013 at 8:39

3 Answers 3

3

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" "[email protected]"

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

1

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" [email protected]

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

4
  • Thanks I just needed the heirloom-mailx package as well. @ Oli: Thanks alot :)
    – Tarun
    Jun 20, 2013 at 10:14
  • bsd-mailx or the regular GNU mailutils will also work, as far as I know.
    – gertvdijk
    Jun 20, 2013 at 11:17
  • For new lines to work, add -e to the echo command: echo -e "This will\nsuccessfully break\nlines
    – Nicolas
    Aug 21, 2014 at 21:14
  • @Nicolas true, thanks. :) Next time, feel free to edit a post.
    – gertvdijk
    Aug 22, 2014 at 11:11
0
(
    echo "To: 
    [email protected]"
    echo "From: system <root>"
    echo "Subject: Rails Energy Valut Failure on `hostname` (`hostname --ip-address`) - `date` server restart"
 ) 2>&1 | /usr/lib/sendmail -t

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .