Last Updated: December 30, 2020
· rawswift

Send mail like a boss!

This will basically let you send email from the terminal, using mailx and Gmail as SMTP server.

First, create a certificate directory then create new certificate and key databases:

$ mkdir ~/.certs
$ certutil -N -d ~/.certs

Then fetch the certificate from Gmail and import the cert file into the new database:

$ echo -n | openssl s_client -connect | 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, send a mail:

$ echo -e "Email content" | mailx -v -s "Email subject" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp:// -S from=" Doe)" -S -S smtp-auth-password=s0m3p@zzW0rD -S ssl-verify=ignore -S nss-config-dir=~/.certs

Woah! That is friggin' long! Let's make it a bit shorter by adding the account settings in /etc/mail.rc: (insert at the end of the file)

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

Let's try again and send a mail to a single recipient:

$ echo -e "Mail body text" | mailx -A gmail -s "Mail subject"

...or send to multiple recipients: (-b = BCC, -c = CC)

$ echo -e "Mail body text" | mailx -A gmail -s "Mail subject" -b -c

Now, that's much better!

Use Case: Send work report like a boss!

Based on Create your work report using Git Log

$ git report | mailx -A gmail -s "Last Week Activity Report"

Sources: Sending email from mailx command in linux using Gmails SMTP, mailx and Gmail NSS config dir, SMTP from bash gives error in certificate peers certificate issuer

8 Responses
Add your response

Wow, very detailed. Nice!

over 1 year ago ·

Great tip

over 1 year ago ·

i love it!

over 1 year ago ·

it works but I get this message while sending. Should I worry about it?

Error in certificate: Peer's certificate issuer is not recognized

over 1 year ago ·

Error in certificate: Peer's certificate issuer is not recognized.

smtp-server: 534-5.7.14 <
smtp-server: 534-5.7.14 nWhmXiLx099999ddZaWck5du8QKDFOkKft
smtp-server: 534-5.7.14 N
smtp-server: 534-5.7.14 z427IQco7GRD-wfDoz2CiFV7JJc7Cc_bb0AiOksdX4JSvYrdi99999999999vcN5XhwQpyWgq
smtp-server: 534-5.7.14 q6TjxrzmzT2Vg1UzEzuQgpgYfnRs> Please log in via your web browser and
smtp-server: 534-5.7.14 then try again.
smtp-server: 534-5.7.14 Learn more at
smtp-server: 534 5.7.14 b109sm15313249qgf.40 - gsmtp
"/root/dead.letter" 11/373
. . . message not sent.

and. gmail stop my attempt and I got an email from them indicating that the login form my vps was suspicious... :(

I guess. I will to try with Mandrill

the message got never sent in my case.

if I did something wrong or there are any suggestions...

over 1 year ago ·

On Ubuntu, I tried it with /etc/mail.rc.

Account gmail does not exist.
/usr/lib/sendmail: No such file or directory
"/home/vagrant/dead.letter" 9/236
. . . message not sent.

Googling around, SOLUTION, I found out that /etc/nail.rc solves the problem.

over 1 year ago ·

thank you so much for writing this. i had to make one minor change to your excellent instructions. where you said:


i had to do:


but besides that very minor point, it worked great. thanks again.

over 1 year ago ·

this article has become my go-to instructions for sending email to Gmail. thank you so much.
is there any chance you can write a similar article sending mail through mailx using Google Oath2 clientId & clientSecret as nodeMailer uses?

9 months ago ·