Logging sent messages to Org Mode with message-sent-hook
| org, emacs
I wanted to e-mail all the EmacsConf speakers who had already uploaded
their videos, and I wanted to keep track of the fact that I'd mailed
them by adding a note to the :LOGBOOK:
drawer in their talk heading.
That way, organizers can just look at the logbook to see if we've
mailed someone instead of digging through our mailboxes.
org-store-log-note
assumes that it's called from the log buffer
created by org-add-log-note
. It doesn't seem to have a smaller
function that can be called to store notes non-interactively, but
that's okay. We can just set up the correct markers and call it from a
temporary buffer.
(defun emacsconf-add-to-logbook (note) "Add NOTE as a logbook entry for the current subtree." (move-marker org-log-note-return-to (point)) (move-marker org-log-note-marker (point)) (with-temp-buffer (insert note) (let ((org-log-note-purpose 'note)) (org-store-log-note))))
Then it's convenient to have a function that adds a note to a specified talk:
(defun emacsconf-add-to-talk-logbook (talk note) "Add NOTE as a logbook entry for TALK." (interactive (list (emacsconf-complete-talk) (read-string "Note: "))) (save-excursion (emacsconf-with-talk-heading talk (emacsconf-add-to-logbook note))))
I discard many drafts on the way to finalizing the process, so I want
the note to be stored only after I actually send the mail. That's the
job of message-sent-hook
. My mail merge function calls
compose-mail
, sets up the body of the buffer, and then adds a lambda
function to message-sent-hook
to file the note in the logbook when
sent.
(add-hook 'message-sent-hook `(lambda () (mapc (lambda (o) (emacsconf-add-to-talk-logbook o "Sent speaker-after-video email")) (list ,@(mapcar (lambda (talk) (plist-get talk :slug)) talks)))) nil t)
To see the mail merge code in context, you can check out the TODO entry at https://emacsconf.org/2022/organizers-notebook/#speaker-after-video . It uses functions from emacsconf.el and emacsconf-mail.el at https://git.emacsconf.org/emacsconf-el/ .