How to Send a Mail with a Message in Unix Script

Sending a mail from a linux shell script

If the server is well configured, eg it has an up and running MTA, you can just use the mail command.

For instance, to send the content of a file, you can do this:

$ cat /path/to/file | mail -s "your subject" your@email.com

man mail for more details.

Shell script to send email

Yes it works fine and is commonly used:

$ echo "hello world" | mail -s "a subject" someone@somewhere.com

Sending email using unix shell scripting

You forgot the quotes:

echo $body | mail $receiver -s "$subj"

Note that you must use double quotes (otherwise, the variable won't be expanded).

Now the question is: Why double quotes around $subj and not $body or $receiver. The answer is that echo doesn't care about the number of arguments. So if $body expands to several words, echo will just print all of them with a single space in between. Here, the quotes would only matter if you wanted to preserve double spaces.

As for $receiver, this works because it expands only to a single word (no spaces). It would break for mail addresses like John Doe <doe@none.com>.

how to setup bash script to send a mail when new user account is created in Linux

I implemented a couple of improvements to your script.

  • On most Linux systems /var/log/secure does not exist. Records concerning accounts are usually logged in /var/log/auth.log.
  • Writing to a file is best avoided when possible. In this case I had everything just pipe into mail.

My version of your script:

#!/bin/bash

#Set the variable which equal to zero
prev_count=0

count=$(grep -s -i "`date --date='yesterday' '+%b %e'`" /var/log/auth.log /var/log/secure | egrep -wi 'useradd' | wc -l)

if [ "$prev_count" -lt "$count" ] ; then

# Send a mail to given email id when errors found in log
SUBJECT="ATTENTION: New User Account is created on server : `date --date='yesterday' '+%b %e'`"

TO="mail-name@example.com"

( \
echo "Hostname: `hostname`"; \
echo -e "\n"; \
echo "The New User Details are below."; \
echo "+------------------------------+"; \
grep -s -i "`date --date='yesterday' '+%b %e'`" \
/var/log/auth.log /var/log/secure | \
egrep -wi 'useradd' | grep -v 'failed adding'| \
awk '{print $4,$8}' | uniq | sed 's/,/ /'; \
echo "+------------------------------+"; \
) | mail -s "$SUBJECT" "$TO"
fi

Sending HTML mail using a shell script

First you need to compose the message. The bare minimum is composed of these two headers:

MIME-Version: 1.0
Content-Type: text/html

... and the appropriate message body:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>

<p>Hello, world!</p>

</body>
</html>

Once you have it, you can pass the appropriate information to the mail command:

body = '...'

echo $body | mail \
-a "From: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com

This is an oversimplified example, since you also need to take care of charsets, encodings, maximum line length... But this is basically the idea.

Alternatively, you can write your script in Perl or PHP rather than plain shell.

Update

A shell script is basically a text file with Unix line endings that starts with a line called shebang that tells the shell what interpreter it must pass the file to, follow some commands in the language the interpreter understands and has execution permission (in Unix that's a file attribute). E.g., let's say you save the following as hello-world:

#!/bin/sh

echo Hello, world!

Then you assign execution permission:

chmod +x hello-world

And you can finally run it:

./hello-world

Whatever, this is kind of unrelated to the original question. You should get familiar with basic shell scripting before doing advanced tasks with it. Here you are a couple of links about bash, a popular shell:

http://www.gnu.org/software/bash/manual/html_node/index.html

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html



Related Topics



Leave a reply



Submit