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, -headerfield, 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(derfield) name:value [-hea(derfield) name:value] ...
34 and all post(8)/send(1) switches'
37 nmhbindir=`cd "${bindir}" && pwd`
39 #### Checks for missing mandatory arguments.
41 if [ ${attacharg} -eq 1 ]; then
42 printf "mhmail: missing argument to -attach\n"; exit 1
43 elif [ ${bodyarg} -eq 1 ]; then
44 printf "mhmail: missing argument to -body\n"; exit 1
45 elif [ ${ccarg} -eq 1 -a "${cclist}"x = x ]; then
46 printf "mhmail: missing argument to -cc\n"; exit 1
47 elif [ ${fromarg} -eq 1 ]; then
48 printf "mhmail: missing argument to -from\n"; exit 1
49 elif [ ${headerfieldarg} -eq 1 ]; then
50 printf "mhmail: missing argument to -headerfield\n"; exit 1
51 elif [ ${subjectarg} -eq 1 ]; then
52 printf "mhmail: missing argument to -subject\n"; exit 1
57 #### Emulate mhmail for reading mail.
58 exec "${nmhbindir}"/inc
60 #### Go through all the switches so we can build the draft.
61 tolist= ## To: addresses
62 attacharg=0 ## whether currently handling -attach
63 attach_send_switch_added=0 ## whether added "-attach Nmh-Attachment" switch
64 body= ## contents of the message body
65 bodyarg=0 ## whether currently handling -body
66 cclist= ## Cc: addresses
67 ccarg=0 ## whether currently handling -cc
68 from= ## From: contents
69 fromarg=0 ## whether currently handling -from
70 headerfieldlist= ## header fields to be added to draft
71 headerfieldarg=0 ## whether currently handling -headerfield
72 mhmailswitch=0 ## whether currently handling any mhmail switch
73 subject= ## Subject: contents
74 subjectarg=0 ## whether currently handling -subject
75 resent=0 ## whether resending
76 postsendargs= ## switches to pass on to post or send
77 post_send_switch_arg=0 ## whether currently handling a post/send switch
78 use_send=0 ## use post (default) or send (-profile)
79 sendsw=1 ## to send (default) or not to send
82 #### Post and send won't accept -f -or -s because they'd be
83 #### ambiguous, so no conflicts with them. And they don't have
84 #### -b, -c, -r, -t. For the new switches that compiled mhmail
85 #### didn't have: let -p indicate mhmail -profile, not send
86 #### -port. -send masks the send(1) -send switch. -attach
87 #### masks the send(1) -attach switch.
88 -at|-att|-atta|-attac|-attach)
92 if [ ${attach_send_switch_added} -eq 0 ]; then
93 #### Override any send -attach switch in user's profile.
95 "${postsendargs:+${postsendargs} }-attach Nmh-Attachment"
96 attach_send_switch_added=1
98 -b|-bo|-bod|-body) mhmailswitch=1; bodyarg=1 ;;
99 -c|-cc) mhmailswitch=1; ccarg=1 ;;
100 -f|-fr|-fro|-from) mhmailswitch=1; fromarg=1 ;;
101 -hea|-head|-heade|-header|-headerf|-headerfi|-headerfie|-headerfiel|\
102 -headerfield) mhmailswitch=1; headerfieldarg=1 ;;
103 -hel|-help) printf "%s\n" "${usage}"; exit ;;
104 -nose|-nosen|-nosend) mhmailswitch=1; sendsw=0 ;;
105 -p|-pr|-pro|-prof|-profi|-profil|-profile) mhmailswitch=1; use_send=1 ;;
106 -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
108 -r|-re|-res|-rese|-resen|-resent) mhmailswitch=1; resent=1 ;;
109 -se|-sen|-send) mhmailswitch=1; sendsw=1 ;;
110 -su|-sub|-subj|-subje|-subjec|-subject) mhmailswitch=1; subjectarg=1 ;;
111 -v|-ve|-ver|-vers|-versi|-versio|-version)
112 #### Cheat instead of using autoconf and make to fill in the version.
113 "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
114 -*) if [ ${mhmailswitch} -eq 1 ]; then
118 post_send_switch_arg=1
119 postsendargs="${postsendargs:+${postsendargs} }${arg}" ;;
121 if [ ${bodyarg} -eq 1 ]; then
125 #### Allow -body "" by using just a newline for the body.
126 [ "${body}"x = x ] && body='
128 elif [ ${fromarg} -eq 1 ]; then
131 elif [ ${subjectarg} -eq 1 ]; then
134 elif [ ${attacharg} -eq 1 ]; then
135 headerfieldlist="${headerfieldlist:+${headerfieldlist}}\
136 Nmh-Attachment: ${arg}
139 elif [ ${headerfieldarg} -eq 1 ]; then
140 #### It's not strictly necessary to have one space after
141 #### the : that separates the header field name from the
142 #### body, but do it to avoid surprising someone.
143 add=`printf "${arg}" | sed -e 's/:/: /' -e 's/: /: /'`
144 headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
147 elif [ ${post_send_switch_arg} -eq 1 ]; then
148 postsendargs="${postsendargs:+${postsendargs} }${arg}"
149 elif [ ${ccarg} -eq 1 ]; then
150 #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
151 cclist="${cclist:+${cclist}, }${arg}"
154 tolist="${tolist:+${tolist}, }${arg}"
159 #### Check for at least one address and -from.
160 if [ "${tolist}"x = x ]; then
161 printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
163 if [ "${from}"x = x ]; then
164 nmhlibdir=`${nmhbindir}/mhparam libdir`/
165 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
168 #### Check for missing mandatory arguments.
172 [ ${resent} -eq 0 ] && prefix= || prefix='Resent-'
173 header="${prefix}To: ${tolist}
175 [ "${cclist}"x = x ] || header="${header}${prefix}Cc: ${cclist}
177 [ "${subject}"x = x ] || header="${header}${prefix}Subject: ${subject}
179 [ "${from}"x = x ] || header="${header}${prefix}From: ${from}
182 if [ "${headerfieldlist}" ]; then
183 header="${header}${headerfieldlist}"
186 #### Set up a file to supply as a draft to post/send. And set a
187 #### trap to remove it. send moves the file to a backup, so it will
188 #### remove that, too.
190 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
191 tmpfile="${tmpdir}/mhmail$$"
192 tmpfilebackup="${tmpdir}/[,#]mhmail$$"
196 if [ ${resent} -eq 0 ]; then
197 #### Add blank line after header if not resending.
200 message_file="${tmpfile}"
202 if [ ${use_send} -eq 0 ]; then
203 postsendargs="${postsendargs:+${postsendargs} }-dist"
204 message_file="${tmpfile}"
206 #### When resending with send, tmpfile will just contain the
207 #### Resent- header fields. "${tmpfileresent}" will contain
208 #### the message that is being resent.
209 tmpfileresent="${tmpdir}/mhmail-resent$$"
210 mhdist=1; export mhdist
211 mhaltmsg=${tmpfileresent}; export mhaltmsg
212 message_file="${tmpfileresent}"
213 printf "" >"${message_file}" || exit 2
217 trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
219 if [ "${body}"x = x ]; then
220 #### First put message header in the file.
221 printf "%s" "${header}" >"${tmpfile}" || exit 2
223 tmpfile_size_before=`wc -c "${message_file}"`
224 #### Now grab the body from stdin. cat >> handles blank lines
225 #### better than body=`cat`.
226 cat >>"${message_file}" || exit 2
227 tmpfile_size_after=`wc -c "${message_file}"`
229 #### Don't allow an empty body (from stdin). Use string
230 #### comparison so we don't have to strip the filename, etc.
231 if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
232 printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
236 #### Add trailing newline to body if it doesn't have one.
237 if [ `tail -n 1 "${message_file}" | wc -l` -ne 1 ]; then
238 printf "\n" >>"${message_file}" || exit 2
241 #### Add trailing newline to body if it doesn't have one.
242 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
245 if [ "${tmpfileresent}" ]; then
246 #### Put just the new message header in the file.
247 printf "%s" "${header}" >"${tmpfile}" || exit 2
248 #### and the body in the file to resend.
249 printf "${body}" >"${tmpfileresent}" || exit 2
251 #### Put message header and body in the file.
252 printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
256 if [ ${sendsw} -eq 0 ]; then
259 if [ ${use_send} -eq 0 ]; then
260 post_or_send=`${nmhbindir}/mhparam postproc`
262 post_or_send="${nmhbindir}/send"
265 if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
268 printf "Letter saved in dead.letter\n"
269 #### exec skips the trap set above.
270 [ "${tmpfileresent}" ] && rm -f "${tmpfileresent}"
271 exec mv "${tmpfile}" dead.letter