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 send(1) (with -profile) or post(8) (without -profile) options.
12 # * Optionally (with -profile) obeys the users profile, including
13 # AliasFile and send entries.
14 # * Adds -debug option for debugging (sending, not incorporating new mail).
17 # * update mhmail man page
19 # * integrate into autoconf
20 # * support undocumented mhmail options?
22 usage='Usage: mhmail [addrs ... [switches]]
32 and all send(1)/post(8) switches'
34 #### Use autoconf to fill in bindir.
37 #### Or if configure hasn't been run yet, figure out nmhbindir at runtime.
39 @*@) nmhbindir=`dirname $0` ;;
43 #### Emulate mhmail for reading mail.
46 #### Go through all the switches so we can build the draft.
65 #### Send and post wouldn't accept -f -or -s because they'd be
66 #### ambiguous, so no conflicts with them. And they don't have
67 #### -b or -c. For the new switches that compiled mhmail didn't
68 #### have: let -p indicate mhmail -profile, not send -port, and
69 #### let -d indicate mhmail -debug, not send -draft.
70 -b|-bo|-bod|-body) bodyarg=1 ;;
72 -d|-de|-deb|-debu|-debug) debug=echo ;;
73 -f|-fr|-fro|-from) header="${header}From:"; otherarg=1; havefrom=1 ;;
74 -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
75 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
76 -s|-su|-sub|-subj|-subje|-subjec|-subject)
77 header="${header}Subject:"; otherarg=1 ;;
78 -v|-ve|-ver|-vers|-versi|-versio|-version)
79 #### Cheat instead of using autoconf and make to fill in the version.
80 ${nmhbindir}/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
81 -*) sendpostargs="${sendpostargs:+${sendpostargs} }${arg}"; switcharg=1 ;;
82 *) if [ ${ccarg} -eq 1 ]; then
83 cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
84 elif [ ${bodyarg} -eq 1 ]; then
85 body=${arg}; bodyarg=0
86 elif [ ${otherarg} -eq 1 ]; then
87 #### Always end ${header} with a newline.
88 header="${header:+${header} }${arg}
90 elif [ ${switcharg} -eq 1 ]; then
91 sendpostargs="${sendpostargs:+${sendpostargs} }${arg}"
94 tolist="${tolist:+${tolist}, }${arg}"
99 #### Check for at least one address and -from.
100 if [ "${tolist}"x = x ]; then
101 printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
103 if [ "${havefrom}" = 0 ]; then
104 nmhlibdir=`${nmhbindir}/mhparam libdir`
105 header="${header:+${header}
106 }From: "`${nmhlibdir}/ap -format '%(localmbox)' 0`"
110 #### Set up a tmpfil and trap to remove it. send moves the file to a backup.
111 tmpdir=${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}
112 tmpfil=${tmpdir}/mhmail$$
113 tmpfilbackup=${tmpdir}/[,#]mhmail$$
114 trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
116 #### Put message header and body in file to supply as draft to
117 #### send/post. ${header} always ends with a newline, so this adds
118 #### the blank that separates the body.
120 printf "%s\n" "To: ${tolist}
123 ${body}" > ${tmpfil} || exit 1
125 if [ "${debug}" ]; then
126 printf "%s:\n" `ls -1 ${tmpfil}`
130 if [ "$use_send" -eq 1 ]; then
131 send_or_post="${nmhbindir}/send"
133 send_or_post="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
136 $debug ${send_or_post} ${tmpfil} ${sendpostargs}