Shell script to send email

I am on a linux machine and I am monitoring the use of the process. Most of the time I will be away from my system and I have access to the Internet on my device. Therefore, I planned to write a shell script that could send me a message about the exit of the process.

Is it possible?

If so, how do I make a shell script send me mail?

Provide a snippet to get started.

+72
linux scripting shell email
Jan 11 2018-11-11T00:
source share
7 answers

Yes, it works fine and is commonly used:

$ echo "hello world" | mail -s "a subject" someone@somewhere.com 
+108
Jan 11 2018-11-11T00:
source share

Basically, there is a program for this called "mail". The email subject can be specified using -s and the address list with -t. You can write the text yourself using the echo command:

 echo "This will go into the body of the mail." | mail -s "Hello world" you@youremail.com 

or get it from other files:

 mail -s "Hello world" you@youremailid.com < /home/calvin/application.log 

mail does not support sending attachments, but Mutt does:

 echo "Sending an attachment." | mutt -a file.zip -s "attachment" target@email.com 

Please note that mutt is much more than mail. You can find the best explanation here.

PS: thanks @slhck for pointing out that my previous answer was terrible .;)

+19
Jan 11 '11 at
source share

sendmail works for me on mac (10.6.8)

 echo "Hello" | sendmail -f my@email.com my@email.com 
+6
Jul 06 2018-12-21T00:
source share
 #!/bin/sh #set -x LANG=fr_FR # ARG FROM="foo@bar.com" TO="foo@bar.com" 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 
+3
Sep 16 '13 at 14:47
source share
 mail -s "Your Subject" your@email.com < /file/with/mail/content 

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

+1
Jan 11 2018-11-11T00:
source share

Well, the simplest solution, of course, would be to output the output to the 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 think sh test.sh & will do the same normally.

0
Jan 11 '11 at 1:59 april
source share
 top -b -n 1 | mail -s "any subject" your_email@domain.com 
0
Jan 11 2018-11-11T00:
source share



All Articles