Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a mail with a message in unix script

New to unix and learning the talk and walk of it. I am writing a script in .ksh and have a requirement of sending a mail with a message. Currently using this command in my script:

    mailx -s"File not found" [email protected]

This command helps me having a subject and the recipient name. My question is how can I write a message along with it. Cause every time i run the script it pauses and asks me to enter the message and then executes, I want to pre-include the message so the script would not pause in between.

like image 954
Rahul sawant Avatar asked Dec 06 '13 14:12

Rahul sawant


People also ask

How do you send the body of an email in Unix?

Specify the mail body in a single line We can specify the subject and message in a single line. To specify the message body in a single line, execute the below command: mail -s "subject" <recipient_address> <<< 'Message'


3 Answers

echo 'Message body goes here' | mail -s 'subject line goes here' [email protected]
like image 183
user2839978 Avatar answered Sep 21 '22 12:09

user2839978


Try this on the command line or inside a script:

echo "This is the message." | mailx -s "Subject" [email protected]

You can use pre-defined messages from files:

cat message.txt | mailx -s "Subject" [email protected]
like image 44
Christian Aigner Avatar answered Sep 21 '22 12:09

Christian Aigner


Alternatively to mailx (mentioned in the other answers) you can also use sendmail:

cat <<EOF | sendmail -t
To: recipients-mailaddress
From: your-mailaddress
Subject: the-subject
mailtext
blabla
.
EOF

Perhaps you need to add the full path to sendmail if it's not in your path. E.g. /usr/sbin/sendmail or /usr/lib/sendmail.

Update:
See also this question

like image 35
pitseeker Avatar answered Sep 18 '22 12:09

pitseeker