Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fragolinux/1d00490aa1cdee1c7f861ae605a89f66 to your computer and use it in GitHub Desktop.

Select an option

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="[email protected](NAME SURNAME)"
set [email protected]
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" [email protected]
echo SOMETHING THAT WILL GO IN EMAIL BODY | mailx -s "EMAIL SUBJECT" -b [email protected] -c [email protected] [email protected]
# to send a TEXTFILE as body (example, a log or script):
cat TEXTFILE | mailx -s "EMAIL SUBJECT" [email protected]
# to add a FILE as attachment:
echo SOMETHING THAT WILL GO IN EMAIL BODY | mailx -s "EMAIL SUBJECT" -a FILE [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment