/
mail.sh
executable file
·71 lines (61 loc) · 1.74 KB
/
mail.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Author: Sai Charan (http://saicharan.in/blog)
# Distributed without warranties or conditions of any kind, either
# express or implied except those under the CC BY-NC 3.0 license.
# You may obtain a copy of the license at:
#
# http://creativecommons.org/licenses/by-nc/3.0/
# References:
# http://beingkejo.wordpress.com/2008/05/28/sendmail-in-a-shell-script/
# Temporary file for containing the mail message
tmp=/tmp/mail-body-`date +%F`;
touch $tmp && chmod 600 $tmp;
# Body header seems to need two newlines to work.
NEWLINE="
";
# Some pre-processing:
# WARNING!! Not a scalable procedure in general. DONOT emulate.
# We expect first arg to be subject and second to be body (in file). If there is
# only one arg, it may be the body. So clear that upfront.
#----Assumes that if there is just one arg, it will be the body.
# ---If two, the first has got to be the subject.
if [ "$2" ] && [ -r "$2" ]; then # File exists & is readable
BODY=$(cat $2);
rm -rf $2;
fi
if [ "$1" ] && [ -r "$1" ]; then
BODY=$(cat $1);
rm -rf $1;
else
if [ "$1" ]; then
SUBJECT=$1;
fi
fi
#Set up the various headers for sendmail to use
TO='';
CC='';
FROM='';
if [ -z "$SUBJECT" ]; then
SUBJECT='Task complete!';
fi
MIMEVersion='1.0';
CONTENTType='text/html; charset=us-ascii';
#Here write the content of your mail.
if [ -z "$BODY" ]; then
BODY="
Task done!
";
else
BODY=$NEWLINE" "$BODY" "$NEWLINE;
fi
echo Sending the mail...
echo -e "To: $TO" > $tmp;
echo -e "Cc: $CC" >> $tmp;
echo -e "From: $FROM" >> $tmp;
echo -e "Content-Type: $CONTENTType" >> $tmp;
echo -e "MIME-Version: $MIMEVersion" >> $tmp;
echo -e "Subject: $SUBJECT" >> $tmp;
echo -e "Body: $BODY" >> $tmp;
/usr/sbin/sendmail -t < $tmp;
rm -rf $tmp;
echo Done!