Created
April 22, 2021 16:46
-
-
Save julianandrews/3215534f49e1a0b1ec7318a3de9d4bb5 to your computer and use it in GitHub Desktop.
Simple shell script to send email using gmail
This file contains 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
# Depends on: msmtp, libsecret-tools | |
# | |
# Set password: | |
# secret-tool store --label="msmtp password for [email protected]" service msmtp username [email protected] | |
# | |
# Send mail: | |
# echo "Message Body" | send-gmail myusername [email protected] "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 "[email protected]" \ | |
--passwordeval "secret-tool lookup service msmtp username [email protected]" \ | |
"$to" << EOF | |
From: [email protected] | |
To: $to | |
Subject: $subject | |
$message_body | |
EOF | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:In any case, the password can be a google app password (and must be if you've got 2FA enabled).