Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 13 down vote accepted

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.

or

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.

share|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

voilà

share|improve this answer

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

#!/bin/bash
# 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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