mu4e: handle attached image when write msg body to html

This commit is contained in:
Jun Hao 2016-04-16 23:13:47 +08:00
parent f998a0ad1e
commit d16957dc97
2 changed files with 24 additions and 4 deletions

View File

@ -74,14 +74,29 @@ Works for the message view."
"Write the body (either html or text) to a temporary file; "Write the body (either html or text) to a temporary file;
return the filename." return the filename."
(let* ((html (mu4e-message-field msg :body-html)) (let* ((html (mu4e-message-field msg :body-html))
(txt (mu4e-message-field msg :body-txt)) (txt (mu4e-message-field msg :body-txt))
(tmpfile (mu4e-make-temp-file "html"))) (tmpfile (mu4e-make-temp-file "html"))
(attachments (remove-if (lambda (part)
(or (null (plist-get part :attachment))
(null (plist-get part :cid))))
(mu4e-message-field msg :parts))))
(unless (or html txt) (unless (or html txt)
(mu4e-error "No body part for this message")) (mu4e-error "No body part for this message"))
(with-temp-buffer (with-temp-buffer
(insert "<head><meta charset=\"UTF-8\"></head>\n") (insert "<head><meta charset=\"UTF-8\"></head>\n")
(insert (or html (concat "<pre>" txt "</pre>"))) (insert (or html (concat "<pre>" txt "</pre>")))
(write-file tmpfile) (write-file tmpfile)
;; rewrite attachment urls
(mapc (lambda (attachment)
(goto-char (point-min))
(while (re-search-forward (format "src=\"cid:%s\"" (plist-get attachment :cid)) nil t)
(if (plist-get attachment :temp)
(replace-match (format "src=\"%s\"" (plist-get attachment :temp)))
(replace-match (format "src=\"%s%s\"" temporary-file-directory (plist-get attachment :name)))
(mu4e~proc-extract 'save (mu4e-message-field :docid) (plist-get attachment :index) mu4e-decryption-policy temporary-file-directory)
(mu4e-remove-file-later (format "%s%s" temporary-file-directory (plist-get attachment :name))))))
attachments)
(save-buffer)
tmpfile))) tmpfile)))
(defun mu4e-action-view-in-browser (msg) (defun mu4e-action-view-in-browser (msg)

View File

@ -139,6 +139,12 @@ return the result."
(mu4e~get-folder 'mu4e-trash-folder msg)) (mu4e~get-folder 'mu4e-trash-folder msg))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e-remove-file-later (filename)
"Remove FILENAME in a few seconds."
(run-at-time "10 sec" nil
(lambda () (ignore-errors (delete-file filename)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e-make-temp-file (ext) (defun mu4e-make-temp-file (ext)
@ -146,8 +152,7 @@ return the result."
self-destruct in a few seconds, enough to open it in another self-destruct in a few seconds, enough to open it in another
program." program."
(let ((tmpfile (make-temp-file "mu4e-" nil (concat "." ext)))) (let ((tmpfile (make-temp-file "mu4e-" nil (concat "." ext))))
(run-at-time "10 sec" nil (mu4e-remove-file-later tmpfile)
(lambda (fname) (ignore-errors (delete-file fname))) tmpfile)
tmpfile)) tmpfile))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;