how to send a mail with a message in unix script

橙三吉。 提交于 2019-12-04 07:27:01
echo 'Message body goes here' | mail -s 'subject line goes here' email@provider.com

Try this on the command line or inside a script:

echo "This is the message." | mailx -s "Subject" abc@def.com

You can use pre-defined messages from files:

cat message.txt | mailx -s "Subject" abc@def.com
pitseeker

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

as mailx takes the body as input on stdin you can pipe the body to it:

echo "Hello World" | mailx -s"File not found" abc@def.com

Or use a here document

mailx -s"File not found" abc@def.com << END_TEXT
Hello World 
END_TEXT

If you also want to add attachment to the you want to send. Here you go:

echo 'Type Message body' | mailx -s 'Type subject' -a path/filename.txt email@provider.com

EXAMPLE:

echo 'PFA report' | mailx -s 'Today's Report' -a `path`/report1306.txt  xyz@gmail.com

Define the mailcontent beforehand and do it like this:

mailx -s"File not found" abc@def.com < mailcontent
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!