Skip to content

Instantly share code, notes, and snippets.

@DamienCassou
Last active March 27, 2018 13:01
Show Gist options
  • Save DamienCassou/2c97afdba7b80251eb6cc00be82e6cfd to your computer and use it in GitHub Desktop.
Save DamienCassou/2c97afdba7b80251eb6cc00be82e6cfd to your computer and use it in GitHub Desktop.
Automatically encrypt Emacs outgoing emails if there is a public key for every recipient
(defun my/can-encrypt-message-p ()
"Return non-nil if current message can be encrypted.
I.e., the keyring has a public key for each recipient."
(let ((recipients (seq-map #'cadr ; only take email address, not recipient name
(seq-mapcat (lambda (header)
(let ((header-value (message-fetch-field header)))
(and
header-value
(mail-extract-address-components header-value t))))
'("To" "CC" "BCC"))))
(context (epg-make-context epa-protocol)))
(seq-every-p (lambda (recipient)
(not (seq-empty-p (epg-list-keys context recipient))))
recipients)))
(defun my/add-encryption-mark-if-possible ()
"Add MML tag to encrypt message when there is a key for each recipient."
(when (my/can-encrypt-message-p)
(mml-secure-message-sign-encrypt)))
(add-hook 'message-send-hook #'my/add-encryption-mark-if-possible)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment