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 # * Adds -send/-nosend, -header-field, and -attach options.
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 # * Instead of silently not sending an empty message, notifies user
16 # "mhmail: empty message not sent, use -body '' to force."
17 # * The compiled mhmail dropped a trailing newline from the -body argument.
18 # * Drops support for undocumented -queue option.
20 usage='Usage: mhmail [addrs ... [switches]]
22 -at(tach) file [-at(tach) file] ...
26 -hea(der-field) name:field [-hea(der-field) name:field] ...
34 and all post(8)/send(1) switches'
37 nmhbindir=`cd "${bindir}" && pwd`
40 #### Emulate mhmail for reading mail.
41 exec "${nmhbindir}"/inc
43 #### Go through all the switches so we can build the draft.
46 attach_send_switch_added=0
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. -attach
73 #### masks the send(1) -attach switch.
74 -at|-att|-atta|-attac|-attach)
77 if [ ${attach_send_switch_added} -eq 0 ]; then
78 #### Override any send -attach switch in user's profile.
80 "${postsendargs:+${postsendargs} }-attach Nmh-Attachment"
81 attach_send_switch_added=1
84 -b|-bo|-bod|-body) bodyarg=1 ;;
86 -f|-fr|-fro|-from) fromarg=1 ;;
87 -hea|-head|-heade|-header|-header-|-header-f|-header-fi|-header-fie|-header-fiel|-header-field) headerfieldarg=1 ;;
88 -hel|-help) printf "%s\n" "${usage}"; exit ;;
89 -nose|-nosen|-nosend) sendsw=0 ;;
90 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
91 -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
93 -r|-re|-res|-rese|-resen|-resent) resent=1 ;;
94 -se|-sen|-send) sendsw=1 ;;
95 -su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
96 -v|-ve|-ver|-vers|-versi|-versio|-version)
97 #### Cheat instead of using autoconf and make to fill in the version.
98 "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
99 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
100 *) if [ ${bodyarg} -eq 1 ]; then
104 #### Allow -body "" by using just a newline for the body.
105 [ "${body}"x = x ] && body='
107 elif [ ${fromarg} -eq 1 ]; then
110 elif [ ${subjectarg} -eq 1 ]; then
113 elif [ ${attacharg} -eq 1 ]; then
114 headerfieldlist="${headerfieldlist:+${headerfieldlist}}\
115 Nmh-Attachment: ${arg}
118 elif [ ${headerfieldarg} -eq 1 ]; then
119 #### It's not strictly necessary to have one space after
120 #### the : that separates the header field name from the
121 #### body, but do it to avoid surprising someone.
122 add=`printf "${arg}" | sed -e 's/:/: /' -e 's/: /: /'`
123 headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
126 elif [ ${switcharg} -eq 1 ]; then
127 postsendargs="${postsendargs:+${postsendargs} }${arg}"
128 elif [ ${ccarg} -eq 1 ]; then
129 #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
130 cclist="${cclist:+${cclist}, }${arg}"
133 tolist="${tolist:+${tolist}, }${arg}"
138 #### Check for at least one address and -from.
139 if [ "${tolist}"x = x ]; then
140 printf "mhmail: usage: mhmail addrs ... [switches]\n"
143 if [ "${from}"x = x ]; then
144 nmhlibdir=`${nmhbindir}/mhparam libdir`/
145 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
149 [ ${resent} -eq 0 ] && prefix= || prefix='Resent-'
150 header="${prefix}To: ${tolist}
152 [ "${cclist}"x = x ] || header="${header}${prefix}Cc: ${cclist}
154 [ "${subject}"x = x ] || header="${header}${prefix}Subject: ${subject}
156 [ "${from}"x = x ] || header="${header}${prefix}From: ${from}
159 if [ "${headerfieldlist}" ]; then
160 header="${header}${headerfieldlist}";
163 #### Set up a file to supply as a draft to send/post. And set a
164 #### trap to remove it. send moves the file to a backup, so it will
165 #### remove that, too.
167 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
168 tmpfile="${tmpdir}/mhmail$$"
169 tmpfilebackup="${tmpdir}/[,#]mhmail$$"
173 if [ ${resent} -eq 0 ]; then
174 #### Add blank line after header if not resending.
177 message_file="${tmpfile}"
179 if [ ${use_send} -eq 0 ]; then
180 postsendargs="${postsendargs:+${postsendargs} }-dist"
181 message_file="${tmpfile}"
183 #### When resending with send, tmpfile will just contain the
184 #### Resent- header fields. "${tmpfileresent}" will contain
185 #### the message that is being resent.
186 tmpfileresent="${tmpdir}/mhmail-resent$$"
187 mhdist=1; export mhdist
188 mhaltmsg=${tmpfileresent}; export mhaltmsg
189 message_file="${tmpfileresent}"
190 printf "" >"${message_file}" || exit 2
194 trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
196 if [ "${body}"x = x ]; then
197 #### First put message header in the file.
198 printf "%s" "${header}" >"${tmpfile}" || exit 2
200 tmpfile_size_before=`wc -c "${message_file}"`
201 #### Now grab the body from stdin. cat >> handles blank lines
202 #### better than body=`cat`.
203 cat >>"${message_file}" || exit 2
204 tmpfile_size_after=`wc -c "${message_file}"`
206 #### Don't allow an empty body (from stdin). Use string
207 #### comparison so we don't have to strip the filename, etc.
208 if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
209 printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
213 #### Add trailing newline to body if it doesn't have one.
214 if [ `tail -n 1 "${message_file}" | wc -l` -ne 1 ]; then
215 printf "\n" >>"${message_file}" || exit 2
218 #### Add trailing newline to body if it doesn't have one.
219 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
222 if [ "${tmpfileresent}" ]; then
223 #### Put just the new message header in the file.
224 printf "%s" "${header}" >"${tmpfile}" || exit 2
225 #### and the body in the file to resend.
226 printf "${body}" >"${tmpfileresent}" || exit 2
228 #### Put message header and body in the file.
229 printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
233 if [ ${sendsw} -eq 0 ]; then
236 if [ ${use_send} -eq 0 ]; then
237 post_or_send=`${nmhbindir}/mhparam postproc`
239 post_or_send="${nmhbindir}/send"
242 if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
245 printf "Letter saved in dead.letter\n"
246 #### exec skips the trap set above.
247 [ "${tmpfileresent}" ] && rm -f "${tmpfileresent}"
248 exec mv "${tmpfile}" dead.letter