102

I am on linux machine and I monitor a process usage. Most of the time I will be away from my system and I have access to internet on my device. So I planned to write a shell-script that can mail me the output of the process.

Is it possible?

If so how to make a shell-script send me mail?

Please provide a snippet to get started.

0

7 Answers 7

157

Yes it works fine and is commonly used:

$ echo "hello world" | mail -s "a subject" [email protected]
10
  • 30
    $ echo "hello world" | mail -s "a subject" -a "attachment file" [email protected] for sending files Feb 18, 2013 at 7:45
  • 3
    Where can I find the preset tutorial for this command? As to execute this command, I need to configure something.
    – Zen
    Sep 6, 2014 at 2:43
  • 1
    @Zen Well you'll need to have your local MTA configured.
    – trojanfoe
    Sep 6, 2014 at 8:26
  • 9
    Don't forget to install the mail program first: sudo apt-get install mailutils Jan 25, 2017 at 16:13
  • 3
    What will the receiver see in the From field? Who will be the sender in this case? Apr 12, 2019 at 5:17
32

Basically there's a program to accomplish that, called "mail". The subject of the email can be specified with a -s and a list of address with -t. You can write the text on your own with the echo command:

echo "This will go into the body of the mail." | mail -s "Hello world" [email protected]

or get it from other files too:

mail -s "Hello world" [email protected] < /home/calvin/application.log

mail doesn't support the sending of attachments, but Mutt does:

echo "Sending an attachment." | mutt -a file.zip -s "attachment" [email protected]

Note that Mutt's much more complete than mail. You can find better explanation here

PS: thanks to @slhck who pointed out that my previous answer was awful. ;)

7
  • 2
    Stackoverflow should be self contained - it would have taken you the same amount of time to copy/paste the URL of that site and to copy/paste the one-liner @Ashwin asked for.
    – slhck
    Jan 11, 2011 at 13:59
  • @slhck: Probably the full page is better than one or two lines and there's explained better than I would do. Mine isn't surely the best answer but downvote it is quite unfair since it provides the same information of any other answer.
    – BlackBear
    Jan 11, 2011 at 14:16
  • This has been a long discussion: meta.stackexchange.com/questions/8231/… - Even if it provides the same information, you could have digested it and cited your source.
    – slhck
    Jan 11, 2011 at 14:18
  • @slhck: I agree with that, rarely I give only a link. But I thought, "a link is better than nothing", in fact I've never sent email in this way, just wanted to be helpful. ;)
    – BlackBear
    Jan 11, 2011 at 14:24
  • 2
    @slhck: edited. Thanks for your comments, there's always room for improvements. ;)
    – BlackBear
    Jan 11, 2011 at 14:42
7

sendmail works for me on the mac (10.6.8)

echo "Hello" | sendmail -f [email protected] [email protected]
3
mail -s "Your Subject" [email protected] < /file/with/mail/content

(/file/with/mail/content should be a plaintext file, not a file attachment or an image, etc)

3
  • 1
    Does this work with binary files? I think I've had to use mutt before to do all the binary file encoding in the past.
    – Marcin
    Jan 11, 2011 at 14:12
  • You mean when trying to send a file as a mail attachment? You're right about this, of course. I meant text files only.
    – slhck
    Jan 11, 2011 at 14:20
  • Yes! @slhck meant text files only. 'mail' cannot make attachments to user.
    – user517400
    Jan 13, 2011 at 11:35
3
#!/bin/sh
#set -x
LANG=fr_FR

# ARG
FROM="[email protected]"
TO="[email protected]"
SUBJECT="test é"
MSG="BODY éé"
FILES="fic1.pdf fic2.pdf"

# http://fr.wikipedia.org/wiki/Multipurpose_Internet_Mail_Extensions
SUB_CHARSET=$(echo ${SUBJECT} | file -bi - | cut -d"=" -f2)
SUB_B64=$(echo ${SUBJECT} | uuencode --base64 - | tail -n+2 | head -n+1)

NB_FILES=$(echo ${FILES} | wc -w)
NB=0
cat <<EOF | /usr/sbin/sendmail -t
From: ${FROM}
To: ${TO}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=frontier
Subject: =?${SUB_CHARSET}?B?${SUB_B64}?=

--frontier
Content-Type: $(echo ${MSG} | file -bi -)
Content-Transfer-Encoding: 7bit

${MSG}
$(test $NB_FILES -eq 0 && echo "--frontier--" || echo "--frontier")
$(for file in ${FILES} ; do
        let NB=${NB}+1
        FILE_NAME="$(basename $file)"
        echo "Content-Type: $(file -bi $file); name=\"${FILE_NAME}\""
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: attachment; filename=\"${FILE_NAME}\""
        #echo ""
        uuencode --base64 ${file} ${FILE_NAME}
        test ${NB} -eq ${NB_FILES} && echo "--frontier--" || echo 
"--frontier"
done)
EOF
0

Well, the easiest solution would of course be to pipe the output into mail:

vs@lambda:~$ cat test.sh
sleep 3 && echo test | mail -s test your@address
vs@lambda:~$ nohup sh test.sh
nohup: ignoring input and appending output to `nohup.out'

I guess sh test.sh & will do just as fine normally.

0
top -b -n 1 | mail -s "any subject" [email protected]