#!/bin/bash # autoreply.sh # # This script sends a canned email response to those who've errone- # ously emailed me at . Once I've manually # tagged those emails `misdirected`, it iterates over those emails and # responds. set -euo pipefail if [[ -z "$(notmuch search tag:misdirected)" ]] ; then echo "[autoreply.sh] Currently no emails are tagged 'misdirected'. Exiting..." exit 1 fi # This weird feed-find-output-into-while-loop comes from # https://github.com/koalaman/shellcheck/wiki/SC2044#correct-code while IFS= read -r -d '' file ; do echo "[loop] Preparing the email..." # Make a temporary file temporary_file=$(mktemp /tmp/autoreply-XXXXX) echo "[debug] Temporary file: $temporary_file" # debug # Pull sender (i.e. recipient of my email), subject, and date from email recipient="$(perl -lane 'print if /^From:/' "$file" | cut -d' ' -f2-)" ### "Frantz Fanon " subject="Re: $(perl -lane 'print if /^Subject:/' "$file" | cut -d' ' -f2-)" ### "Re: Lorem ipsum baby" date="$(perl -lane 'print if /^Date:/' "$file" | cut -d' ' -f2-)" # Copy canned message to temporary file printf "To: $recipient\nFrom: adam@theadamcooper.com\nCc: amcooper@gmail.com\nDate: $(date +'%a, %d %b %Y %R %z')\nSubject: $subject\n\n" > "$temporary_file" cat /home/adam/dotfiles/bin/autoreply/misdirected_email_autoreply.txt >> "$temporary_file" printf "\n\nOn $date, $recipient wrote:\n" >> "$temporary_file" # Append email body to temporary file discard="0" while IFS= read -r line ; do if [[ $discard = "1" ]]; then echo "> $line" >> "$temporary_file" else if [[ -z "$line" ]]; then discard="1" fi fi done < "$file" # Send the email! printf "[debug] Outgoing email:\n$(cat "$temporary_file")\n" # debug echo "[loop] Sending reply to $recipient ... " cat "$temporary_file" | msmtp --read-envelope-from --read-recipients cat "$temporary_file" | notmuch insert --folder=Sent -inbox -unread +sent +misdirected-reply # Remove the misdirected tag notmuch tag -misdirected -- tag:misdirected echo "[loop] Done." done < <(notmuch search --output=files --format=text0 tag:misdirected) echo "[autoreply.sh] All done!"