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