How can I send gpg encrypted mail automatically from the linux command line?

I'm a little stumped on this one, I've tried using mutt but it doesn't encrypt mail unless it's used interactively.

Does anyone know if you can use the build in mail command to do this some how?


try something like

gpg -ea -r "Recipient name" -o - filename | mail -s "Subject line" recipient@example.com

to send an ascii-armored, public-key-encrypted copy of the file "filename" to a person named "Recipient name" (who is in your gpg keyring) at email address recipient@example.com with the specified subject line.


echo "Your secret message" | gpg -ea -r "Recipient name" | mail -s "Subject" recipient@example.com

to send text directly rather than from a cleartext file on disk.

| improve this answer | |
  • Does that sign the message (with your private key) as well? – teeks99 Aug 6 '13 at 18:11
  • 1
    Add "s" to the gpg command for that - e.g., gpg -eas -r "John Smith" – gbroiles Aug 6 '13 at 22:08

An alternative for those using msmtp.

cat <<EOF | gpg -ea -r "recipient gpg name" | msmtp -a "account default" recipient@mail.com Subject: Hello Kosmos Type your message here, yada yada yada. EOF


| improve this answer | |

Here's a little script I wrote. Save it to ~/username/bin/gpgmail and run chmod 755 gpgmail. Run using gpgmail.

# Send encrypted email
# Requires gpg and mail to be setup

echo "Available keys:"
gpg --list-keys
# Gather variables
echo "Enter public key of recipient:"
read user
echo "Enter email:"
read email
echo "Enter subject:"
read subject
echo "Enter message:"
read message

# Pipe the echoed message to gpg, sign and encrypt it to ascii (-eas), include your key so you can read it,
# include recipients key, pipe to mail with the (unencrypted) subject, send to the given email.
echo "$message" | gpg2 --no-emit-version -eas -r galenasphaug@gmail.com -r $user | mail -s "$subject" $email
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.