scripts

Scripts for general automations
git clone git://git.laack.co/scripts.git
Log | Files | Refs

email-send.sh (1720B)


      1 #!/bin/bash
      2 
      3 # USAGE: email-send.sh {SUBJECT}
      4 # Reads message from stdin
      5 # If there is no subject it sends "Message from {hostname}"
      6 
      7 # TODO: This isn't consistent with the gotify thing; is this fine?
      8 # gotify stuff will probably be much shorter so maybe?
      9 
     10 set -eo pipefail
     11 
     12 UUID=$(uuidgen)
     13 HOSTNAME=$(hostnamectl | head -n 1 | awk '{print $3}')
     14 
     15 WORKDIR="/tmp/emails"
     16 PLAIN="$WORKDIR/$UUID.txt"
     17 ENC="$WORKDIR/$UUID.asc"
     18 FINAL="$WORKDIR/$UUID.eml"
     19 
     20 mkdir -p "$WORKDIR"
     21 
     22 BOUNDARY="pgp-boundary-$UUID"
     23 
     24 SUBJECT="Message from $HOSTNAME"
     25 
     26 if [ -n "$1" ]; then
     27     SUBJECT=$1
     28 fi
     29 
     30 
     31 cat > "$PLAIN" <<EOF
     32 Content-Type: text/plain; charset=utf-8
     33 
     34 $(cat)
     35 EOF
     36 
     37 gpg --yes --batch \
     38     --armor \
     39     --output "$ENC" \
     40     --encrypt \
     41     --recipient "$EMAIL_ADDRESS" \
     42     "$PLAIN"
     43 
     44 {
     45   echo "From: Automated <$EMAIL_ADDRESS>"
     46   echo "To: Automated <$EMAIL_ADDRESS>"
     47   echo "Subject: $SUBJECT"
     48   echo "MIME-Version: 1.0"
     49   echo "Content-Type: multipart/encrypted; protocol=\"application/pgp-encrypted\"; boundary=\"$BOUNDARY\""
     50   echo
     51   echo "This is an OpenPGP/MIME encrypted message."
     52   echo
     53   echo "--$BOUNDARY"
     54   echo "Content-Type: application/pgp-encrypted"
     55   echo
     56   echo "Version: 1"
     57   echo
     58   echo "--$BOUNDARY"
     59   echo "Content-Type: application/octet-stream; name=\"encrypted.asc\""
     60   echo "Content-Transfer-Encoding: 7bit"
     61   echo "Content-Disposition: inline; filename=\"encrypted.asc\""
     62   echo
     63   cat "$ENC"
     64   echo
     65   echo "--$BOUNDARY--"
     66 } > "$FINAL"
     67 
     68 curl -s --ssl-reqd \
     69     --url "smtps://$EMAIL_DOMAIN:465" \
     70     --user "$EMAIL_ADDRESS:$EMAIL_PW" \
     71     --mail-from "$EMAIL_ADDRESS" \
     72     --mail-rcpt "$EMAIL_ADDRESS" \
     73     --upload-file "$FINAL"
     74 
     75 rm -f "$PLAIN" "$ENC" "$FINAL"
     76 
     77 echo "PGP/MIME encrypted message sent."