org-mime.el 12 KB

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