How do I send emails with Unix and Gmail?

Why?

If your Unix-based operating system fails, you’ll never know if you don’t forward system emails. When your system is acting up, it will generate emails. However, a Unix system doesn’t send emails by default because spammers hate everyone.

How?

  1. Install mailx (use a package manager)
  2. Edit your mail.rc
vi /etc/mail.rc
  1. Paste it at the bottom of your mail.rc.
set smtp=smtps://smtp.gmail.com:465
set smtp-auth=login
set smtp-auth-user=USERNAME@gmail.com
set smtp-auth-password=YOURPASSWORD
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/ # You might need to adjust this depending on your Unix OS
  1. Test it out
echo "My Test Message" | mail -v -s "My Test Subject" email@some.domain.tld
  1. If you’re lucky, you’ll see something like this and an email in your inbox.
Resolving host smtp.gmail.com . . . done.
Connecting to 74.125.199.108:465 . . . connected.
220 smtp.gmail.com ESMTP u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> EHLO homelab.is-leet.com
250-smtp.gmail.com at your service, [50.126.101.7]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
>>> AUTH LOGIN
334 VXNlcm5hbWU6
>>> amVmZmFiYWlsZXlAZ21haWwuY29t
334 UGFzc3dvcmQ6
>>> eHZhdHNldXhzb3FvcGt3ZA==
235 2.7.0 Accepted
>>> MAIL FROM:<jeff@homelab.is-leet.com>
250 2.1.0 OK u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> RCPT TO:<jeffabailey@gmail.com>
250 2.1.5 OK u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> DATA
354  Go ahead u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> .
250 2.0.0 OK  1650153154 u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> QUIT
221 2.0.0 closing connection u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp

Have a great day! 😎