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@
38 nmhbindir=`dirname $0`
40 #### Or if configure/make haven't been run yet, figure out nmhbindir at runtime.
42 @*@) nmhbindir=`dirname $0` ;;
46 #### Emulate mhmail for reading mail.
49 #### Go through all the switches so we can build the draft.
68 #### Post and send won't accept -f -or -s because they'd be
69 #### ambiguous, so no conflicts with them. And they don't have
70 #### -b or -c. For the new switches that compiled mhmail didn't
71 #### have: let -p indicate mhmail -profile, not send -port, and
72 #### let -d indicate mhmail -debug, not send -draft.
73 -b|-bo|-bod|-body) bodyarg=1 ;;
75 -d|-de|-deb|-debu|-debug) debug=echo ;;
76 -f|-fr|-fro|-from) header="${header}From:"; otherarg=1; havefrom=1 ;;
77 -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
78 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
79 -s|-su|-sub|-subj|-subje|-subjec|-subject)
80 header="${header}Subject:"; otherarg=1 ;;
81 -v|-ve|-ver|-vers|-versi|-versio|-version)
82 #### Cheat instead of using autoconf and make to fill in the version.
83 ${nmhbindir}/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
84 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
85 *) if [ ${ccarg} -eq 1 ]; then
86 cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
87 elif [ ${bodyarg} -eq 1 ]; then
90 #### Allow -body "" by using just a newline for the body.
91 [ "${body}"x = x ] && body='
93 elif [ ${otherarg} -eq 1 ]; then
94 #### Always end ${header} with a newline.
95 header="${header:+${header} }${arg}
97 elif [ ${switcharg} -eq 1 ]; then
98 postsendargs="${postsendargs:+${postsendargs} }${arg}"
101 tolist="${tolist:+${tolist}, }${arg}"
106 #### Check for at least one address and -from.
107 if [ "${tolist}"x = x ]; then
108 printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
110 if [ "${havefrom}" = 0 ]; then
111 nmhlibdir=`${nmhbindir}/mhparam libdir`
112 header="${header:+${header}}From: "\
113 `${nmhlibdir}/ap -format '%(localmbox)' 0`'
117 #### If no -body, read message from stdin the easy way.
118 if [ "${body}"x = x ]; then
119 #### This will lose any trailing newline(s), so we can't
120 #### send the message when stdin contains only newlines.
123 #### Don't allow an empty body (from stdin).
124 if [ "${body}"x = x ]; then
125 printf "empty body, message not sent\n"
129 #### Add trailing newline to body if it doesn't have one.
130 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
134 #### Set up a tmpfil and trap to remove it. send moves the file to a backup.
135 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
136 tmpfil="${tmpdir}/mhmail$$"
137 tmpfilbackup="${tmpdir}/[,#]mhmail$$"
138 trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
140 #### Put message header and body in file to supply as draft to
141 #### send/post. ${header} always ends with a newline, so this adds
142 #### the blank that separates the body.
144 printf "%s" "To: ${tolist}
147 ${body}" > ${tmpfil} || exit 1
149 if [ "${debug}" ]; then
150 printf "%s:\n" `ls -1 ${tmpfil}`
154 if [ "$use_send" -eq 0 ]; then
155 post_or_send="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
157 post_or_send="${nmhbindir}/send"
160 if $debug ${post_or_send} ${tmpfil} ${postsendargs}; then
163 printf "Letter saved in dead.letter\n"
164 #### exec skips the trap set above.
165 exec mv ${tmpfil} dead.letter