org-mime.el 11 KB

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