org-mime.el 12 KB

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