org-mime.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. 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. (when images (concat "--" "<<alternative>>-{\n"))
  144. "--" "[[text/html]]\n" html
  145. images
  146. (when images (concat "--" "}-<<alternative>>\n"))
  147. "--" "}-<<alternative>>\n"))
  148. ('vm "?")))
  149. (defun org-mime-replace-images (str current-file)
  150. "Replace images in html files with cid links."
  151. (let (html-images)
  152. (cons
  153. (replace-regexp-in-string ;; replace images in html
  154. "src=\"\\([^\"]+\\)\""
  155. (lambda (text)
  156. (format
  157. "src=\"cid:%s\""
  158. (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
  159. (match-string 1 text)))
  160. (path (expand-file-name
  161. url (file-name-directory current-file)))
  162. (ext (file-name-extension path))
  163. (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
  164. (add-to-list 'html-images
  165. (org-mime-file (concat "image/" ext) path id))
  166. id)))
  167. str)
  168. html-images)))
  169. (defun org-mime-htmlize (arg)
  170. "Export a portion of an email body composed using `mml-mode' to
  171. html using `org-mode'. If called with an active region only
  172. export that region, otherwise export the entire body."
  173. (interactive "P")
  174. (require 'ox-org)
  175. (require 'ox-html)
  176. (let* ((region-p (org-region-active-p))
  177. (html-start (or (and region-p (region-beginning))
  178. (save-excursion
  179. (goto-char (point-min))
  180. (search-forward mail-header-separator)
  181. (+ (point) 1))))
  182. (html-end (or (and region-p (region-end))
  183. ;; TODO: should catch signature...
  184. (point-max)))
  185. (raw-body (concat org-mime-default-header
  186. (buffer-substring html-start html-end)))
  187. (tmp-file (make-temp-name (expand-file-name
  188. "mail" temporary-file-directory)))
  189. (body (org-export-string-as raw-body 'org t))
  190. ;; because we probably don't want to export a huge style file
  191. (org-export-htmlize-output-type 'inline-css)
  192. ;; makes the replies with ">"s look nicer
  193. (org-export-preserve-breaks org-mime-preserve-breaks)
  194. ;; dvipng for inline latex because MathJax doesn't work in mail
  195. (org-html-with-latex 'dvipng)
  196. ;; to hold attachments for inline html images
  197. (html-and-images
  198. (org-mime-replace-images
  199. (org-export-string-as raw-body 'html t) tmp-file))
  200. (html-images (unless arg (cdr html-and-images)))
  201. (html (org-mime-apply-html-hook
  202. (if arg
  203. (format org-mime-fixedwith-wrap body)
  204. (car html-and-images)))))
  205. (delete-region html-start html-end)
  206. (save-excursion
  207. (goto-char html-start)
  208. (insert (org-mime-multipart
  209. body html (mapconcat 'identity html-images "\n"))))))
  210. (defun org-mime-apply-html-hook (html)
  211. (if org-mime-html-hook
  212. (with-temp-buffer
  213. (insert html)
  214. (goto-char (point-min))
  215. (run-hooks 'org-mime-html-hook)
  216. (buffer-string))
  217. html))
  218. (defmacro org-mime-try (&rest body)
  219. `(condition-case nil ,@body (error nil)))
  220. (defun org-mime-send-subtree (&optional fmt)
  221. (save-restriction
  222. (org-narrow-to-subtree)
  223. (run-hooks 'org-mime-send-subtree-hook)
  224. (flet ((mp (p) (org-entry-get nil p org-mime-use-property-inheritance)))
  225. (let* ((file (buffer-file-name (current-buffer)))
  226. (subject (or (mp "MAIL_SUBJECT") (nth 4 (org-heading-components))))
  227. (to (mp "MAIL_TO"))
  228. (cc (mp "MAIL_CC"))
  229. (bcc (mp "MAIL_BCC"))
  230. (body (buffer-substring
  231. (save-excursion (goto-char (point-min))
  232. (forward-line 1)
  233. (when (looking-at "[ \t]*:PROPERTIES:")
  234. (re-search-forward ":END:" nil)
  235. (forward-char))
  236. (point))
  237. (point-max))))
  238. (org-mime-compose body (or fmt 'org) file to subject
  239. `((cc . ,cc) (bcc . ,bcc)))))))
  240. (defun org-mime-send-buffer (&optional fmt)
  241. (run-hooks 'org-mime-send-buffer-hook)
  242. (let* ((region-p (org-region-active-p))
  243. (subject (org-export-grab-title-from-buffer))
  244. (file (buffer-file-name (current-buffer)))
  245. (body-start (or (and region-p (region-beginning))
  246. (save-excursion (goto-char (point-min)))))
  247. (body-end (or (and region-p (region-end)) (point-max)))
  248. (temp-body-file (make-temp-file "org-mime-export"))
  249. (body (buffer-substring body-start body-end)))
  250. (org-mime-compose body (or fmt 'org) file nil subject)))
  251. (defun org-mime-compose (body fmt file &optional to subject headers)
  252. (require 'message)
  253. (message-mail to subject headers nil)
  254. (message-goto-body)
  255. (flet ((bhook (body fmt)
  256. (let ((hook (intern (concat "org-mime-pre-"
  257. (symbol-name fmt)
  258. "-hook"))))
  259. (if (> (eval `(length ,hook)) 0)
  260. (with-temp-buffer
  261. (insert body)
  262. (goto-char (point-min))
  263. (eval `(run-hooks ',hook))
  264. (buffer-string))
  265. body))))
  266. (let ((fmt (if (symbolp fmt) fmt (intern fmt))))
  267. (cond
  268. ((eq fmt 'org)
  269. (require 'ox-org)
  270. (insert (org-export-string-as
  271. (org-babel-trim (bhook body 'org)) 'org t)))
  272. ((eq fmt 'ascii)
  273. (require 'ox-ascii)
  274. (insert (org-export-string-as
  275. (concat "#+Title:\n" (bhook body 'ascii)) 'ascii t)))
  276. ((or (eq fmt 'html) (eq fmt 'html-ascii))
  277. (require 'ox-ascii)
  278. (require 'ox-org)
  279. (let* ((org-link-file-path-type 'absolute)
  280. ;; we probably don't want to export a huge style file
  281. (org-export-htmlize-output-type 'inline-css)
  282. (html-and-images
  283. (org-mime-replace-images
  284. (org-export-string-as (bhook body 'html) 'html t) file))
  285. (images (cdr html-and-images))
  286. (html (org-mime-apply-html-hook (car html-and-images))))
  287. (insert (org-mime-multipart
  288. (org-export-string-as
  289. (org-babel-trim
  290. (bhook body (if (eq fmt 'html) 'org 'ascii)))
  291. (if (eq fmt 'html) 'org 'ascii) t)
  292. html)
  293. (mapconcat 'identity images "\n"))))))))
  294. (defun org-mime-org-buffer-htmlize ()
  295. "Create an email buffer containing the current org-mode file
  296. exported to html and encoded in both html and in org formats as
  297. mime alternatives."
  298. (interactive)
  299. (org-mime-send-buffer 'html))
  300. (defun org-mime-subtree ()
  301. "Create an email buffer containing the current org-mode subtree
  302. exported to a org format or to the format specified by the
  303. MAIL_FMT property of the subtree."
  304. (interactive)
  305. (org-mime-send-subtree
  306. (or (org-entry-get nil "MAIL_FMT" org-mime-use-property-inheritance) 'org)))
  307. (provide 'org-mime)