Sending Mail Semi-Programmatically In Emacs

I have an ongoing project that I manage with an Org mode file. The file has the structure

* [2012-03-21 Wed]
** Second Level Headline
   Some information about the second level headline

where the level one headlines are the date and the level two headlines describe an event that happened on that date.

Most days, there are two or three events and every time I enter one I email a teammate with just the second level headline. I used to do that by simply retyping the headline into an email or maybe cutting and pasting. Then I wondered if I could automate that a bit. Here's what I can up with:

(defun mail-org-headline ()
  (let* ((bnds (bounds-of-thing-at-point 'line))
         (line (buffer-substring-no-properties (car bnds) (cdr bnds))))
     (insert "To:\nSubject: Headline Report\n\n")
     (insert (substring line 3))

With the point on the headline I want to send, I call mail-org-headline and the email is automatically prepared and pops up in the OS X Mail App ready to send. Because the email is prepared in a temporary buffer, there is nothing to clean up in Emacs.

The call to substring on the next-to-last line strips off the two stars and space. Since I use the native on my Mac, I've never bothered configuring the Emacs mail apparatus. If I did, I could probably get Emacs to send the email directly without invoking Mail but I like it the way it is because it gives me a chance to check the email before I send it.

I don't imagine many people have the same use case as I described above but I decided it was worth sharing to show how to send mail programmatically from Emacs. If you want to do something similar, be aware that you can use most of the usual header fields in addition to To: and Subject:.

This entry was posted in Programming and tagged , , . Bookmark the permalink.
  • Michael Norrish

    Where does mail-send come from?

    • jcs

      It's in sendmail.el on my 23.3 version of Emacs.

  • That's great fun. Desperately trying to think of a use for it now. :)