This function is useful in a hook

There are three ways to combine Emacs mail clients such as Gnus with version 3 of the BBDB rolodex and GPG encryption.

First, you can just use the provided (keys) to manually hook in GPG in your mail message.

Or, second, you automate this, using bbdb-pgp.el, which I wrote about on 14 October, here.

And here is a third way, more or less synonymous with the second.

I touched up the package that was written by Uwe Brauwer for BBDB version 2. There were a few minor changes needed to make it work with BBDB version 3.

From the changelog:

I replaced these functions:

bbdb-canonicalize-net-hook by bbdb-canonicalize-mail-function, bbdb-search-simple by bbdb-message-search and bbdb-record-getprop record by bbdb-record-field (car ;; record).

And I added five local variables that Uwe had stored elswhere: my-mml-use-pgpmime, my-encrypt-only-pgpmime, my-encrypt-and-sign-pgpmime, my-encrypt-only-pgp and my-encrypt-and-sign-pgp.

To use this code, put the package in your emacs load path. Then hook it into Emacs Message:

(add-hook 'message-send-hook 'bbdbencryptadpt-try-bbdbencrypt)

The package itself:

;;; bbdbadapt-encrypt.el --- Use the BBDB to encrypt/sign (smime or pgp)

;; Copyright (C) 2009 Uwe Brauer

;; Author: Uwe Brauer oub (at )
;; Maintainer: Uwe Brauer oub (at )
;; Created: 23 Feb 2013
;; Version: 1.1, 19 October 2013
;; Keywords:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 1, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.

;; A copy of the GNU General Public License can be obtained from this
;; program's author (send electronic mail to or from
;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
;; 02139, USA.

;; This code allows to encrypt/sign message either by using pgp
;; (pgpmime) or smime according to the revant bbdb entry which should
;; read for for example:
;; encrypt: smime 

;;; Change log:
;; Adapted on 19 October 2013 to work with BBDB version 3 
;; by Gijs Hillenius gijs (at )
;; replaced bbdb-canonicalize-net-hook by
;; bbdb-canonicalize-mail-function
:; replaced bbdb-search-simple by bbdb-message-search
;; replaced bbdb-record-getprop record by bbdb-record-field (car
;; record)
;; added local variables my-mml-use-pgpmime, my-encrypt-only-pgpmime,
;; my-encrypt-and-sign-pgpmime, my-encrypt-only-pgp and
;; my-encrypt-and-sign-pgp

;;; Code:

(defvar my-mml-use-pgpmime t
  "*Variable which controls to use pgpmime or pgp ASCII, default is pgpmime=

(defun my-encrypt-only-pgpmime ()
  "Only encypt with pgpmime, DONT sign!"
  (mml-secure-message-encrypt-pgpmime 1))

(defun my-encrypt-and-sign-pgpmime ()
  "Encrypt and  sign with pgpmime!"
  (mml-secure-message-encrypt-pgpmime nil))

(defun my-encrypt-only-pgp ()
  "Only encypt with pgp ASCII, DONT sign!"
  (mml-secure-message-encrypt-pgp 1))

(defun my-encrypt-and-sign-pgp ()
  "Encrypt and  sign with pgp ASCII!"
  (mml-secure-message-encrypt-pgp nil))

(defconst bbdbadapt-encrypt-version (concat "0." (substring "$Revision: 1.1 $" 13 14))
        "$Id: bbdbadapt-encrypt.el,v 1.1 2013/02/10 08:49:57 oub Exp oub $
Report bugs to: Uwe Brauer")

(defun bbdbencryptadpt-search-record (to)
  "Search a BBDB record associated to TO or return NIL."
  (let* ((data (mail-extract-address-components to))
         (name (car data))
         (net  (car (cdr data))))
    (if (equal name net) (setq name nil))
    (if (and net bbdb-canonicalize-mail-function)
        (setq net (bbdb-canonicalize-address net)))
    (bbdb-message-search name net)))

(defun bbdbencryptadpt-try-bbdbencrypt ()
  "Try to adapt non-interactively the current bbdbencrypt. 
This function looks silently in the current message to find how to
choose the bbdbencrypt. It does nothinng if not enough information is
present. This function is useful in a hook."
    (condition-case nil
          (goto-char (point-min))
          (let ((record (bbdbencryptadpt-search-record 
                         ;;(bbdb-extract-field-value "To"))))
                         (bbdb-message-header "To"))))
                        (if record
                                (let ((signame (bbdbencryptadpt-retrieve-bbdbencrypt record)))
                                  (when (and (stringp signame) (string= signame "smime"))
                                  (when (and (stringp signame) (string= signame "smimencrypt"))
                                  (when (and (stringp signame) (string= signame "pgp"))

(defun bbdbencryptadpt-retrieve-bbdbencrypt (&optional record) 
  "Retrieve the bbdbencrypt (a symbol) associated to a mailee.
The search is done through a BBDB record. "
  (if (not record)
        (goto-char (point-min))
        (let* ((to  (bbdb-extract-field-value "To"))
               (rec (bbdbencryptadpt-search-record to)) )
          (if rec (bbdbencryptadpt-do-retrieve-bbdbencrypt rec)
            (progn (message "No bound record")
    (bbdbencryptadpt-do-retrieve-bbdbencrypt record)))

(defun bbdbencryptadpt-do-retrieve-bbdbencrypt (record)
  (let ((signame
                 (bbdb-record-field (car record) 'encrypt)))
    (if (stringp signame)
        (setq signame signame))

(provide 'bbdbadapt-encrypt)