Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fragolinux/1d00490aa1cdee1c7f861ae605a89f66 to your computer and use it in GitHub Desktop.
Save fragolinux/1d00490aa1cdee1c7f861ae605a89f66 to your computer and use it in GitHub Desktop.
email from command line using gmail as smtp
# how to send emails, with attachments, from linux command line, using gmail as relay server
# you need to allow insecure apps: https://support.google.com/accounts/answer/6010255
sudo apt-get install -y libnss3-tools heirloom-mailx
mkdir ~/.certs
certutil -N --empty-password -d ~/.certs
echo -n | openssl s_client -connect smtp.gmail.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/gmail.crt
certutil -A -n "Google Internet Authority" -t "C,," -d ~/.certs -i ~/.certs/gmail.crt
# now create a new config file (example: nano $HOME/.mailrc) containing the following lines - change USER and PASSWORD as you need:
set smtp-use-starttls
set ssl-verify=ignore
set smtp-auth=login
set smtp=smtp://smtp.gmail.com:587
set from="USER@gmail.com(NAME SURNAME)"
set smtp-auth-user=USER@gmail.com
set smtp-auth-password=PASSWORD
set ssl-verify=ignore
set nss-config-dir=~/.certs
# example usage - switches -b and -c are optional:
echo SOMETHING THAT WILL GO IN EMAIL BODY | mailx -s "EMAIL SUBJECT" recipient@some.com
echo SOMETHING THAT WILL GO IN EMAIL BODY | mailx -s "EMAIL SUBJECT" -b bcc_user@some.com -c cc_user@some.com recipient@some.com
# to send a TEXTFILE as body (example, a log or script):
cat TEXTFILE | mailx -s "EMAIL SUBJECT" recipient@some.com
# to add a FILE as attachment:
echo SOMETHING THAT WILL GO IN EMAIL BODY | mailx -s "EMAIL SUBJECT" -a FILE recipient@some.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment