Last Updated: December 31, 2020
·
894
· Osvaldo Zonetti

Sending emails easily from the command line

This tip is based on Ryan Yonzon's tip: https://coderwall.com/p/ez1x2w

I just added some fixes I needed to make in order to run on Ubuntu.

Preparing

Installing dependencies:

$ sudo apt-get install heirloom-mailx libnss3-tools sendmail -y

Creating the "certs" directory and importing the cert file from Gmail server:

$ mkdir ~/.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

Configuring

Create the file ~/.mailrc and add the following configuration (updating accordingly to your account):

account gmail {
  set smtp-use-starttls
  set ssl-verify=ignore
  set smtp-auth=login
  set smtp=smtp://smtp.gmail.com:587
  set from="username@gmail.com(John Doe)"
  set smtp-auth-user=username@gmail.com
  set smtp-auth-password="s0m3p@zzW0rD"
  set ssl-verify=ignore
  set nss-config-dir=/home/path/to/.certs
}

Sending emails

$ echo "Email content" | mailx -A gmail -s "Email subject" destination@email.com