Open mime attachments in Emacs

As shown in a previous post, Emacs has some nice email readers. However, the default handling of most attachments (MIME types in traditional programming speak) is just to save the file. I have configured Emacs to rely on Windows for opening some types of attachments that I find often. As an example, I show below how to open PDFs and Word files, although it can be extended to other file types. Place this code in your Emacs' init.el file.

(ctree-set-calist-strictly
 'mime-acting-condition
 '((mode . "play")
   (type . application)
   (subtype . pdf)
   (method . jjgr-default-mime-open)))
(ctree-set-calist-strictly
 'mime-acting-condition
 '((mode . "play")
   (type . application)
   (subtype . vnd.openxmlformats-officedocument.wordprocessingml.document)
   (method . jjgr-default-mime-open)))

(defun jjgr-default-mime-open (&optional a b)
  (let* ((entity (get-text-property (point) 'mime-view-entity))
         (name (mime-entity-safe-filename entity))
         (filename (concat (getenv "HOMEDRIVE")
                           (getenv "HOMEPATH")
                           "\\Downloads\\"
                           name)))
    (mime-write-entity-content entity filename)
    (print `(start-process "cmd.exe" "*Play*" "cmd.exe" ,filename))
    (start-process "cmd.exe" nil "cmd.exe" "/c" "start" "/b" filename)))