Linux – How to send encrypted mail using mailx command in Linux/Unix

emaillinuxpgpshellunix

I want to make my email encrypted as email is confidential. Can anyone please tell me how to encrypt the email using the mailx command. In my shell script I have already used mailx to send the emails, so I want this to be done by mailx command only. Any suggestions ?

Best Answer

Here’s how I did it:

  1. Create text file.
  2. Encrypt that text file.
  3. Attach that encrypted text file to an email.

Here is a Bash script that implements the idea:

#!/bin/bash
date > /tmp/gpgtxt.txt


gpg -ea -r receiver@mail.com /tmp/gpgtxt.txt

cat /tmp/gpgtxt.txt.asc | mailx -s "cli encryption" \
-a /tmp/gpgtxt.txt.asc \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://smtp.gmail.com:587 \
-S from="sender@send.com" \
-S smtp-auth-user=sender@send.com \
-S smtp-auth-password="senderpassword" \
receiver@mail.com