org-mime.el 12 KB

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