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

21

11

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?

Rwky

Posted 2010-09-14T12:28:27.700

Reputation: 648

Answers

25

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.

gbroiles

Posted 2010-09-14T12:28:27.700

Reputation: 3 938

Does that sign the message (with your private key) as well? – teeks99 – 2013-08-06T18:11:34.170

1Add "s" to the gpg command for that - e.g., gpg -eas -r "John Smith" – gbroiles – 2013-08-06T22:08:28.477

0

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

clownfishhuman

Posted 2010-09-14T12:28:27.700

Reputation: 13

0

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à

qhaz

Posted 2010-09-14T12:28:27.700

Reputation: 21