File: biabam

package info (click to toggle)
biabam 0.9.7-7
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 172 kB
  • ctags: 3
  • sloc: sh: 630; makefile: 32
file content (176 lines) | stat: -rwxr-xr-x 5,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
#    BIABAM: Biabam Is A Bash Attachment Mailer
#    Copyright (C) 2000, 2003, 2004 Mads Martin J´┐Żrgensen <mmj@mmj.dk>
#
#    This program is free software; you can redistribute it
#    and/or modify it under the terms of the GNU General Public
#    License as published by the Free Software Foundation; either
#    version 2 of the License, or (at your option) any later
#    version.
#
#    This program is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied
#    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#    PURPOSE.  See the GNU General Public License for more
#    details.
#
#    You should have received a copy of the GNU General Public
#    License along with this program; if not, write to the Free
#    Software Foundation, Inc., 59 Temple Place, Suite 330,
#    Boston, MA 02111-1307 USA

# change these to suit your needs
SENDMAIL=/usr/sbin/sendmail  # your Mail-Transfer-Agent
SENDMAIL_OPTS=-i             # and its required options
SP=,  # separator for filenames and recipients (default is the comma character)

# no user serviceable parts below this point
VERSION=0.9.7

if  [ $# -lt 2 ]; then
  echo "Usage:"
  echo "$0 filename1[${SP}filename2${SP}filenameN] [-s subject] recipient1[${SP}recipient2${SP}recipientN]"
  exit 1
fi

if ! which uuencode > /dev/null; then
  echo "This program needs the uuencode utility to perform base64 encoding."
  exit 1
fi

# have they supplied a subject in first position
SUBJECT="File delivery"
if [ a"$1" = "a-s" ]; then
  shift
  SUBJECT="$1"
  shift
fi

# Find out the number of files to attach
TOTAL_ATTACHMENTS=`echo "$1" | awk -F$SP '{print NF}'`

for i in `seq 1 $TOTAL_ATTACHMENTS`;
do
  # store filenames in an array
  ARR_ATTACHMENTS[$i]=$(echo "$1" | awk -F$SP '{print $'$i'}' )
done

if ! TEMPFILE="`mktemp /tmp/biabam.XXXXXX`"; then
  echo "Biabam is unable to create the temporary file."
  exit 1
fi

BASETEMP="`basename $TEMPFILE`"

for i in `seq 1 $TOTAL_ATTACHMENTS`; do
  # array for attachments basename
  BASEATTACHMENT[$i]="`basename \"${ARR_ATTACHMENTS[$i]}\"`"
  # test if file exists
  if ! test -f "${ARR_ATTACHMENTS[$i]}"; then
    echo "File \"${ARR_ATTACHMENTS[$i]}\" is not a regular file."
    exit 1
  fi
  # array for temp files
  if ! TEMPUUENCODED[$i]="`mktemp /tmp/biabam.uu.XXXXXX`"; then
    echo "Biabam is unable to create the temporary uuencoded file."
    exit 1
  fi

  # use 'file' to get MIME type if possible
  TYPE[$i]="application/unknown"; # array for mime type files
  if which file > /dev/null ; then
    MIME=`file -bi -- "${ARR_ATTACHMENTS[$i]}"`
    MIME=${MIME/,*;/;}
    MIME=${MIME/%,*/}
    TYPE[$i]=${MIME:-application/unknown}
  fi

  uuencode --base64 -- "${ARR_ATTACHMENTS[$i]}" "${BASEATTACHMENT[$i]}" | \
  sed '1d;$d' > ${TEMPUUENCODED[$i]}
done

BOUNDARY="$BASETEMP$BASETEMP"

shift 		# skip over filename

# have they supplied a subject in second position
if [ a"$1" = "a-s" ]; then
  shift
  SUBJECT="$1"
  shift
fi

# Find out the number of recipients to send the email
TOTAL_EMAILS=`echo "$1" | awk -F$SP '{print NF}'`

for i in `seq 1 $TOTAL_EMAILS`; do
  STR="'{print \$$i}'"
  # store recipients in an array
  ARR_EMAILS[$i]=`sh -c "echo "$1" | awk -F$SP $STR" `
done

TO="To:"

for i in `seq 1 $TOTAL_EMAILS`; do
  # write the 'To:' field with all recipients previously stored in the array
  TO="$TO ${ARR_EMAILS[$i]},"
done

TO=${TO:0:${#TO}-1} # remove the last comma

HOST=`hostname`

echo $TO >> $TEMPFILE
echo "Subject: $SUBJECT" >> $TEMPFILE
echo "X-Mailer: BIABAM $VERSION" >> $TEMPFILE
echo "Message-ID: <`date +%Y%m%d%H%M%S`.$BASETEMP@$HOST>" >> $TEMPFILE
echo "Mime-Version: 1.0" >> $TEMPFILE 
echo "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"" >> $TEMPFILE
echo "Content-Disposition: inline" >> $TEMPFILE
echo >> $TEMPFILE
echo >> $TEMPFILE
echo "--$BOUNDARY" >> $TEMPFILE
echo "Content-Type: text/plain; charset=us-ascii" >> $TEMPFILE
echo "Content-Disposition: inline" >> $TEMPFILE
echo >> $TEMPFILE
test -t 0 && echo "Email body (type CTRL-d on a blank line to finish):"
cat >> $TEMPFILE
echo >> $TEMPFILE

for i in `seq 1 $TOTAL_ATTACHMENTS`; do
  echo "--$BOUNDARY" >> $TEMPFILE
  echo "Content-Type: ${TYPE[$i]}" >> $TEMPFILE
  echo "Content-Disposition: attachment; filename=\"${BASEATTACHMENT[$i]}\"" >> $TEMPFILE
  echo "Content-Transfer-Encoding: base64" >> $TEMPFILE
  echo >> $TEMPFILE
  # write all uuencoded attachments files in the email source code
  cat ${TEMPUUENCODED[$i]} >> $TEMPFILE
  echo >> $TEMPFILE
done

echo "--$BOUNDARY--" >> $TEMPFILE
echo >> $TEMPFILE

for i in `seq 1 $TOTAL_EMAILS`; do
  # put all recipients together to call sendmail below
  RECIPIENTS="$RECIPIENTS ${ARR_EMAILS[$i]}"
done

if [[ -z $RECIPIENTS ]]; then
  echo "Biabam is unable to find the recipients."
  #clean
  for i in `seq 1 $TOTAL_ATTACHMENTS`; do
    /bin/rm -f ${TEMPUUENCODED[$i]} # remove all temp files created
  done
  /bin/rm -f $TEMPFILE # remove the email source code tempfile
  exit 1
else
  cat $TEMPFILE | $SENDMAIL $SENDMAIL_OPTS $RECIPIENTS # here we call sendmail
fi

for i in `seq 1 $TOTAL_ATTACHMENTS`; do
  /bin/rm -f ${TEMPUUENCODED[$i]} # remove all temp files created
done

/bin/rm -f $TEMPFILE # remove the email source code tempfile