#!/usr/bin/ksh usage () { echo 'usage: mailx [-dv] [-F name] [-r from] [-s subject] <-t | [-b bcc] [-c cc] [to...]> at least one of -t, -b, -c or to... must be specified.' >&2 exit $1 } sendmail='/usr/sbin/sendmail' bcc= bccsep='BCC: ' cc= ccsep='CC: ' name= namesep='-F ' from= fromsep='-f ' subject='(no subject)' subjectsep='Subject: ' debug= toopt= verbose= undisclosed='undisclosed-recipients:;' while getopts 'b:c:dF:hr:s:tv' c; do case ${c} in 'b') bcc="${bcc}${bccsep}${OPTARG}"; bccsep=',' ;; 'c') cc="${cc}${ccsep}${OPTARG}"; ccsep=',' ;; 'd') debug='-d' ;; 'F') name="${namesep}'${OPTARG}'" ;; 'h') usage 0 ;; 'r') from="${fromsep}'${OPTARG}'" ;; 's') subject="${OPTARG}" ;; 't') toopt='-t' ;; 'v') verbose='-v' ;; *) usage 1 ;; esac done shift $(($OPTIND-1)) nl=' ' toarg= to= tosep= if [[ -n ${toopt} ]]; then cc= bcc= else if [[ $# = 0 ]]; then if [[ -n ${cc}${bcc} ]]; then set -- "${undisclosed}" else usage 1 fi else for arg; do toarg="${toarg}${tosep}'${arg}'"; tosep=' '; done fi tosep='To: ' for arg; do to="${to}${tosep}${arg}"; tosep=','; done [[ -n ${to} ]] && to="${to}${nl}" [[ -n ${cc} ]] && cc="${cc}${nl}" [[ -n ${bcc} ]] && bcc="${bcc}${nl}" fi [[ -n ${subject} ]] && subject="${subjectsep}${subject}${nl}" read -r line nl1="${nl}" nl2= case ${line} in *':'*) case ${line%%:*} in *' '*) ;; *) nl1= nl2="${nl}" ;; esac ;; '') nl1= nl2= ;; esac if [[ -n ${debug} ]]; then sendmail=sendmail sendmail () { echo sendmail "$@"; cat; } fi eval ${sendmail} ${toopt} ${verbose} ${name} ${from} ${toarg} << EOF ${subject}${to}${cc}${bcc}${nl1}${line}${nl2}$(cat) EOF #!eof