view sendmultipart.sh @ 433:03ced2f9f271

Quote group name
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 23 Jun 2017 17:07:18 +0859
parents db840ea6e347
children d764faf9194c
line wrap: on
line source

#!/bin/sh
# send multipart message via email
# (C)2012,2015 by HIROSE Yuuji [yuuji(at)yatex.org]
# You can obtain the latest version of this script from:
#   http://www.gentei.org/~yuuji/software/sendmultipart.sh
# Last modified Fri Oct 21 10:37:19 2016 on firestorm
#
# Typical usage:
#  echo "Hi, here's photo I've taken.  Enjoy" | \
#    sendmultipart.sh -t rcpt1@example.com,rcpt2@example.org \
#      -s "Photo of party" -f myself@example.net photo*.jpg

myname=`basename $0`
usage () {
  cat<<_EOU_
$myname: Send multipart message via email
Usage: $0 -t recipient [options] file(s)
	-t ToAddress	Recipient address.
			Multiple -t option and/or Multiple ToAddresses
			delimited by comma are acceptable.
	-s Subject	Set subject to \`Subject'
	-f FromAddress	Set From: header to \`FromAddress'
_EOU_
  exit 0
}

conf=~/.sendmultipart
verbose=0
hgid='$HGid$'
mailer=`echo $hgid|cut -d' ' -f2,3,4`

base64byuu() {
  uuencode -m $1 < $1 | tail +2
}
base64=${BASE64:-base64byuu}
boundary="${mailer}_`date +%Y%m%d,%H%M%Sx`"
ctheader="Content-Type: Multipart/Mixed;
 boundary=\"$boundary\""
textcharset=iso-2022-jp
rcpts=""
rcptscomma=""

[ -f $conf ] && . $conf		# read rc file

while [ x"$1" != x"" ]; do
  case "$1" in
    -t)	shift;
      rcpts="$rcpts${rcpts:+ }`echo $1|tr , ' '`"
      rcptscomma="$rcptscomma${rcptscomma:+, }$1"
      ;;
    -s) shift; subject="`echo $1|nkf -M`" ;;
    -f) shift; from="From: $1" ;;
    -v)	verbose=1 ;;
    -h) usage ;;		# -h helpオプション
    --) shift; break ;;
    *)	break ;;		# -で始まらないものが来たら即処理突入
  esac
  shift	
done
plainheader="Content-Type: text/plain; charset=$textcharset
Content-Transfer-Encoding: 7bit"

tolower() {
  tr '[A-Z]' '[a-z]'
}
cattextwithheader() {
  coding=`nkf -g $1|cut -d' ' -f1`
  case `echo $coding | tolower` in
    iso-2022-jp) encoding=7bit   cat=cat;;
    *)		 encoding=base64 cat="$base64" ;;
  esac
  filename=`echo $1|nkf -M`
  cat<<EOF
Content-Type: text/plain; charset="$coding"
Content-Disposition: inline; filename="$filename"
Content-Transfer-Encoding: $encoding

EOF
  $cat $1
}

# Begin procedure
if [ x"$rcpts" = x"" ]; then
  sendmail="cat"
else
  sendmail="sendmail"
fi
body=`nkf -j`			# Convert stdin to iso-2022-jp

# Generate contents
( cat<<EOF
To: ${rcptscomma:-[Not specified]}
Subject: ${subject:-$*}
$ctheader
Mime-Version: 1.0
X-Mailer: $mailer
$from

--$boundary
$plainheader

EOF
  echo "$body"
  echo
  for file in "$@"; do
    echo "--$boundary"
    case `echo $file|tolower` in
      *.txt|*.jis|*.sjis|*.euc|*.utf*)
	cattextwithheader $file
	;;
      *)
	echo -n "Content-Type: "
	file --mime-type - < "$file" | cut -d' ' -f2
	echo "Content-Transfer-Encoding: base64"
	echo "Content-Disposition: inline; filename=\"$file\""
	echo
	$base64 $file
	;;
    esac
    echo
  done
  echo "--${boundary}--"
) | $sendmail $rcpts

exit 0

yatex.org