Last active
May 11, 2017 13:06
-
-
Save mikejw/0ef963c67dec4085d9f5ee1d54c4f241 to your computer and use it in GitHub Desktop.
Email a file from anywhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# gmutt.sh by Mike Whiting - [email protected] | |
# | |
# 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