97
|
1 #!/bin/sh
|
|
2 # send multipart message via email
|
|
3 # (C)2012 by HIROSE Yuuji [yuuji(at)yatex.org]
|
|
4 # You can obtain the latest version of this script from:
|
|
5 # http://www.gentei.org/~yuuji/software/sendmultipart.sh
|
|
6 # Last modified Sat Aug 1 16:00:31 2015 on firestorm
|
|
7 #
|
|
8 # Typical usage:
|
|
9 # echo "Hi, here's photo I've taken. Enjoy" | \
|
|
10 # sendmultipart.sh -t rcpt1@example.com,rcpt2@example.org \
|
|
11 # -s "Photo of party" -f myself@example.net photo*.jpg
|
|
12
|
|
13 myname=`basename $0`
|
|
14 usage () {
|
|
15 cat<<_EOU_
|
|
16 $myname: Send multipart message via email
|
|
17 Usage: $0 -t recipient [options] file(s)
|
|
18 -t ToAddress Recipient address.
|
|
19 Multiple -t option and/or Multiple ToAddresses
|
|
20 delimited by comma are acceptable.
|
|
21 -s Subject Set subject to \`Subject'
|
|
22 -f FromAddress Set From: header to \`FromAddress'
|
|
23 _EOU_
|
|
24 exit 0
|
|
25 }
|
|
26
|
|
27 conf=~/.sendmultipart
|
|
28 verbose=0
|
|
29 hgid='$HGid$'
|
|
30 mailer=`echo $hgid|cut -d' ' -f2,3,4`
|
|
31
|
|
32 base64byuu() {
|
|
33 uuencode -m $1 < $1 | tail +2
|
|
34 }
|
|
35 base64=${BASE64:-base64byuu}
|
|
36 boundary="${mailer}_`date +%Y%m%d,%H%M%Sx`"
|
|
37 ctheader="Content-Type: Multipart/Mixed;
|
|
38 boundary=\"$boundary\""
|
|
39 textcharset=iso-2022-jp
|
|
40 rcpts=""
|
|
41 rcptscomma=""
|
|
42
|
|
43 [ -f $conf ] && . $conf # read rc file
|
|
44
|
|
45 while [ x"$1" != x"" ]; do
|
|
46 case "$1" in
|
|
47 -t) shift;
|
|
48 rcpts="$rcpts${rcpts:+ }`echo $1|tr , ' '`"
|
|
49 rcptscomma="$rcptscomma${rcptscomma:+, }$1"
|
|
50 ;;
|
|
51 -s) shift; subject="`echo $1|nkf -M`" ;;
|
|
52 -f) shift; from="From: $1" ;;
|
|
53 -v) verbose=1 ;;
|
|
54 -h) usage ;; # -h helpオプション
|
|
55 --) shift; break ;;
|
|
56 *) break ;; # -で始まらないものが来たら即処理突入
|
|
57 esac
|
|
58 shift
|
|
59 done
|
|
60 plainheader="Content-Type: text/plain; charset=$textcharset
|
|
61 Content-Transfer-Encoding: 7bit"
|
|
62
|
|
63 tolower() {
|
|
64 tr '[A-Z]' '[a-z]'
|
|
65 }
|
|
66 cattextwithheader() {
|
|
67 coding=`nkf -g $1|cut -d' ' -f1`
|
|
68 case `echo $coding | tolower` in
|
|
69 iso-2022-jp) encoding=7bit cat=cat;;
|
|
70 *) encoding=base64 cat="$base64" ;;
|
|
71 esac
|
|
72 filename=`echo $1|nkf -M`
|
|
73 cat<<EOF
|
|
74 Content-Type: text/plain; charset="$coding"
|
|
75 Content-Disposition: inline; filename="$filename"
|
|
76 Content-Transfer-Encoding: $encoding
|
|
77
|
|
78 EOF
|
|
79 $cat $1
|
|
80 }
|
|
81
|
|
82
|
|
83 # if [ "$1" = "" ]; then
|
|
84 # usage
|
|
85 # fi
|
|
86
|
|
87 # Begin procedure
|
|
88 if [ x"$rcpts" = x"" ]; then
|
|
89 sendmail="cat"
|
|
90 else
|
|
91 sendmail="sendmail"
|
|
92 fi
|
|
93 body=`nkf -j` # Convert stdin to iso-2022-jp
|
|
94
|
|
95 # Generate contents
|
|
96 ( cat<<EOF
|
|
97 To: ${rcptscomma:-[Not specified]}
|
|
98 Subject: ${subject:-$*}
|
|
99 $ctheader
|
|
100 Mime-Version: 1.0
|
|
101 X-Mailer: $mailer
|
|
102 $from
|
|
103
|
|
104 --$boundary
|
|
105 $plainheader
|
|
106
|
|
107 EOF
|
|
108 echo "$body"
|
|
109 echo
|
|
110 for file in "$@"; do
|
|
111 echo "--$boundary"
|
|
112 case `echo $file|tolower` in
|
|
113 *.txt|*.jis|*.sjis|*.euc|*.utf*)
|
|
114 cattextwithheader $file
|
|
115 ;;
|
|
116 *)
|
|
117 case `echo $file | tolower` in
|
|
118 *.jpg|*.jpeg) echo "Content-Type: Image/jpeg" ;;
|
|
119 *.png) echo "Content-Type: Image/png" ;;
|
|
120 *.gif) echo "Content-Type: Image/gif" ;;
|
|
121 *) echo "Content-Type: Application/Octet-Stream" ;;
|
|
122 esac
|
|
123 echo "Content-Transfer-Encoding: base64"
|
|
124 echo "Content-Disposition: inline; filename=\"$file\""
|
|
125 echo
|
|
126 $base64 $file
|
|
127 ;;
|
|
128 esac
|
|
129 echo
|
|
130 done
|
|
131 echo "--${boundary}--"
|
|
132 ) | $sendmail $rcpts
|
|
133
|
|
134 exit 0
|