org-mime.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-use-property-inheritance nil
  51. "Non-nil means al MAIL_ properties apply also for sublevels."
  52. :group 'org-mime
  53. :type 'boolean)
  54. (defcustom org-mime-default-header
  55. "#+OPTIONS: latex:t\n"
  56. "Default header to control html export options, and ensure
  57. first line isn't assumed to be a title line."
  58. :group 'org-mime
  59. :type 'string)
  60. (defcustom org-mime-library 'mml
  61. "Library to use for marking up MIME elements."
  62. :group 'org-mime
  63. :type '(choice 'mml 'semi 'vm))
  64. (defcustom org-mime-preserve-breaks t
  65. "Used as temporary value of `org-export-preserve-breaks' during
  66. mime encoding."
  67. :group 'org-mime
  68. :type 'boolean)
  69. (defcustom org-mime-fixedwith-wrap
  70. "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
  71. "Format string used to wrap a fixedwidth HTML email."
  72. :group 'org-mime
  73. :type 'string)
  74. (defcustom org-mime-html-hook nil
  75. "Hook to run over the html buffer before attachment to email.
  76. This could be used for example to post-process html elements."
  77. :group 'org-mime
  78. :type 'hook)
  79. (mapc (lambda (fmt)
  80. (eval `(defcustom
  81. ,(intern (concat "org-mime-pre-" fmt "-hook"))
  82. nil
  83. (concat "Hook to run before " fmt " export.\nFunctions "
  84. "should take no arguments and will be run in a "
  85. "buffer holding\nthe text to be exported."))))
  86. '("ascii" "org" "html"))
  87. (defcustom org-mime-send-subtree-hook nil
  88. "Hook to run in the subtree in the Org-mode file before export.")
  89. (defcustom org-mime-send-buffer-hook nil
  90. "Hook to run in the Org-mode file before export.")
  91. ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
  92. (defun org-mime-change-element-style (element style)
  93. "Set new default htlm style for <ELEMENT> elements in exported html."
  94. (while (re-search-forward (format "<%s" element) nil t)
  95. (replace-match (format "<%s style=\"%s\"" element style))))
  96. (defun org-mime-change-class-style (class style)
  97. "Set new default htlm style for objects with classs=CLASS in
  98. exported html."
  99. (while (re-search-forward (format "class=\"%s\"" class) nil t)
  100. (replace-match (format "class=\"%s\" style=\"%s\"" class style))))
  101. ;; ;; example addition to `org-mime-html-hook' adding a dark background
  102. ;; ;; color to <pre> elements
  103. ;; (add-hook 'org-mime-html-hook
  104. ;; (lambda ()
  105. ;; (org-mime-change-element-style
  106. ;; "pre" (format "color: %s; background-color: %s;"
  107. ;; "#E6E1DC" "#232323"))
  108. ;; (org-mime-change-class-style
  109. ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
  110. (defun org-mime-file (ext path id)
  111. "Markup a file for attachment."
  112. (case org-mime-library
  113. ('mml (format
  114. "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
  115. ext path id))
  116. ('semi (concat
  117. (format
  118. "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
  119. ext id)
  120. (base64-encode-string
  121. (with-temp-buffer
  122. (set-buffer-multibyte nil)
  123. (binary-insert-encoded-file path)
  124. (buffer-string)))))
  125. ('vm "?")))
  126. (defun org-mime-multipart (plain html)
  127. "Markup a multipart/alternative with text/plain and text/html
  128. alternatives."
  129. (case org-mime-library
  130. ('mml (format (concat "<#multipart type=alternative><#part type=text/plain>"
  131. "%s<#part type=text/html>%s<#/multipart>\n")
  132. plain html))
  133. ('semi (concat
  134. "--" "<<alternative>>-{\n"
  135. "--" "[[text/plain]]\n" plain
  136. "--" "[[text/html]]\n" html
  137. "--" "}-<<alternative>>\n"))
  138. ('vm "?")))
  139. (defun org-mime-replace-images (str current-file)
  140. "Replace images in html files with cid links."
  141. (let (html-images)
  142. (cons
  143. (replace-regexp-in-string ;; replace images in html
  144. "src=\"\\([^\"]+\\)\""
  145. (lambda (text)
  146. (format
  147. "src=\"cid:%s\""
  148. (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
  149. (match-string 1 text)))
  150. (path (expand-file-name
  151. url (file-name-directory current-file)))
  152. (ext (file-name-extension path))
  153. (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
  154. (add-to-list 'html-images
  155. (org-mime-file (concat "image/" ext) path id))
  156. id)))
  157. str)
  158. html-images)))
  159. (defun org-mime-htmlize (arg)
  160. "Export a portion of an email body composed using `mml-mode' to
  161. html using `org-mode'. If called with an active region only
  162. export that region, otherwise export the entire body."
  163. (interactive "P")
  164. (let* ((region-p (org-region-active-p))
  165. (html-start (or (and region-p (region-beginning))
  166. (save-excursion
  167. (goto-char (point-min))
  168. (search-forward mail-header-separator)
  169. (+ (point) 1))))
  170. (html-end (or (and region-p (region-end))
  171. ;; TODO: should catch signature...
  172. (point-max)))
  173. (raw-body (buffer-substring html-start html-end))
  174. (tmp-file (make-temp-name (expand-file-name
  175. "mail" temporary-file-directory)))
  176. (body (org-export-string raw-body 'org (file-name-directory tmp-file)))
  177. ;; because we probably don't want to skip part of our mail
  178. (org-export-skip-text-before-1st-heading nil)
  179. ;; because we probably don't want to export a huge style file
  180. (org-export-htmlize-output-type 'inline-css)
  181. ;; makes the replies with ">"s look nicer
  182. (org-export-preserve-breaks org-mime-preserve-breaks)
  183. ;; to hold attachments for inline html images
  184. (html-and-images
  185. (org-mime-replace-images
  186. (org-export-string raw-body 'html (file-name-directory tmp-file))
  187. tmp-file))
  188. (html-images (unless arg (cdr html-and-images)))
  189. (html (org-mime-apply-html-hook
  190. (if arg
  191. (format org-mime-fixedwith-wrap body)
  192. (car html-and-images)))))
  193. (delete-region html-start html-end)
  194. (save-excursion
  195. (goto-char html-start)
  196. (insert (org-mime-multipart body html)
  197. (mapconcat 'identity html-images "\n")))))
  198. (defun org-mime-apply-html-hook (html)
  199. (if org-mime-html-hook
  200. (with-temp-buffer
  201. (insert html)
  202. (goto-char (point-min))
  203. (run-hooks 'org-mime-html-hook)
  204. (buffer-string))
  205. html))
  206. (defmacro org-mime-try (&rest body)
  207. `(condition-case nil ,@body (error nil)))
  208. (defun org-mime-send-subtree (&optional fmt)
  209. (save-restriction
  210. (org-narrow-to-subtree)
  211. (run-hooks 'org-mime-send-subtree-hook)
  212. (let* ((file (buffer-file-name (current-buffer)))
  213. (subject (nth 4 (org-heading-components)))
  214. (to (org-entry-get nil "MAIL_TO" org-mime-use-property-inheritance))
  215. (cc (org-entry-get nil "MAIL_CC" org-mime-use-property-inheritance))
  216. (bcc (org-entry-get nil "MAIL_BCC" org-mime-use-property-inheritance))
  217. (body (buffer-substring
  218. (save-excursion (goto-char (point-min))
  219. (forward-line 1)
  220. (when (looking-at "[ \t]*:PROPERTIES:")
  221. (re-search-forward ":END:" nil)
  222. (forward-char))
  223. (point))
  224. (point-max))))
  225. (org-mime-compose body (or fmt 'org) file to subject
  226. `((cc . ,cc) (bcc . ,bcc))))))
  227. (defun org-mime-send-buffer (&optional fmt)
  228. (run-hooks 'org-mime-send-buffer-hook)
  229. (let* ((region-p (org-region-active-p))
  230. (subject (org-export-grab-title-from-buffer))
  231. (file (buffer-file-name (current-buffer)))
  232. (body-start (or (and region-p (region-beginning))
  233. (save-excursion (goto-char (point-min)))))
  234. (body-end (or (and region-p (region-end)) (point-max)))
  235. (temp-body-file (make-temp-file "org-mime-export"))
  236. (body (buffer-substring body-start body-end)))
  237. (org-mime-compose body (or fmt 'org) file nil subject)))
  238. (defun org-mime-compose (body fmt file &optional to subject headers)
  239. (require 'message)
  240. (message-mail to subject headers nil)
  241. (message-goto-body)
  242. (flet ((bhook (body fmt)
  243. (let ((hook (intern (concat "org-mime-pre-"
  244. (symbol-name fmt)
  245. "-hook"))))
  246. (if (> (eval `(length ,hook)) 0)
  247. (with-temp-buffer
  248. (insert body)
  249. (goto-char (point-min))
  250. (eval `(run-hooks ',hook))
  251. (buffer-string))
  252. body))))
  253. (let ((fmt (if (symbolp fmt) fmt (intern fmt))))
  254. (cond
  255. ((eq fmt 'org)
  256. (insert (org-export-string (org-babel-trim (bhook body 'org)) 'org)))
  257. ((eq fmt 'ascii)
  258. (insert (org-export-string
  259. (concat "#+Title:\n" (bhook body 'ascii)) 'ascii)))
  260. ((or (eq fmt 'html) (eq fmt 'html-ascii))
  261. (let* ((org-link-file-path-type 'absolute)
  262. ;; we probably don't want to export a huge style file
  263. (org-export-htmlize-output-type 'inline-css)
  264. (html-and-images (org-mime-replace-images
  265. (org-export-string
  266. (bhook body 'html)
  267. 'html (file-name-nondirectory file))
  268. file))
  269. (images (cdr html-and-images))
  270. (html (org-mime-apply-html-hook (car html-and-images))))
  271. (insert (org-mime-multipart
  272. (org-export-string
  273. (org-babel-trim
  274. (bhook body (if (eq fmt 'html) 'org 'ascii)))
  275. (if (eq fmt 'html) 'org 'ascii))
  276. html)
  277. (mapconcat 'identity images "\n"))))))))
  278. (defun org-mime-org-buffer-htmlize ()
  279. "Create an email buffer containing the current org-mode file
  280. exported to html and encoded in both html and in org formats as
  281. mime alternatives."
  282. (interactive)
  283. (org-mime-send-buffer 'html))
  284. (defun org-mime-subtree ()
  285. "Create an email buffer containing the current org-mode subtree
  286. exported to a org format or to the format specified by the
  287. MAIL_FMT property of the subtree."
  288. (interactive)
  289. (org-mime-send-subtree
  290. (or (org-entry-get nil "MAIL_FMT" org-use-mime-property-inheritance) 'org)))
  291. (provide 'org-mime)