I use emacs for email and org capture to keep track of my open loops. Recently I have been trying to combine the two and capture tasks that link to emails. I wanted to tag my capture items based on the inbox they fall into, :@home: for my personal and :@school for my school emails. I cobbled together this function based upon different stackexchange posts and figured I would share it here for anyone who wanted to do the same.

Code

This function generates the tag based on the :to address of the stored email. If the email is my personal (alexday135@gmail) the tag is :@home: and any other email is tagged :@school:.

(defun create-mail-tag ()
  "Create the tag for the todo"
  (let ((to (plist-get org-store-link-plist :to)))
    (if (equal to "'Alex Day' <alexday135@gmail.com>")
        ":@home:"
        ":@school:")))

This is the actual capture template. This just creates a new item in my inbox with a link to the email (%a), the tag (%(create-mail-tag)), and then the date the email was sent on the line below. No capture window pops up and it is immediately added to my inbox.

(setq org-capture-templates
    '(("e" "Email" entry (file+headline "~/Dropbox/gtd/inbox.org" "Inbox")
        "* TODO [#A] Reply: %a %(create-mail-tag)\n%:date-timestamp"
        :immediate-finish t)))