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 # * update mhmail man page
20 # * integrate into autoconf
21 # * support undocumented mhmail options?
23 usage='Usage: mhmail [addrs ... [switches]]
33 and all post(8)/send(1) switches'
35 #### Use autoconf to fill in bindir.
38 #### Or if configure hasn't been run yet, figure out nmhbindir at runtime.
40 @*@) nmhbindir=`dirname $0` ;;
44 #### Emulate mhmail for reading mail.
47 #### Go through all the switches so we can build the draft.
66 #### Post and send won't accept -f -or -s because they'd be
67 #### ambiguous, so no conflicts with them. And they don't have
68 #### -b or -c. For the new switches that compiled mhmail didn't
69 #### have: let -p indicate mhmail -profile, not send -port, and
70 #### let -d indicate mhmail -debug, not send -draft.
71 -b|-bo|-bod|-body) bodyarg=1 ;;
73 -d|-de|-deb|-debu|-debug) debug=echo ;;
74 -f|-fr|-fro|-from) header="${header}From:"; otherarg=1; havefrom=1 ;;
75 -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
76 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
77 -s|-su|-sub|-subj|-subje|-subjec|-subject)
78 header="${header}Subject:"; otherarg=1 ;;
79 -v|-ve|-ver|-vers|-versi|-versio|-version)
80 #### Cheat instead of using autoconf and make to fill in the version.
81 ${nmhbindir}/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
82 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
83 *) if [ ${ccarg} -eq 1 ]; then
84 cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
85 elif [ ${bodyarg} -eq 1 ]; then
86 body=${arg}; bodyarg=0
87 #### Allow -body "" by using just a newline for the body.
88 [ "${body}"x = x ] && body="
90 elif [ ${otherarg} -eq 1 ]; then
91 #### Always end ${header} with a newline.
92 header="${header:+${header} }${arg}
94 elif [ ${switcharg} -eq 1 ]; then
95 postsendargs="${postsendargs:+${postsendargs} }${arg}"
98 tolist="${tolist:+${tolist}, }${arg}"
103 #### Check for at least one address and -from.
104 if [ "${tolist}"x = x ]; then
105 printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
107 if [ "${havefrom}" = 0 ]; then
108 nmhlibdir=`${nmhbindir}/mhparam libdir`
109 header="${header:+${header}}From: "\
110 `${nmhlibdir}/ap -format '%(localmbox)' 0`"
114 #### If no -body, read message from stdin the easy way.
115 [ "${body}"x = x ] && body=`cat`
116 #### Don't allow an empty body (from stdin).
117 [ "${body}"x = x ] && exit 1
119 #### Set up a tmpfil and trap to remove it. send moves the file to a backup.
120 tmpdir=${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}
121 tmpfil=${tmpdir}/mhmail$$
122 tmpfilbackup=${tmpdir}/[,#]mhmail$$
123 trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
125 #### Put message header and body in file to supply as draft to
126 #### send/post. ${header} always ends with a newline, so this adds
127 #### the blank that separates the body.
129 printf "%s" "To: ${tolist}
132 ${body}" > ${tmpfil} || exit 1
134 if [ "${debug}" ]; then
135 printf "%s:\n" `ls -1 ${tmpfil}`
139 if [ "$use_send" -eq 0 ]; then
140 post_or_send="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
142 post_or_send="${nmhbindir}/send"
145 if $debug ${post_or_send} ${tmpfil} ${postsendargs}; then
148 #### exec disables the trap set above.
149 printf "Letter saved in dead.letter\n"
150 exec mv ${tmpfil} dead.letter