s4

changeset 97:7f9569a7e0ce

add sendmultipart.sh
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 03 Aug 2015 18:00:35 +0900
parents 67b55dcf6fcb
children 70de8824f27e
files sendmultipart.sh
diffstat 1 files changed, 134 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sendmultipart.sh	Mon Aug 03 18:00:35 2015 +0900
     1.3 @@ -0,0 +1,134 @@
     1.4 +#!/bin/sh
     1.5 +# send multipart message via email
     1.6 +# (C)2012 by HIROSE Yuuji [yuuji(at)yatex.org]
     1.7 +# You can obtain the latest version of this script from:
     1.8 +#   http://www.gentei.org/~yuuji/software/sendmultipart.sh
     1.9 +# Last modified Sat Aug  1 16:00:31 2015 on firestorm
    1.10 +#
    1.11 +# Typical usage:
    1.12 +#  echo "Hi, here's photo I've taken.  Enjoy" | \
    1.13 +#    sendmultipart.sh -t rcpt1@example.com,rcpt2@example.org \
    1.14 +#      -s "Photo of party" -f myself@example.net photo*.jpg
    1.15 +
    1.16 +myname=`basename $0`
    1.17 +usage () {
    1.18 +  cat<<_EOU_
    1.19 +$myname: Send multipart message via email
    1.20 +Usage: $0 -t recipient [options] file(s)
    1.21 +	-t ToAddress	Recipient address.
    1.22 +			Multiple -t option and/or Multiple ToAddresses
    1.23 +			delimited by comma are acceptable.
    1.24 +	-s Subject	Set subject to \`Subject'
    1.25 +	-f FromAddress	Set From: header to \`FromAddress'
    1.26 +_EOU_
    1.27 +  exit 0
    1.28 +}
    1.29 +
    1.30 +conf=~/.sendmultipart
    1.31 +verbose=0
    1.32 +hgid='$HGid$'
    1.33 +mailer=`echo $hgid|cut -d' ' -f2,3,4`
    1.34 +
    1.35 +base64byuu() {
    1.36 +  uuencode -m $1 < $1 | tail +2
    1.37 +}
    1.38 +base64=${BASE64:-base64byuu}
    1.39 +boundary="${mailer}_`date +%Y%m%d,%H%M%Sx`"
    1.40 +ctheader="Content-Type: Multipart/Mixed;
    1.41 + boundary=\"$boundary\""
    1.42 +textcharset=iso-2022-jp
    1.43 +rcpts=""
    1.44 +rcptscomma=""
    1.45 +
    1.46 +[ -f $conf ] && . $conf		# read rc file
    1.47 +
    1.48 +while [ x"$1" != x"" ]; do
    1.49 +  case "$1" in
    1.50 +    -t)	shift;
    1.51 +      rcpts="$rcpts${rcpts:+ }`echo $1|tr , ' '`"
    1.52 +      rcptscomma="$rcptscomma${rcptscomma:+, }$1"
    1.53 +      ;;
    1.54 +    -s) shift; subject="`echo $1|nkf -M`" ;;
    1.55 +    -f) shift; from="From: $1" ;;
    1.56 +    -v)	verbose=1 ;;
    1.57 +    -h) usage ;;		# -h helpオプション
    1.58 +    --) shift; break ;;
    1.59 +    *)	break ;;		# -で始まらないものが来たら即処理突入
    1.60 +  esac
    1.61 +  shift	
    1.62 +done
    1.63 +plainheader="Content-Type: text/plain; charset=$textcharset
    1.64 +Content-Transfer-Encoding: 7bit"
    1.65 +
    1.66 +tolower() {
    1.67 +  tr '[A-Z]' '[a-z]'
    1.68 +}
    1.69 +cattextwithheader() {
    1.70 +  coding=`nkf -g $1|cut -d' ' -f1`
    1.71 +  case `echo $coding | tolower` in
    1.72 +    iso-2022-jp) encoding=7bit   cat=cat;;
    1.73 +    *)		 encoding=base64 cat="$base64" ;;
    1.74 +  esac
    1.75 +  filename=`echo $1|nkf -M`
    1.76 +  cat<<EOF
    1.77 +Content-Type: text/plain; charset="$coding"
    1.78 +Content-Disposition: inline; filename="$filename"
    1.79 +Content-Transfer-Encoding: $encoding
    1.80 +
    1.81 +EOF
    1.82 +  $cat $1
    1.83 +}
    1.84 +
    1.85 +
    1.86 +# if [ "$1" = "" ]; then
    1.87 +#   usage
    1.88 +# fi
    1.89 +
    1.90 +# Begin procedure
    1.91 +if [ x"$rcpts" = x"" ]; then
    1.92 +  sendmail="cat"
    1.93 +else
    1.94 +  sendmail="sendmail"
    1.95 +fi
    1.96 +body=`nkf -j`			# Convert stdin to iso-2022-jp
    1.97 +
    1.98 +# Generate contents
    1.99 +( cat<<EOF
   1.100 +To: ${rcptscomma:-[Not specified]}
   1.101 +Subject: ${subject:-$*}
   1.102 +$ctheader
   1.103 +Mime-Version: 1.0
   1.104 +X-Mailer: $mailer
   1.105 +$from
   1.106 +
   1.107 +--$boundary
   1.108 +$plainheader
   1.109 +
   1.110 +EOF
   1.111 +  echo "$body"
   1.112 +  echo
   1.113 +  for file in "$@"; do
   1.114 +    echo "--$boundary"
   1.115 +    case `echo $file|tolower` in
   1.116 +      *.txt|*.jis|*.sjis|*.euc|*.utf*)
   1.117 +	cattextwithheader $file
   1.118 +	;;
   1.119 +      *)
   1.120 +	case `echo $file | tolower` in
   1.121 +	  *.jpg|*.jpeg)	echo "Content-Type: Image/jpeg" ;;
   1.122 +	  *.png)	echo "Content-Type: Image/png" ;;
   1.123 +	  *.gif)	echo "Content-Type: Image/gif" ;;
   1.124 +	  *)		echo "Content-Type: Application/Octet-Stream" ;;
   1.125 +	esac
   1.126 +	echo "Content-Transfer-Encoding: base64"
   1.127 +	echo "Content-Disposition: inline; filename=\"$file\""
   1.128 +	echo
   1.129 +	$base64 $file
   1.130 +	;;
   1.131 +    esac
   1.132 +    echo
   1.133 +  done
   1.134 +  echo "--${boundary}--"
   1.135 +) | $sendmail $rcpts
   1.136 +
   1.137 +exit 0