## Better integration of org-mode and email

| categories: | tags: | View Comments

I like to email org-mode headings and content to people. It would be nice to have some records of when a heading was sent, and to whom. We store this information in a heading. It is pretty easy to write a simple function that emails a selected region.

(defun email-region (start end)
"Send region as the body of an email."
(interactive "r")
(let ((content (buffer-substring start end)))
(compose-mail)
(message-goto-body)
(insert content)
(message-goto-to)))


that function is not glamorous, and you still have to fill in the email fields, and unless you use gnus and org-contacts, the only record keeping is through the email provider.

What I would like is to send a whole heading in an email. The headline should be the subject, and if there are TO, CC or BCC properties, those should be used. If there is no TO, then I want to grab the TO from the email after you enter it and store it as a property. You should be able to set OTHER-HEADERS as a property (this is just for fun. There is no practical reason for this yet). After you send the email, it should record in the heading when it was sent.

It turned out that is a relatively tall order. While it is easy to setup the email if you have everything in place, it is tricky to get the information on TO and the time sent after the email is sent. Past lispers had a lot of ideas to make this possible, and a day of digging got me to the answer. You can specify some "action" functions that get called at various times, e.g. after sending, and a return action when the compose window is done. Unfortunately, I could not figure out any way to do things except to communicate through some global variables.

So here is the code that lets me send org-headings, with the TO, CC, BCC properties, and that records when I sent the email after it is sent.

(defvar *email-heading-point* nil
"global variable to store point in for returning")

"global variable to store to address in email")

"after returning from compose do this"
(org-set-property "SENT-ON" (current-time-string))
;; reset this incase you added new ones
)

(defun email-send-action ()
"send action for compose-mail"

"Send the current org-mode heading as the body of an email, with headline as the subject.

use these properties
TO

save when it was sent as s SENT property. this is overwritten on
subsequent sends. could save them all in a logbook?
"
(interactive)
; store location.
(org-mark-subtree)
(let ((content (buffer-substring (point) (mark)))
(TO (org-entry-get (point) "TO" t))
(CC (org-entry-get (point) "CC" t))
(BCC (org-entry-get (point) "BCC" t))
(continue nil)
(switch-function nil)
(yank-action nil)
(send-actions '((email-send-action . nil)))

(compose-mail TO SUBJECT OTHER-HEADERS continue switch-function yank-action send-actions return-action)
(message-goto-body)
(insert content)
(when CC
(message-goto-cc)
(insert CC))
(when BCC
(message-goto-bcc)
(insert BCC))
(if TO
(message-goto-body)
(message-goto-to))
))


This works pretty well for me. Since I normally use this to send tasks to people, it keeps the task organized where I want it, and I can embed an org-id in the email so if the person replies to it telling me the task is done, I can easily navigate to the task to mark it off. Pretty handy.