org-mime.el 12 KB

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