move pstab to td-ps, improve td-gnuplot
[hband-tools.git] / root-tools / sendmail.smtp
blob7dec5d2f703e9b8a1e1bbbe71778ac109b7f1515
1 #!/bin/bash
3 # This is a script to mimic sendmail(1) interface, but
4 # forward mail to local MTA via sendEmail(1).
6 . /usr/lib/tool/bash-utils || exit -1
8 declare -a recipient
9 from_header=''
10 envelope_sender=''
11 get_recipients_from_headers=no
12 single_dot_terminates_input=yes
14 while [ -n "$1" ]
16 case "$1" in
17 -F) shift
18 from_header=$1;;
19 -f|-r)
20 shift
21 envelope_sender=$1;;
22 -t) get_recipients_from_headers=yes;;
23 -ti)
24 get_recipients_from_headers=yes
25 single_dot_terminates_input=no
27 -i|-oi)
28 single_dot_terminates_input=no;;
29 -o)
30 shift
31 warnx "Ignoring option: -o $1";;
32 -bp|-bs)
33 errx 22 "Option not supported: $1";;
34 -*) errx 22 "Unknown option: $1";;
35 *) recipient+=("$1");;
36 esac
37 shift
38 done
40 if [ $get_recipients_from_headers = yes -a ${#recipient[@]} != 0 ]
41 then
42 errx 22 "Option -t and specifying recipients are mutually exclusive."
46 maildomain=`cat /etc/domain || cat /etc/mailname`
48 qualify_address()
50 # add local domain name if there is no domain part in the address
51 local addr=$1
52 if ! [[ $addr =~ @ ]]
53 then
54 addr=$addr@$maildomain
56 echo "$addr"
59 if [ -z "$envelope_sender" ]
60 then
61 envelope_sender=$USER@$maildomain
62 else
63 envelope_sender=`qualify_address "$envelope_sender"`
66 for idx in ${!recipient[@]}
68 recipient[$idx]=`qualify_address "${recipient[$idx]}"`
69 done
72 auto_qualify_recipients()
74 local line hname hcontent
76 while IFS=$'\n' read -r line
78 if [[ $line =~ ^(To|Cc|Bcc):([^$'\r']+) ]]
79 then
80 hname=${BASH_REMATCH[1]}
81 hcontent=${BASH_REMATCH[2]}
82 hcontent=`qualify_address "$hcontent"`
83 line="$hname:$hcontent"$'\r'
85 printf '%s\n' "$line"
86 done
89 call_sendEmail()
91 sendEmail -f "$envelope_sender" -t "${recipient[@]}" \
92 -o message-format=raw -o message-file=- \
93 -s "${SMTP_SERVER:-localhost:25}" \
94 1>&2
97 if [ $get_recipients_from_headers = yes ]
98 then
99 raw_email=`cat`
100 _IFS=$IFS
101 IFS=$'\n'
102 recipient+=(`echo "$raw_email" | auto_qualify_recipients | mail-extract-raw-headers To Cc Bcc | mail-extract-addresses`)
103 IFS=$_IFS
104 if [ -n "$SENDMAIL_DEBUG" ]
105 then
106 warnx "detected recipients: ${recipient[@]}"
108 echo "$raw_email" | call_sendEmail
109 else
110 call_sendEmail