π

Getting Emails into Org-Mode

Show Sidebar

Update 2017-03-17: Link to the method of Sam Flint

This article is for Org-mode users who are looking for a possibility to get emails into org-mode which is independent of their email client (MUA). You're going to need access to your mail server for this method.

From time to time, I get tasks by email where the email body contains helpful information on that task. Therefore, it is nice to have a method that gets the email header and body into my Org-mode system.

I do not use an Emacs-based mail setup, which would make this task an easy one depending on your MUA solution. Therefore, I created a workaround by bouncing relevant emails to a special email address I use for forwarding emails to Org-mode: orgmode@MY-DOMAIN.AT

I am maintaining my own email server and I am (still) using procmail to filter/preprocess emails. Please start with mailfilter instead if you do a fresh start. In contrast to good old procmail, mailfilter is maintained and offers a modern interface.

So when I receive a new email to that specific email address the following procmail filter rule matches:

 :0
 * ^(Resent-To|X-Original-To|To|Bcc|Cc): orgmode@MY-DOMAIN.AT
 {
     :0
     |/home/karl/src/vk-add-email-to-org.sh
 }	  

The filter rule pipes the email into the shell script vk-add-email-to-org.sh which looks like this:

 #!/bin/sh

 ## generates an org-file-entry from the email sent through stdin

 ORGFILE="/home/karl/org/inbox.org"

 ## build email by adding input from stdin line by line:
 while IFS= read -r line
 do
   email="${email}
   ${line}"
 done

 ## extract header information:
 subject=`echo "${email}"|formail -x "Subject"`
 header=`echo "${email}"|formail -X "From" -X "Subject" -X "Date" -X "Message-ID" -X "To"`
 body=`echo "${email}"| sed -e '1,/^$/ d'`

 ## append mail header and body as new TODO heading to ORGFILE:
 echo "* TODO Email:"${subject} >> ${ORGFILE}
 echo >> ${ORGFILE}
 echo "#+BEGIN_QUOTE" >> ${ORGFILE}
 echo "${header}" >> ${ORGFILE}
 echo "#+END_QUOTE" >> ${ORGFILE}
 echo  >> ${ORGFILE}	  

Since all my computers have a synchronized version of my Org-mode files, the mail information is appended to my current Org-mode inbox file. This is the same file I use for getting in stuff from MobileOrg.

Refiling from inbox.org is the same process as refiling my input from MobileOrg which I do on a daily basis.

So maybe this is also a handy workflow for you as well. I do like it because it is completely independent of my mail client and the hosts I am working on. Please drop me an email or comment below when you do have remarks or found similar methods.

Sam Flint

Sam Flint published his version of the same workflow that is using fetchmail, procmail, ripMIME and Pandoc. It is an extended and more elaborated version of my workflow.

Comment via email or via Disqus comments below: