org-mime.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
  76. (defun org-mime-change-element-style (element style)
  77. "Set new default htlm style for <ELEMENT> elements in exported html."
  78. (while (re-search-forward (format "<%s" element) nil t)
  79. (replace-match (format "<%s style=\"%s\"" element style))))
  80. (defun org-mime-change-class-style (class style)
  81. "Set new default htlm style for objects with classs=CLASS in
  82. exported html."
  83. (while (re-search-forward (format "class=\"%s\"" class) nil t)
  84. (replace-match (format "class=\"%s\" style=\"%s\"" class style))))
  85. ;; ;; example addition to `org-mime-html-hook' adding a dark background
  86. ;; ;; color to <pre> elements
  87. ;; (add-hook 'org-mime-html-hook
  88. ;; (lambda ()
  89. ;; (org-mime-change-element-style
  90. ;; "pre" (format "color: %s; background-color: %s;"
  91. ;; "#E6E1DC" "#232323"))
  92. ;; (org-mime-change-class-style
  93. ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
  94. (defun org-mime-file (ext path id)
  95. "Markup a file for attachment."
  96. (case org-mime-library
  97. ('mml (format
  98. "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
  99. ext path id))
  100. ('semi (concat
  101. (format
  102. "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
  103. ext id)
  104. (base64-encode-string
  105. (with-temp-buffer
  106. (set-buffer-multibyte nil)
  107. (binary-insert-encoded-file path)
  108. (buffer-string)))))
  109. ('vm "?")))
  110. (defun org-mime-multipart (plain html)
  111. "Markup a multipart/alternative with text/plain and text/html
  112. alternatives."
  113. (case org-mime-library
  114. ('mml (format (concat "<#multipart type=alternative><#part type=text/plain>"
  115. "%s<#part type=text/html>%s<#/multipart>\n")
  116. plain html))
  117. ('semi (concat
  118. "--" "<<alternative>>-{\n"
  119. "--" "[[text/plain]]\n" plain
  120. "--" "[[text/html]]\n" html
  121. "--" "}-<<alternative>>\n"))
  122. ('vm "?")))
  123. (defun org-mime-replace-images (str current-file)
  124. "Replace images in html files with cid links."
  125. (let (html-images)
  126. (cons
  127. (replace-regexp-in-string ;; replace images in html
  128. "src=\"\\([^\"]+\\)\""
  129. (lambda (text)
  130. (format
  131. "src=\"cid:%s\""
  132. (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
  133. (match-string 1 text)))
  134. (path (expand-file-name
  135. url (file-name-directory current-file)))
  136. (ext (file-name-extension path))
  137. (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
  138. (add-to-list 'html-images
  139. (org-mime-file (concat "image/" ext) path id))
  140. id)))
  141. str)
  142. html-images)))
  143. (defun org-mime-htmlize (arg)
  144. "Export a portion of an email body composed using `mml-mode' to
  145. html using `org-mode'. If called with an active region only
  146. export that region, otherwise export the entire body."
  147. (interactive "P")
  148. (let* ((region-p (org-region-active-p))
  149. (html-start (or (and region-p (region-beginning))
  150. (save-excursion
  151. (goto-char (point-min))
  152. (search-forward mail-header-separator)
  153. (+ (point) 1))))
  154. (html-end (or (and region-p (region-end))
  155. ;; TODO: should catch signature...
  156. (point-max)))
  157. (raw-body (buffer-substring html-start html-end))
  158. (tmp-file (make-temp-name (expand-file-name "mail" temporary-file-directory)))
  159. (body (org-export-string raw-body "org" (file-name-directory tmp-file)))
  160. ;; because we probably don't want to skip part of our mail
  161. (org-export-skip-text-before-1st-heading nil)
  162. ;; because we probably don't want to export a huge style file
  163. (org-export-htmlize-output-type 'inline-css)
  164. ;; makes the replies with ">"s look nicer
  165. (org-export-preserve-breaks org-mime-preserve-breaks)
  166. ;; to hold attachments for inline html images
  167. (html-and-images
  168. (org-mime-replace-images
  169. (org-export-string raw-body "html" (file-name-directory tmp-file))
  170. tmp-file))
  171. (html-images (unless arg (cdr html-and-images)))
  172. (html (org-mime-apply-html-hook
  173. (if arg
  174. (format org-mime-fixedwith-wrap body)
  175. (car html-and-images)))))
  176. (delete-region html-start html-end)
  177. (save-excursion
  178. (goto-char html-start)
  179. (insert (org-mime-multipart body html)
  180. (mapconcat 'identity html-images "\n")))))
  181. (defun org-mime-apply-html-hook (html)
  182. (if org-mime-html-hook
  183. (with-temp-buffer
  184. (insert html)
  185. (goto-char (point-min))
  186. (run-hooks 'org-mime-html-hook)
  187. (buffer-string))
  188. html))
  189. (defun org-mime-org-buffer-htmlize ()
  190. "Export the current org-mode buffer to HTML using
  191. `org-export-as-html' and package the results into an email
  192. handling with appropriate MIME encoding."
  193. (interactive)
  194. (require 'reporter)
  195. (let* ((region-p (org-region-active-p))
  196. (current-file (buffer-file-name (current-buffer)))
  197. (html-start (or (and region-p (region-beginning))
  198. (save-excursion
  199. (goto-char (point-min)))))
  200. (html-end (or (and region-p (region-end))
  201. (point-max)))
  202. (temp-body-file (make-temp-file "org-mime-export"))
  203. (raw-body (buffer-substring html-start html-end))
  204. (body (with-temp-buffer
  205. (insert raw-body)
  206. (write-file temp-body-file)
  207. (org-export-as-org nil nil nil 'string t)))
  208. (org-link-file-path-type 'absolute)
  209. ;; because we probably don't want to export a huge style file
  210. (org-export-htmlize-output-type 'inline-css)
  211. ;; to hold attachments for inline html images
  212. (html-and-images (org-mime-replace-images
  213. (org-export-as-html nil nil nil 'string t)
  214. current-file))
  215. (html-images (cdr html-and-images))
  216. (html (org-mime-apply-html-hook (car html-and-images))))
  217. ;; dump the exported html into a fresh message buffer
  218. (reporter-compose-outgoing)
  219. (goto-char (point-max))
  220. (prog1 (insert (org-mime-multipart body html)
  221. (mapconcat 'identity html-images "\n"))
  222. (delete-file temp-body-file))))
  223. (provide 'org-mime)