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 send(1) or post(8).
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 send(1)/post(8) 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 #### Send and post wouldn'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 -*) sendpostargs="${sendpostargs:+${sendpostargs} }${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 elif [ ${otherarg} -eq 1 ]; then
88 #### Always end ${header} with a newline.
89 header="${header:+${header} }${arg}
91 elif [ ${switcharg} -eq 1 ]; then
92 sendpostargs="${sendpostargs:+${sendpostargs} }${arg}"
95 tolist="${tolist:+${tolist}, }${arg}"
100 #### Check for at least one address and -from.
101 if [ "${tolist}"x = x ]; then
102 printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
104 if [ "${havefrom}" = 0 ]; then
105 nmhlibdir=`${nmhbindir}/mhparam libdir`
106 header="${header:+${header}}From: "\
107 `${nmhlibdir}/ap -format '%(localmbox)' 0`"
111 #### If no -body, read message from stdin the easy way.
112 if [ "${body}"x = x ]; then
116 #### Set up a tmpfil and trap to remove it. send moves the file to a backup.
117 tmpdir=${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}
118 tmpfil=${tmpdir}/mhmail$$
119 tmpfilbackup=${tmpdir}/[,#]mhmail$$
120 trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
122 #### Put message header and body in file to supply as draft to
123 #### send/post. ${header} always ends with a newline, so this adds
124 #### the blank that separates the body.
126 printf "%s\n" "To: ${tolist}
129 ${body}" > ${tmpfil} || exit 1
131 if [ "${debug}" ]; then
132 printf "%s:\n" `ls -1 ${tmpfil}`
136 if [ "$use_send" -eq 1 ]; then
137 send_or_post="${nmhbindir}/send"
139 send_or_post="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
142 $debug ${send_or_post} ${tmpfil} ${sendpostargs}