Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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="tarun@gmail.com"
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.

share|improve this question
    
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

2 Answers 2

up vote 1 down vote accepted

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

echo "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 |).

share|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
    
@ gertvdijk : Ohk. Thanks for the help. :) –  Tarun Jun 20 '13 at 11:28
    
For new lines to work, add -e to the echo command: echo -e "This will\nsuccessfully break\nlines –  Nicolas 2 hours ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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