3 # mhmail -- simple mail program
5 # This code is Copyright (c) 2012, by the authors of nmh. See the
6 # COPYRIGHT file in the root directory of the nmh distribution for
7 # complete copyright information.
9 # Emulation of compiled mhmail(1), with these differences:
10 # * Instead of silently not sending an empty message, notifies user
11 # "mhmail: empty message not sent, use -body '' to force."
12 # * The compiled mhmail dropped a trailing newline from the -body argument.
13 # * Supports all post(8) (by default, without -profile) or send(1)
14 # (with -profile) options.
15 # * Optionally (with -profile) obeys the users profile, including
16 # AliasFile and send entries.
17 # * Adds -debug option for debugging (sending, not incorporating new mail).
18 # * Drops support for undocumented -queue option.
21 # * add support for undocumented -resent compiled mhmail switch
22 # * add -attach file ... switch
24 usage='Usage: mhmail [addrs ... [switches]]
34 and all post(8)/send(1) switches'
37 nmhbindir=`cd "${bindir}" && pwd`
40 #### Emulate mhmail for reading mail.
41 exec "${nmhbindir}"/inc
43 #### Go through all the switches so we can build the draft.
63 #### Post and send won't accept -f -or -s because they'd be
64 #### ambiguous, so no conflicts with them. And they don't have
65 #### -b or -c. For the new switches that compiled mhmail didn't
66 #### have: let -p indicate mhmail -profile, not send -port, and
67 #### let -d indicate mhmail -debug, not send -draft.
68 -b|-bo|-bod|-body) bodyarg=1 ;;
70 -d|-de|-deb|-debu|-debug) debug=echo ;;
71 -f|-fr|-fro|-from) fromarg=1 ;;
72 -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
73 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
74 -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
75 -v|-ve|-ver|-vers|-versi|-versio|-version)
76 #### Cheat instead of using autoconf and make to fill in the version.
77 "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
78 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
79 *) if [ ${bodyarg} -eq 1 ]; then
82 #### Allow -body "" by using just a newline for the body.
83 [ "${body}"x = x ] && body='
85 elif [ ${fromarg} -eq 1 ]; then
86 from="${arg}"; fromarg=0
87 elif [ ${subjectarg} -eq 1 ]; then
88 subject="${arg}"; subjectarg=0
89 elif [ ${switcharg} -eq 1 ]; then
90 postsendargs="${postsendargs:+${postsendargs} }${arg}"
91 elif [ ${ccarg} -eq 1 ]; then
92 #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
93 cclist="${cclist:+${cclist}, }${arg}"
96 tolist="${tolist:+${tolist}, }${arg}"
101 #### Check for at least one address and -from.
102 if [ "${tolist}"x = x ]; then
103 printf "mhmail: usage: mhmail addrs ... [switches]\n"
106 if [ "${from}"x = x ]; then
107 nmhlibdir=`${nmhbindir}/mhparam libdir`/
108 #### If nmhlibdir isn't right, assume that the nmh lib dir is on the PATH.
109 [ -x "${nmhlibdir}ap" ] || nmhlibdir=
110 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
114 header="To: ${tolist}
116 [ "${cclist}"x = x ] || header="${header}Cc: ${cclist}
118 [ "${subject}"x = x ] || header="${header}Subject: ${subject}
120 [ "${from}"x = x ] || header="${header}From: ${from}
125 #### Set up a file to supply as a draft to send/post. And set a
126 #### trap to remove it. send moves the file to a backup, so it will
127 #### remove that, too.
129 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
130 tmpfil="${tmpdir}/mhmail$$"
131 tmpfilbackup="${tmpdir}/[,#]mhmail$$"
132 trap 'rm -f '"${tmpfil}"' '"${tmpfilbackup}" EXIT
134 if [ "${body}"x = x ]; then
135 #### First put message header in the file. cat >> handles blank
136 #### lines better than body=`cat`.
137 printf "%s" "${header}" > "${tmpfil}" || exit 1
139 tmpfile_size_before=`wc -c "${tmpfil}"`
140 #### Now grab the body from stdin.
142 tmpfile_size_after=`wc -c "${tmpfil}"`
144 #### Don't allow an empty body (from stdin). Use string
145 #### comparison so we don't have to strip the filename, etc.
146 if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
147 printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
151 #### Add trailing newline to body if it doesn't have one.
152 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
155 #### Put message header and body in the file.
156 printf "%s" "${header}${body}" > "${tmpfil}" || exit 1
159 if [ "${debug}" ]; then
160 printf "%s:\n" `ls -1 "${tmpfil}"`
164 if [ "$use_send" -eq 0 ]; then
165 post_or_send=`${nmhbindir}/mhparam postproc`
167 post_or_send="${nmhbindir}/send"
170 if $debug "${post_or_send}" "${tmpfil}" ${postsendargs}; then
173 printf "Letter saved in dead.letter\n"
174 #### exec skips the trap set above.
175 exec mv "${tmpfil}" dead.letter