3 # mhmail.c -- 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) using nmh post(8) or send(1).
10 # Differences from compiled mhmail:
11 # * Supports all post(8) (by default, without -profile) or send(1)
12 # (with -profile) options.
13 # * Optionally (with -profile) obeys the users profile, including
14 # AliasFile and send entries.
15 # * Adds -debug option for debugging (sending, not incorporating new mail).
18 # * set prefix properly so that it supports distcheck
19 # * support undocumented mhmail options?
21 usage='Usage: mhmail [addrs ... [switches]]
31 and all post(8)/send(1) switches'
33 #### Use autoconf to fill in bindir.
34 # This doesn't work with distcheck.
36 # exec_prefix=@exec_prefix@
39 #### Or if configure/make haven't been run yet, figure out nmhbindir at runtime.
41 @*@) nmhbindir=`dirname $0` ;;
45 #### Emulate mhmail for reading mail.
48 #### Go through all the switches so we can build the draft.
67 #### Post and send won't accept -f -or -s because they'd be
68 #### ambiguous, so no conflicts with them. And they don't have
69 #### -b or -c. For the new switches that compiled mhmail didn't
70 #### have: let -p indicate mhmail -profile, not send -port, and
71 #### let -d indicate mhmail -debug, not send -draft.
72 -b|-bo|-bod|-body) bodyarg=1 ;;
74 -d|-de|-deb|-debu|-debug) debug=echo ;;
75 -f|-fr|-fro|-from) header="${header}From:"; otherarg=1; havefrom=1 ;;
76 -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
77 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
78 -s|-su|-sub|-subj|-subje|-subjec|-subject)
79 header="${header}Subject:"; otherarg=1 ;;
80 -v|-ve|-ver|-vers|-versi|-versio|-version)
81 #### Cheat instead of using autoconf and make to fill in the version.
82 ${nmhbindir}/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
83 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
84 *) if [ ${ccarg} -eq 1 ]; then
85 cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
86 elif [ ${bodyarg} -eq 1 ]; then
89 #### Allow -body "" by using just a newline for the body.
90 [ "${body}"x = x ] && body='
92 elif [ ${otherarg} -eq 1 ]; then
93 #### Always end ${header} with a newline.
94 header="${header:+${header} }${arg}
96 elif [ ${switcharg} -eq 1 ]; then
97 postsendargs="${postsendargs:+${postsendargs} }${arg}"
100 tolist="${tolist:+${tolist}, }${arg}"
105 #### Check for at least one address and -from.
106 if [ "${tolist}"x = x ]; then
107 printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
109 if [ "${havefrom}" = 0 ]; then
110 nmhlibdir=`${nmhbindir}/mhparam libdir`
111 header="${header:+${header}}From: "\
112 `${nmhlibdir}/ap -format '%(localmbox)' 0`'
116 #### If no -body, read message from stdin the easy way.
117 if [ "${body}"x = x ]; then
118 #### This will lose any trailing newline(s), so we can't
119 #### send the message when stdin contains only newlines.
122 #### Don't allow an empty body (from stdin).
123 if [ "${body}"x = x ]; then
124 printf "empty body, message not sent\n"
128 #### Add trailing newline to body if it doesn't have one.
129 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
133 #### Set up a tmpfil and trap to remove it. send moves the file to a backup.
134 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
135 tmpfil="${tmpdir}/mhmail$$"
136 tmpfilbackup="${tmpdir}/[,#]mhmail$$"
137 trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
139 #### Put message header and body in file to supply as draft to
140 #### send/post. ${header} always ends with a newline, so this adds
141 #### the blank that separates the body.
143 printf "%s" "To: ${tolist}
146 ${body}" > ${tmpfil} || exit 1
148 if [ "${debug}" ]; then
149 printf "%s:\n" `ls -1 ${tmpfil}`
153 if [ "$use_send" -eq 0 ]; then
154 post_or_send="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
156 post_or_send="${nmhbindir}/send"
159 if $debug ${post_or_send} ${tmpfil} ${postsendargs}; then
162 printf "Letter saved in dead.letter\n"
163 #### exec skips the trap set above.
164 exec mv ${tmpfil} dead.letter