s4

view sendmultipart.sh @ 670:93e6e883c677

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