3 # mhmail -- 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), with these differences:
10 # * Instead of silently not sending an empty message, notifies user
11 # "mhmail: empty message not sent, use -body '' to force."
12 # * The compiled mhmail dropped a trailing newline from the -body argument.
13 # * Supports all post(8) (by default, without -profile) or send(1)
14 # (with -profile) options.
15 # * Optionally (with -profile) obeys the users profile, including
16 # AliasFile and send entries.
17 # * Adds -send/-nosend and -header-field options.
18 # * Drops support for undocumented -queue option.
21 # * add -attach file ... switch
23 usage='Usage: mhmail [addrs ... [switches]]
28 -hea(der-field) name:field [-hea(der-field) name:field] ...
36 and all post(8)/send(1) switches'
39 nmhbindir=`cd "${bindir}" && pwd`
42 #### Emulate mhmail for reading mail.
43 exec "${nmhbindir}"/inc
45 #### Go through all the switches so we can build the draft.
64 -*) switcharg=0; headerfieldarg=0 ;;
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, -c, or -r. For the new switches that compiled mhmail
71 #### didn't have: let -p indicate mhmail -profile, not send
72 #### -port. -send masks the send(1) -send switch.
73 -b|-bo|-bod|-body) bodyarg=1 ;;
75 -f|-fr|-fro|-from) fromarg=1 ;;
76 -hea|-head|-heade|-header|-header-|-header-f|-header-fi|-header-fie|-header-fiel|-header-field) headerfieldarg=1 ;;
77 -hel|-help) printf "%s\n" "${usage}"; exit ;;
78 -nose|-nosen|-nosend) sendsw=0 ;;
79 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
80 -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
82 -r|-re|-res|-rese|-resen|-resent) resent=1 ;;
83 -se|-sen|-send) sendsw=1 ;;
84 -su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
85 -v|-ve|-ver|-vers|-versi|-versio|-version)
86 #### Cheat instead of using autoconf and make to fill in the version.
87 "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
88 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
89 *) if [ ${bodyarg} -eq 1 ]; then
93 #### Allow -body "" by using just a newline for the body.
94 [ "${body}"x = x ] && body='
96 elif [ ${fromarg} -eq 1 ]; then
99 elif [ ${subjectarg} -eq 1 ]; then
102 elif [ ${headerfieldarg} -eq 1 ]; then
103 #### It's not strictly necessary to have one space after
104 #### the : that separates the header field name from the
105 #### body, but do it to avoid surprising someone.
106 add=`printf "${arg}" | sed -e 's/:/: /' -e 's/: /: /'`
107 headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
110 elif [ ${switcharg} -eq 1 ]; then
111 postsendargs="${postsendargs:+${postsendargs} }${arg}"
112 elif [ ${ccarg} -eq 1 ]; then
113 #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
114 cclist="${cclist:+${cclist}, }${arg}"
117 tolist="${tolist:+${tolist}, }${arg}"
122 #### Check for at least one address and -from.
123 if [ "${tolist}"x = x ]; then
124 printf "mhmail: usage: mhmail addrs ... [switches]\n"
127 if [ "${from}"x = x ]; then
128 nmhlibdir=`${nmhbindir}/mhparam libdir`/
129 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
133 [ ${resent} -eq 0 ] && prefix= || prefix='Resent-'
134 header="${prefix}To: ${tolist}
136 [ "${cclist}"x = x ] || header="${header}${prefix}Cc: ${cclist}
138 [ "${subject}"x = x ] || header="${header}${prefix}Subject: ${subject}
140 [ "${from}"x = x ] || header="${header}${prefix}From: ${from}
143 if [ "${headerfieldlist}" ]; then
144 header="${header}${headerfieldlist}";
147 #### Set up a file to supply as a draft to send/post. And set a
148 #### trap to remove it. send moves the file to a backup, so it will
149 #### remove that, too.
151 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
152 tmpfile="${tmpdir}/mhmail$$"
153 tmpfilebackup="${tmpdir}/[,#]mhmail$$"
157 if [ ${resent} -eq 0 ]; then
158 #### Add blank line after header if not resending.
161 message_file="${tmpfile}"
163 if [ ${use_send} -eq 0 ]; then
164 postsendargs="${postsendargs:+${postsendargs} }-dist"
165 message_file="${tmpfile}"
167 #### When resending with send, tmpfile will just contain the
168 #### Resent- header fields. "${tmpfileresent}" will contain
169 #### the message that is being resent.
170 tmpfileresent="${tmpdir}/mhmail-resent$$"
171 mhdist=1; export mhdist
172 mhaltmsg=${tmpfileresent}; export mhaltmsg
173 message_file="${tmpfileresent}"
174 printf "" >"${message_file}" || exit 2
178 trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
180 if [ "${body}"x = x ]; then
181 #### First put message header in the file.
182 printf "%s" "${header}" >"${tmpfile}" || exit 2
184 tmpfile_size_before=`wc -c "${message_file}"`
185 #### Now grab the body from stdin. cat >> handles blank lines
186 #### better than body=`cat`.
187 cat >>"${message_file}" || exit 2
188 tmpfile_size_after=`wc -c "${message_file}"`
190 #### Don't allow an empty body (from stdin). Use string
191 #### comparison so we don't have to strip the filename, etc.
192 if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
193 printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
197 #### Add trailing newline to body if it doesn't have one.
198 if [ `tail -n 1 "${message_file}" | wc -l` -ne 1 ]; then
199 printf "\n" >>"${message_file}" || exit 2
202 #### Add trailing newline to body if it doesn't have one.
203 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
206 if [ "${tmpfileresent}" ]; then
207 #### Put just the new message header in the file.
208 printf "%s" "${header}" >"${tmpfile}" || exit 2
209 #### and the body in the file to resend.
210 printf "${body}" >"${tmpfileresent}" || exit 2
212 #### Put message header and body in the file.
213 printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
217 if [ ${sendsw} -eq 0 ]; then
220 if [ ${use_send} -eq 0 ]; then
221 post_or_send=`${nmhbindir}/mhparam postproc`
223 post_or_send="${nmhbindir}/send"
226 if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
229 printf "Letter saved in dead.letter\n"
230 #### exec skips the trap set above.
231 [ "${tmpfileresent}" ] && rm -f "${tmpfileresent}"
232 exec mv "${tmpfile}" dead.letter