diff sendmultipart.sh @ 97:7f9569a7e0ce

add sendmultipart.sh
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 03 Aug 2015 18:00:35 +0900
parents
children 70de8824f27e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sendmultipart.sh	Mon Aug 03 18:00:35 2015 +0900
@@ -0,0 +1,134 @@
+#!/bin/sh
+# send multipart message via email
+# (C)2012 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 Sat Aug  1 16:00:31 2015 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
+}
+
+
+# if [ "$1" = "" ]; then
+#   usage
+# fi
+
+# 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
+	;;
+      *)
+	case `echo $file | tolower` in
+	  *.jpg|*.jpeg)	echo "Content-Type: Image/jpeg" ;;
+	  *.png)	echo "Content-Type: Image/png" ;;
+	  *.gif)	echo "Content-Type: Image/gif" ;;
+	  *)		echo "Content-Type: Application/Octet-Stream" ;;
+	esac
+	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