rp

simple email tools
git clone https://git.parazyd.org/rp
Log | Files | Refs | README | LICENSE

commit fd5a91333a9dd1e8770a6b993fc277427698a0e7
parent 2991db784849bf0d8cb3eea6580368e9f40654dd
Author: parazyd <parazyd@dyne.org>
Date:   Sat, 28 Mar 2020 21:55:35 +0100

Add rpattach.

Diffstat:
Abin/rpattach | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+), 0 deletions(-)

diff --git a/bin/rpattach b/bin/rpattach @@ -0,0 +1,73 @@ +#!/bin/sh + +usage() { + cat <<EOF >&2 +usage: $(basename "$0") [file] [file] [...] < email +EOF + exit 1 +} + +if [ -z "$1" ]; then + usage +fi + +for i in "$@"; do + if ! [ -r "$i" ]; then + printf "Insufficient permissions to read %s" "$i" >&2 + exit 1 + fi +done + +email="$(cat)" +email_body="$(printf "%s" "$email" | sed '1,/^$/d')" +email_headers="$(printf "%s" "$email" | sed '/^$/q')" +content_type="$(printf "%s" "$email_headers" | grep -i '^content-type:' | cut -d' ' -f2-)" +email_headers="$(printf "%s" "$email_headers" | grep -vi '^content-type:')" + +mime_boundary="$(rputil -e genmimeb)" +main_content_type="multipart/mixed; boundary=\"$mime_boundary\"" + +cat <<EOF +$email_headers +Content-Type: $main_content_type +Content-Disposition: inline + +--$mime_boundary +Content-Type: $content_type +Content-Disposition: inline + +$email_body +EOF + +for i in "$@"; do + mime_type="$(file --mime-type "$i" | cut -d' ' -f2)" + + case "$mime_type" in + text*|ascii*|json*) + encoding="8bit" + ;; + *) + encoding="base64" + ;; + esac + + if [ "$encoding" = 8bit ]; then + mime_type="$(file -i "$i" | cut -d' ' -f2-)" + fi + + cat <<EOF + +--$mime_boundary +Content-Type: $mime_type +Content-Disposition: attachment; filename="$(basename "$i")" +Content-Transfer-Encoding: $encoding + +EOF + if [ "$encoding" = base64 ]; then + base64 -w 72 "$i" + else + cat "$i" + fi +done + +printf -- "\n--%s--\n" "$mime_boundary"