1 | #!/bin/sh
|
---|
2 |
|
---|
3 | # This is a shell script to emulate the standard mail(1) aka mailx(1)
|
---|
4 | # aka Mail(1) tool usage for sending emails. This shell script is not
|
---|
5 | # a client for checking or reading mail. Also, this version of mail
|
---|
6 | # does not touch any mail boxes.
|
---|
7 |
|
---|
8 | # Copyright (c) 2003 by Jeremy C. Reed
|
---|
9 | # This software is provided "as is" without any warranties. You may
|
---|
10 | # redistribute and use this source, with or without modification,
|
---|
11 | # as long as you include this copyright and disclaimer.
|
---|
12 |
|
---|
13 | # To do: some switches to consider:
|
---|
14 | # -v display details of delivery
|
---|
15 | # -E do not send empty messages
|
---|
16 | # -c send carbon copies to users
|
---|
17 | # -b send blind carbon copies
|
---|
18 |
|
---|
19 | # todo: test if read can read in long enough lines
|
---|
20 | # some shells may have limit of 256 characters per read?
|
---|
21 |
|
---|
22 | SENDMAIL=sendmail # set to path of your sendmail(8) command
|
---|
23 | SENDMAIL_ARGS="-t -i" # -i is so dot doesn't terminate message
|
---|
24 | # -t is to get recipients from headers
|
---|
25 |
|
---|
26 | unset subject
|
---|
27 | unset to_addr
|
---|
28 | dontsendempty=0
|
---|
29 |
|
---|
30 | about ()
|
---|
31 | {
|
---|
32 | echo "This is mailx.sh version 0.03. This is simple, shell script"
|
---|
33 | echo "to implement mailx(1) functionality for sending mail. It does"
|
---|
34 | echo "not provide an interface for checking or reading email messages."
|
---|
35 | echo
|
---|
36 |
|
---|
37 | # maybe do this without forking a command
|
---|
38 | mailname=$( basename $0 )
|
---|
39 |
|
---|
40 | if [ -n "$1" ] ; then
|
---|
41 | echo "$mailname: $1" 1>&2
|
---|
42 | fi
|
---|
43 |
|
---|
44 | echo "Usage: $mailname [-s subject] to-addr ..." 1>&2
|
---|
45 |
|
---|
46 | if [ -n "$1" ] ; then
|
---|
47 | if [ -n "$2" ] ; then
|
---|
48 | exit $2
|
---|
49 | fi
|
---|
50 | exit 100
|
---|
51 | fi
|
---|
52 |
|
---|
53 | exit 0
|
---|
54 | }
|
---|
55 |
|
---|
56 | if [ $# -eq 0 ] ; then
|
---|
57 | about "Sorry, this is not a mail reader." 1
|
---|
58 | fi
|
---|
59 |
|
---|
60 | while [ -n "$1" ] ; do
|
---|
61 | case "$1" in
|
---|
62 | -*)
|
---|
63 | if [ "$1" = "-s" ] ; then
|
---|
64 | if [ -z "$2" ] ; then
|
---|
65 | about "The \"-s\" option requires an argument." 4
|
---|
66 | fi
|
---|
67 | subject="$2"
|
---|
68 | shift
|
---|
69 | elif [ "$1" = "-u" -o "$1" = "-f" ] ; then
|
---|
70 | about "Sorry, this is not a mail reader." 2
|
---|
71 | # elif [ "$1" = "-E" ] ; then
|
---|
72 | # this probably needs to have entire message saved to temp file first
|
---|
73 | # dontsendempty=1
|
---|
74 | else
|
---|
75 | # about "unknown option -- $1" 3
|
---|
76 | about "Switch \"$1\" is not implemented." 3
|
---|
77 | fi
|
---|
78 | ;;
|
---|
79 | *) if [ -n "$to_addr" ] ; then
|
---|
80 | to_addr="${to_addr}, $1"
|
---|
81 | else
|
---|
82 | to_addr="$1"
|
---|
83 | fi
|
---|
84 | ;;
|
---|
85 | esac
|
---|
86 | shift
|
---|
87 | done
|
---|
88 |
|
---|
89 |
|
---|
90 | if [ -t 0 ] ; then # no piped in standard input
|
---|
91 | if [ -z "$subject" ] ; then
|
---|
92 | echo -n "Subject: "
|
---|
93 | OLD_IFS=$IFS
|
---|
94 | IFS=""
|
---|
95 | read -r subject
|
---|
96 | IFS=$OLD_IFS
|
---|
97 | fi
|
---|
98 | fi
|
---|
99 |
|
---|
100 | if [ -z "$to_addr" ] ; then
|
---|
101 | about "No recipients specified." 6
|
---|
102 | # about "You must specify recipients." 6
|
---|
103 | fi
|
---|
104 |
|
---|
105 | # generate SMTP headers
|
---|
106 |
|
---|
107 | # mail(1) doesn't seem to set date
|
---|
108 | #date +"Date: %a, %e %b %G %T %z"
|
---|
109 | ## maybe pipe though sed to get rid of extra space
|
---|
110 | ## before single digit day of month?
|
---|
111 |
|
---|
112 | {
|
---|
113 | # if [ -n "$to_addr" ] ; then
|
---|
114 | echo "To: ${to_addr}"
|
---|
115 | # fi
|
---|
116 |
|
---|
117 | if [ -n "$subject" ] ; then
|
---|
118 | echo "Subject: $subject"
|
---|
119 | fi
|
---|
120 |
|
---|
121 | # end of headers
|
---|
122 | echo ""
|
---|
123 |
|
---|
124 | # output
|
---|
125 | line_count=0
|
---|
126 | OLD_IFS=$IFS
|
---|
127 | IFS=""
|
---|
128 | while read -r line ; do
|
---|
129 | # finish if manual, non-piped input is a period on a line by itself
|
---|
130 | if [ "$line" = "." -a -t 0 ] ; then
|
---|
131 | break
|
---|
132 | fi
|
---|
133 | echo "$line"
|
---|
134 | line_count=$(( line_count + 1 ))
|
---|
135 | done
|
---|
136 |
|
---|
137 | IFS=$OLD_IFS
|
---|
138 |
|
---|
139 | if [ -t 0 ] ; then # no piped in standard input
|
---|
140 | echo "EOT" 1>&2
|
---|
141 | fi
|
---|
142 |
|
---|
143 | # later add switch to not send mail by exiting
|
---|
144 | if [ "$line_count" -eq 0 ] ; then
|
---|
145 | # if [ "$dontsendempty" -eq 1 ] ; then
|
---|
146 | # todo this, have it save to temp file and then send to sendmail as needed
|
---|
147 | # # No message, so just exit cleanly
|
---|
148 | # exit 0
|
---|
149 | # fi
|
---|
150 | if [ -z "$subject" ] ; then
|
---|
151 | echo "No message, no subject; hope that's ok" 1>&2
|
---|
152 | else
|
---|
153 | echo "Null message body; hope that's ok" 1>&2
|
---|
154 | fi
|
---|
155 | fi
|
---|
156 |
|
---|
157 | } | $SENDMAIL $SENDMAIL_ARGS
|
---|
158 |
|
---|