Skip to content

Instantly share code, notes, and snippets.

@mikejw
Last active May 11, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikejw/0ef963c67dec4085d9f5ee1d54c4f241 to your computer and use it in GitHub Desktop.
Save mikejw/0ef963c67dec4085d9f5ee1d54c4f241 to your computer and use it in GitHub Desktop.
Email a file from anywhere
#!/bin/bash
# gmutt.sh by Mike Whiting - mikejw3@gmai.com
#
# With one line send file attachments securely
# using mutt from gmail account and then remove generated
# config.
#
# You will need to set up 2-factor authentication with gmail
# and add an app password here:
# https://myaccount.google.com/apppasswords
#
# WARNING: may destroy pre-existing mutt config
#
# Download with:
# curl -s --output gmutt.sh https://gist.githubusercontent.com/mikejw/0ef963c67dec4085d9f5ee1d54c4f241/raw
#
# Usage:
# ./gmutt.sh <gmail username> <filename> <recipient email>
MUTTRC=~/.muttrc
USERNAME=$1
FILE=$2
RECIPIENT=$3
ERROR=1
read -r -d '' CONF << 'EOF'
\nset imap_user = "[USERNAME]@gmail.com"
\nset imap_pass = "[PASSWORD]"
\nset smtp_url = "smtp://[USERNAME]@smtp.gmail.com:587/"
\nset smtp_pass = "[PASSWORD]"
\nset from = "[USERNAME]@gmail.com"
\nset realname = "Your Real Name"
\nset folder = "imaps://imap.gmail.com:993"
\nset spoolfile = "+INBOX"
\nset postponed="+[Gmail]/Drafts"
\nset header_cache=~/.mutt/cache/headers
\nset message_cachedir=~/.mutt/cache/bodies
\nset certificate_file=~/.mutt/certificates
\nset move = no
EOF
if [ -z $FILE ] || [ ! -f $FILE ]; then
echo "Error: Woops, can't find target file"
elif [ -z "$RECIPIENT" ]; then
echo "Error: No email address supplied"
elif [ -z "$USERNAME" ]; then
echo "Error: No gmail username provided"
else
echo "Sending ${FILE} to ${RECIPIENT}..."
echo "Installing deps..."
apt-key update -qq > /dev/null 2>&1
apt-get update -y -qq > /dev/null 2>&1
apt-get install -y -qq \
openssl mutt \
> /dev/null 2>&1
read -s -p "Enter gmail password: " PASSWORD
if [ -z "$PASSWORD" ]; then
echo "Error: No password"
else
touch ${MUTTRC}
grep -Fq "gmail" ${MUTTRC}
if [[ $? -eq 1 ]]; then
echo ""
echo "Configuring mutt..."
CONF=$(echo $CONF | sed "s/\[USERNAME\]/${USERNAME}/g")
CONF=$(echo $CONF | sed "s/\[PASSWORD\]/${PASSWORD}/g")
echo -e $CONF > ${MUTTRC}
fi
echo "Sending an attachment." | mutt -s "Attachment" ${RECIPIENT} -a ${FILE}
echo "Clean up..."
rm ${MUTTRC}
echo "Done."
ERROR=0
fi
fi
if [ "$ERROR" -eq "1" ]; then
echo "Usage: ./gmutt.sh <gmail username> <filename> <recipient email>"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment