org-mime.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;; org-mime.el --- org html export for text/html MIME emails
  2. ;; Copyright (C) 2010 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: mime, mail, email, html
  5. ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; WYSWYG, html mime composition using org-mode
  24. ;;
  25. ;; For mail composed using the orgstruct-mode minor mode, this
  26. ;; provides a function for converting all or part of your mail buffer
  27. ;; to embedded html as exported by org-mode. Call `org-mime-htmlize'
  28. ;; in a message buffer to convert either the active region or the
  29. ;; entire buffer to html.
  30. ;;
  31. ;; Similarly the `org-mime-org-buffer-htmlize' function can be called
  32. ;; from within an org-mode buffer to convert the buffer to html, and
  33. ;; package the results into an email handling with appropriate MIME
  34. ;; encoding.
  35. ;;
  36. ;; you might want to bind this to a key with something like the
  37. ;; following message-mode binding
  38. ;;
  39. ;; (add-hook 'message-mode-hook
  40. ;; (lambda ()
  41. ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
  42. ;;
  43. ;; and the following org-mode binding
  44. ;;
  45. ;; (add-hook 'org-mode-hook
  46. ;; (lambda ()
  47. ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
  48. ;;; Code:
  49. (require 'cl)
  50. (defcustom org-mime-default-header
  51. "#+OPTIONS: latex:t\n"
  52. "Default header to control html export options, and ensure
  53. first line isn't assumed to be a title line."
  54. :group 'org-mime
  55. :type 'string)
  56. (defcustom org-mime-library 'mml
  57. "Library to use for marking up MIME elements."
  58. :group 'org-mime
  59. :type '(choice 'mml 'semi 'vm))
  60. (defcustom org-mime-preserve-breaks t
  61. "Used as temporary value of `org-export-preserve-breaks' during
  62. mime encoding."
  63. :group 'org-mime
  64. :type 'boolean)
  65. (defcustom org-mime-fixedwith-wrap
  66. "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
  67. "Format string used to wrap a fixedwidth HTML email."
  68. :group 'org-mime
  69. :type 'string)
  70. (defcustom org-mime-html-hook nil
  71. "Hook to run over the html buffer before attachment to email.
  72. This could be used for example to post-process html elements."
  73. :group 'org-mime
  74. :type 'hook)
  75. (mapc (lambda (fmt)
  76. (eval `(defcustom
  77. ,(intern (concat "org-mime-pre-" fmt "-hook"))
  78. nil
  79. (concat "Hook to run before " fmt " export.\nFunctions "
  80. "should take no arguments and will be run in a "
  81. "buffer holding\nthe text to be exported."))))
  82. '("ascii" "org" "html"))
  83. (defcustom org-mime-send-subtree-hook nil
  84. "Hook to run in the subtree in the Org-mode file before export.")
  85. (defcustom org-mime-send-buffer-hook nil
  86. "Hook to run in the Org-mode file before export.")
  87. ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
  88. (defun org-mime-change-element-style (element style)
  89. "Set new default htlm style for <ELEMENT> elements in exported html."
  90. (while (re-search-forward (format "<%s" element) nil t)
  91. (replace-match (format "<%s style=\"%s\"" element style))))
  92. (defun org-mime-change-class-style (class style)
  93. "Set new default htlm style for objects with classs=CLASS in
  94. exported html."
  95. (while (re-search-forward (format "class=\"%s\"" class) nil t)
  96. (replace-match (format "class=\"%s\" style=\"%s\"" class style))))
  97. ;; ;; example addition to `org-mime-html-hook' adding a dark background
  98. ;; ;; color to <pre> elements
  99. ;; (add-hook 'org-mime-html-hook
  100. ;; (lambda ()
  101. ;; (org-mime-change-element-style
  102. ;; "pre" (format "color: %s; background-color: %s;"
  103. ;; "#E6E1DC" "#232323"))
  104. ;; (org-mime-change-class-style
  105. ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
  106. (defun org-mime-file (ext path id)
  107. "Markup a file for attachment."
  108. (case org-mime-library
  109. ('mml (format
  110. "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
  111. ext path id))
  112. ('semi (concat
  113. (format
  114. "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
  115. ext id)
  116. (base64-encode-string
  117. (with-temp-buffer
  118. (set-buffer-multibyte nil)
  119. (binary-insert-encoded-file path)
  120. (buffer-string)))))
  121. ('vm "?")))
  122. (defun org-mime-multipart (plain html)
  123. "Markup a multipart/alternative with text/plain and text/html
  124. alternatives."
  125. (case org-mime-library
  126. ('mml (format (concat "<#multipart type=alternative><#part type=text/plain>"
  127. "%s<#part type=text/html>%s<#/multipart>\n")
  128. plain html))
  129. ('semi (concat
  130. "--" "<<alternative>>-{\n"
  131. "--" "[[text/plain]]\n" plain
  132. "--" "[[text/html]]\n" html
  133. "--" "}-<<alternative>>\n"))
  134. ('vm "?")))
  135. (defun org-mime-replace-images (str current-file)
  136. "Replace images in html files with cid links."
  137. (let (html-images)
  138. (cons
  139. (replace-regexp-in-string ;; replace images in html
  140. "src=\"\\([^\"]+\\)\""
  141. (lambda (text)
  142. (format
  143. "src=\"cid:%s\""
  144. (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
  145. (match-string 1 text)))
  146. (path (expand-file-name
  147. url (file-name-directory current-file)))
  148. (ext (file-name-extension path))
  149. (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
  150. (add-to-list 'html-images
  151. (org-mime-file (concat "image/" ext) path id))
  152. id)))
  153. str)
  154. html-images)))
  155. (defun org-mime-htmlize (arg)
  156. "Export a portion of an email body composed using `mml-mode' to
  157. html using `org-mode'. If called with an active region only
  158. export that region, otherwise export the entire body."
  159. (interactive "P")
  160. (let* ((region-p (org-region-active-p))
  161. (html-start (or (and region-p (region-beginning))
  162. (save-excursion
  163. (goto-char (point-min))
  164. (search-forward mail-header-separator)
  165. (+ (point) 1))))
  166. (html-end (or (and region-p (region-end))
  167. ;; TODO: should catch signature...
  168. (point-max)))
  169. (raw-body (buffer-substring html-start html-end))
  170. (tmp-file (make-temp-name (expand-file-name
  171. "mail" temporary-file-directory)))
  172. (body (org-export-string raw-body 'org (file-name-directory tmp-file)))
  173. ;; because we probably don't want to skip part of our mail
  174. (org-export-skip-text-before-1st-heading nil)
  175. ;; because we probably don't want to export a huge style file
  176. (org-export-htmlize-output-type 'inline-css)
  177. ;; makes the replies with ">"s look nicer
  178. (org-export-preserve-breaks org-mime-preserve-breaks)
  179. ;; to hold attachments for inline html images
  180. (html-and-images
  181. (org-mime-replace-images
  182. (org-export-string raw-body 'html (file-name-directory tmp-file))
  183. tmp-file))
  184. (html-images (unless arg (cdr html-and-images)))
  185. (html (org-mime-apply-html-hook
  186. (if arg
  187. (format org-mime-fixedwith-wrap body)
  188. (car html-and-images)))))
  189. (delete-region html-start html-end)
  190. (save-excursion
  191. (goto-char html-start)
  192. (insert (org-mime-multipart body html)
  193. (mapconcat 'identity html-images "\n")))))
  194. (defun org-mime-apply-html-hook (html)
  195. (if org-mime-html-hook
  196. (with-temp-buffer
  197. (insert html)
  198. (goto-char (point-min))
  199. (run-hooks 'org-mime-html-hook)
  200. (buffer-string))
  201. html))
  202. (defmacro org-mime-try (&rest body)
  203. `(condition-case nil ,@body (error nil)))
  204. (defun org-mime-send-subtree (&optional fmt)
  205. (save-restriction
  206. (org-narrow-to-subtree)
  207. (run-hooks 'org-mime-send-subtree-hook)
  208. (let* ((file (buffer-file-name (current-buffer)))
  209. (subject (nth 4 (org-heading-components)))
  210. (to (org-entry-get nil "MAIL_TO"))
  211. (cc (org-entry-get nil "MAIL_CC"))
  212. (bcc (org-entry-get nil "MAIL_BCC"))
  213. (body (buffer-substring
  214. (save-excursion (goto-char (point-min))
  215. (forward-line 1)
  216. (when (looking-at "[ \t]*:PROPERTIES:")
  217. (re-search-forward ":END:" nil)
  218. (forward-char))
  219. (point))
  220. (point-max))))
  221. (org-mime-compose body (or fmt 'org) file to subject
  222. `((cc . ,cc) (bcc . ,bcc))))))
  223. (defun org-mime-send-buffer (&optional fmt)
  224. (run-hooks 'org-mime-send-buffer-hook)
  225. (let* ((region-p (org-region-active-p))
  226. (subject (org-export-grab-title-from-buffer))
  227. (file (buffer-file-name (current-buffer)))
  228. (body-start (or (and region-p (region-beginning))
  229. (save-excursion (goto-char (point-min)))))
  230. (body-end (or (and region-p (region-end)) (point-max)))
  231. (temp-body-file (make-temp-file "org-mime-export"))
  232. (body (buffer-substring body-start body-end)))
  233. (org-mime-compose body (or fmt 'org) file nil subject)))
  234. (defun org-mime-compose (body fmt file &optional to subject headers)
  235. (require 'message)
  236. (message-mail to subject headers nil)
  237. (message-goto-body)
  238. (flet ((bhook (body fmt)
  239. (let ((hook (intern (concat "org-mime-pre-"
  240. (symbol-name fmt)
  241. "-hook"))))
  242. (if (> (eval `(length ,hook)) 0)
  243. (with-temp-buffer
  244. (insert body)
  245. (goto-char (point-min))
  246. (eval `(run-hooks ',hook))
  247. (buffer-string))
  248. body))))
  249. (let ((fmt (if (symbolp fmt) fmt (intern fmt))))
  250. (cond
  251. ((eq fmt 'org)
  252. (insert (org-export-string (org-babel-trim (bhook body 'org)) 'org)))
  253. ((eq fmt 'ascii)
  254. (insert (org-export-string
  255. (concat "#+Title:\n" (bhook body 'ascii)) 'ascii)))
  256. ((or (eq fmt 'html) (eq fmt 'html-ascii))
  257. (let* ((org-link-file-path-type 'absolute)
  258. ;; we probably don't want to export a huge style file
  259. (org-export-htmlize-output-type 'inline-css)
  260. (html-and-images (org-mime-replace-images
  261. (org-export-string
  262. (bhook body 'html)
  263. 'html (file-name-nondirectory file))
  264. file))
  265. (images (cdr html-and-images))
  266. (html (org-mime-apply-html-hook (car html-and-images))))
  267. (insert (org-mime-multipart
  268. (org-export-string
  269. (org-babel-trim
  270. (bhook body (if (eq fmt 'html) 'org 'ascii)))
  271. (if (eq fmt 'html) 'org 'ascii))
  272. html)
  273. (mapconcat 'identity images "\n"))))))))
  274. (defun org-mime-org-buffer-htmlize ()
  275. "Create an email buffer containing the current org-mode file
  276. exported to html and encoded in both html and in org formats as
  277. mime alternatives."
  278. (interactive)
  279. (org-mime-send-buffer 'html))
  280. (defun org-mime-subtree ()
  281. "Create an email buffer containing the current org-mode subtree
  282. exported to a org format or to the format specified by the
  283. MAIL_FMT property of the subtree."
  284. (interactive)
  285. (org-mime-send-subtree (or (org-entry-get nil "MAIL_FMT") 'org)))
  286. (provide 'org-mime)