#!/bin/bash # # gpgmail v1.0 # # Copyright 2011 by Florian Streibelt # # shellscript to send gpg-encrypted mails from the commandline # produces a PGP-Mime encapsulated message. # function usage(){ cat<&2 usage ;; :) echo "error: This option needs an argument: -$OPTARG" >&2 usage ;; esac done if [ "$MAILTO" == "" ]; then echo "error: No recipient given" >&2 usage fi LOCALPART="${LOCALPART:-$(hostname -f)}" FROMNAME="$(getent passwd "$USER" | cut -d : -f 5 | cut -d , -f 1)" FROM="${FROM:-${USER}@${LOCALPART}}" SUBJECT="${SUBJECT:-no subject}" KEYID="${KEYID:-$MAILTO}" if [ "$dry" == "yes" ]; then echo "Localpart: $LOCALPART" echo "FROMNAME: $FROMNAME" echo "FROM: $FROM" echo "MAILTO: $MAILTO" echo "SUBJECT: $SUBJECT" echo "KEYID: $KEYID" exit 0 fi if [ "$INFILE" != "" ]; then # a file was given to be sent: msgfile="$INFILE" else # no message file - so read from stdin. # use a tempfile to be able to check # if stdin gives us no data. #create tempfile and make sure it gets deleted #when we exit/get killed. msgfile=$(mktemp) trap "rm $msgfile" EXIT if [ ! -e ${msgfile} ]; then echo "error: cannot create tempfile" >&2 exit 2 fi #get message from stdin into msgfile: cat > "$msgfile" fi #are we supposed to check for empty messages? if [ "$send_empty_mails" == "no" ]; then if [ ! -s "$msgfile" ]; then # do not send empty mails... # no error message to prevent Cron from # sending just the error by mail. exit 0 fi fi # # THOU SHALT NOT TOUCH ANY LINE BELOW # BOUNDARY="${RANDOM}${PPID}${RANDOM}" DATE="$(date -R)" ( cat< Return-Path: <$FROM> Message-ID: <$$.$(date +%s).$RANDOM@$LOCALPART> Date: ${DATE} User-Agent: bash-${BASH_VERSION} gpgmail written by Florian Streibelt Errors-To: ${FROM} MIME-Version: 1.0 To: ${MAILTO} Subject: ${SUBJECT} Content-Type: multipart/encrypted; protocol="application/pgp-encrypted"; boundary="------------$BOUNDARY" This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156) --------------$BOUNDARY Content-Type: application/pgp-encrypted Content-Description: PGP/MIME version identification Version: 1 --------------$BOUNDARY Content-Type: application/octet-stream; name="encrypted.asc" Content-Description: OpenPGP encrypted message Content-Disposition: inline; filename="encrypted.asc" PGPMIMEHEADER ( cat<