Last Updated: July 31, 2016
· 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 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, 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://smtp.gmail.com:587 -S from="username@gmail.com(John Doe)" -S smtp-auth-user=username@gmail.com -S smtp-auth-password=s0m3p@zzW0rD -S ssl-verify=ignore -S nss-config-dir=~/.certs recipient@some.com

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://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

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

$ echo -e "Mail body text" | mailx -A gmail -s "Mail subject" recipient@some.com

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

$ echo -e "Mail body text" | mailx -A gmail -s "Mail subject" -b bcc_user@some.com -c cc_user@some.com recipient@some.com

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" client@company.com

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

Say Thanks

7 Responses
Add your response


Wow, very detailed. Nice!

over 1 year ago ·
Kerie avatar

Great tip

over 1 year ago ·

i love it!

over 1 year ago ·
Default profile 0 normal

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 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtFO
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 https://support.google.com/mail/bin/answer.py?answer=78754 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:

-connect smtp.gmail.com:465

i had to do:

-connect encrypted.google.com:443

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

over 1 year ago ·
Filed Under