rp

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

rpsend (2372B)


      1 #!/bin/sh
      2 #
      3 # Copy me if you can
      4 # by parazyd
      5 #
      6 
      7 profile="${RPPROFILE:-$HOME/.rp/default}"
      8 
      9 usage() {
     10 	cat <<EOF
     11 * Usage: $(basename $0) [-n] [-v] [-l] [-a] < email
     12   -n: dry run
     13   -l: list outbox
     14   -v: verbose list output
     15   -a: send all
     16 EOF
     17 	exit 1
     18 }
     19 
     20 listoutbox() {
     21 	headers="Date To Cc Subject"
     22 
     23 	for i in $(find "$profile"/tmp/outbox -type f); do
     24 		if [ -n "$verbose" ];then
     25 			printf "%s\n%s\\n\\n" "$i" "$(rpheaders -v $headers < $i)"
     26 		else
     27 			echo "$i"
     28 		fi
     29 	done
     30 }
     31 
     32 while getopts "nvlah" opt; do
     33 	case "$opt" in
     34 	n)
     35 		dryrun="--pretend"
     36 		;;
     37 	v)
     38 		verbose="true"
     39 		;;
     40 	l)
     41 		list="true"
     42 		;;
     43 	a)
     44 		echo "* Sending all queued emails..."
     45 		for j in $(rpsend -l); do
     46 			rpsend < "$j" && { [ -z "$dryrun" ] && rm -f "$j" ; }
     47 		done
     48 		exit $?
     49 		;;
     50 	*)
     51 		usage
     52 		;;
     53 	esac
     54 done
     55 
     56 if [ -n "$list" ]; then
     57 	listoutbox
     58 	exit 0
     59 fi
     60 
     61 email="$(cat)"
     62 
     63 rnet="$(rpheaders rnet < "$profile"/config)"
     64 rimap="$(echo "$rnet" | cut -d'!' -f2)"
     65 ruser="$(rpheaders ruser < "$profile"/config)"
     66 
     67 printf "\\n\\n* SMTP send via %s\\n" "$rimap"
     68 
     69 if ! [ -f "$profile/smtp-fingerprint" ]; then
     70 	echo "* No saved TLS certificate fingerprint found. Going with TOFU."
     71 	rputil -e getfpr "$rnet" > "$profile"/smtp-fingerprint
     72 fi
     73 
     74 known="$(cat "$profile"/smtp-fingerprint)"
     75 served="$(rputil -e getfpr "$rnet")"
     76 
     77 if [ "$known" != "$served" ]; then
     78 	cat <<EOF
     79 * Known  fingerprint: $known
     80 * Served fingerprint: $served
     81 
     82 * Server fingerprint mismatch!"
     83 * The known one is different, this may be a man-in-the-middle!
     84 * To trust the new one, edit or delete $profile/smtp-fingerprint
     85 EOF
     86 	exit 1
     87 fi
     88 
     89 
     90 msmtpcfg="$(mktemp)"
     91 cat > "$msmtpcfg" << EOF
     92 account default
     93 from $ruser
     94 user $ruser
     95 host $rimap
     96 password $(rpheaders rpass < "$profile"/config)
     97 port 587
     98 auth plain
     99 tls on
    100 tls_starttls on
    101 tls_certcheck on
    102 tls_fingerprint ${known}
    103 EOF
    104 
    105 _subj="$(echo "$email" | rpheaders Subject)"
    106 _dest="$(echo "$email" | rpheaders To)"
    107 _cc="$(echo "$email" | rpheaders Cc)"
    108 
    109 cat <<EOF
    110 * Subject: $_subj
    111 * To:      $_dest
    112 * Cc:      $_cc
    113 * Sending $(rputil -e humansize "${#email}") over the network...
    114 EOF
    115 
    116 cat | msmtp "$dryrun" -C "$msmtpcfg" -t -X "$profile/tmp/msmtp.log" << EOF
    117 $email
    118 EOF
    119 
    120 _e=$?
    121 
    122 rm -f "$msmtpcfg"
    123 
    124 case "$_e" in
    125 	0)
    126 		echo "* Email sent successfully"
    127 		;;
    128 	*)
    129 		echo "* msmtp failed"
    130 		exit $_e
    131 		;;
    132 esac
    133 
    134 # if successful, place into Sent
    135 cat | /usr/libexec/dovecot/deliver -m Sent << EOF
    136 $email
    137 EOF