Skip to content

Instantly share code, notes, and snippets.

@julianandrews
Created April 22, 2021 16:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianandrews/3215534f49e1a0b1ec7318a3de9d4bb5 to your computer and use it in GitHub Desktop.
Save julianandrews/3215534f49e1a0b1ec7318a3de9d4bb5 to your computer and use it in GitHub Desktop.
Simple shell script to send email using gmail
# Depends on: msmtp, libsecret-tools
#
# Set password:
# secret-tool store --label="msmtp password for jandrews271@gmail.com" service msmtp username jandrews271@gmail.com
#
# Send mail:
# echo "Message Body" | send-gmail myusername recipient@exmaple.com "My Subject"
send-gmail() {
local user="$1"
local to="$2"
local subject="$3"
local message_body=$(</dev/stdin)
msmtp \
--host smtp.gmail.com \
--port 587 \
--tls=on \
--tls-starttls=on \
--auth=on \
--user "$user" \
--from "$user@gmail.com" \
--passwordeval "secret-tool lookup service msmtp username $user@gmail.com" \
"$to" << EOF
From: $user@gmail.com
To: $to
Subject: $subject
$message_body
EOF
}
@julianandrews
Copy link
Author

julianandrews commented Apr 22, 2021

In theory msmtp supports gnome-keyring natively, but it wasn't working for me. I think since I'm not using a full gnome environment some environment variables aren't set correctly. This seems more robust anyway.

You could of course drop the gnome-keyring/libsecret-tools dependency and just replace the passwordeval line with:

--passwordeval 'echo myverysecretpassword' \

In any case, the password can be a google app password (and must be if you've got 2FA enabled).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment