1 ;;; org-mime.el --- org html export for text/html MIME emails
3 ;; Copyright (C) 2010 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: mime, mail, email, html
7 ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; WYSWYG, html mime composition using org-mode
31 ;; For mail composed using the orgstruct-mode minor mode, this
32 ;; provides a function for converting all or part of your mail buffer
33 ;; to embedded html as exported by org-mode. Call `org-mime-htmlize'
34 ;; in a message buffer to convert either the active region or the
35 ;; entire buffer to html.
37 ;; Similarly the `org-mime-org-buffer-htmlize' function can be called
38 ;; from within an org-mode buffer to convert the buffer to html, and
39 ;; package the results into an email handling with appropriate MIME
42 ;; you might want to bind this to a key with something like the
43 ;; following message-mode binding
45 ;; (add-hook 'message-mode-hook
47 ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
49 ;; and the following org-mode binding
51 ;; (add-hook 'org-mode-hook
53 ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
58 (defcustom org-mime-default-header
59 "#+OPTIONS: latex:t\n"
60 "Default header to control html export options, and ensure
61 first line isn't assumed to be a title line."
65 (defcustom org-mime-library
'mml
66 "Library to use for marking up MIME elements."
68 :type
'(choice 'mml
'semi
'vm
))
70 (defcustom org-mime-preserve-breaks t
71 "Used as temporary value of `org-export-preserve-breaks' during
76 (defcustom org-mime-fixedwith-wrap
77 "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
78 "Format string used to wrap a fixedwidth HTML email."
82 (defcustom org-mime-html-hook nil
83 "Hook to run over the html buffer before attachment to email.
84 This could be used for example to post-process html elements."
88 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
89 (defun org-mime-change-element-style (element style
)
90 "Set new default htlm style for <ELEMENT> elements in exported html."
91 (while (re-search-forward (format "<%s" element
) nil t
)
92 (replace-match (format "<%s style=\"%s\"" element style
))))
94 (defun org-mime-change-class-style (class style
)
95 "Set new default htlm style for objects with classs=CLASS in
97 (while (re-search-forward (format "class=\"%s\"" class
) nil t
)
98 (replace-match (format "class=\"%s\" style=\"%s\"" class style
))))
100 ;; ;; example addition to `org-mime-html-hook' adding a dark background
101 ;; ;; color to <pre> elements
102 ;; (add-hook 'org-mime-html-hook
104 ;; (org-mime-change-element-style
105 ;; "pre" (format "color: %s; background-color: %s;"
106 ;; "#E6E1DC" "#232323"))
107 ;; (org-mime-change-class-style
108 ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
110 (defun org-mime-file (ext path id
)
111 "Markup a file for attachment."
112 (case org-mime-library
114 "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
118 "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
120 (base64-encode-string
122 (set-buffer-multibyte nil
)
123 (binary-insert-encoded-file path
)
127 (defun org-mime-multipart (plain html
)
128 "Markup a multipart/alternative with text/plain and text/html
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")
135 "--" "<<alternative>>-{\n"
136 "--" "[[text/plain]]\n" plain
137 "--" "[[text/html]]\n" html
138 "--" "}-<<alternative>>\n"))
141 (defun org-mime-replace-images (str current-file
)
142 "Replace images in html files with cid links."
145 (replace-regexp-in-string ;; replace images in html
146 "src=\"\\([^\"]+\\)\""
150 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text
)
151 (match-string 1 text
)))
152 (path (expand-file-name
153 url
(file-name-directory current-file
)))
154 (ext (file-name-extension path
))
155 (id (replace-regexp-in-string "[\/\\\\]" "_" path
)))
156 (add-to-list 'html-images
157 (org-mime-file (concat "image/" ext
) path id
))
162 (defun org-mime-htmlize (arg)
163 "Export a portion of an email body composed using `mml-mode' to
164 html using `org-mode'. If called with an active region only
165 export that region, otherwise export the entire body."
167 (let* ((region-p (org-region-active-p))
168 (html-start (or (and region-p
(region-beginning))
170 (goto-char (point-min))
171 (search-forward mail-header-separator
)
173 (html-end (or (and region-p
(region-end))
174 ;; TODO: should catch signature...
176 (raw-body (buffer-substring html-start html-end
))
177 (tmp-file (make-temp-name (expand-file-name "mail" temporary-file-directory
)))
178 (body (org-mime-org-export "org" raw-body tmp-file
))
179 ;; because we probably don't want to skip part of our mail
180 (org-export-skip-text-before-1st-heading nil
)
181 ;; because we probably don't want to export a huge style file
182 (org-export-htmlize-output-type 'inline-css
)
183 ;; makes the replies with ">"s look nicer
184 (org-export-preserve-breaks org-mime-preserve-breaks
)
185 ;; to hold attachments for inline html images
187 (org-mime-replace-images
188 (org-mime-org-export "html" raw-body tmp-file
)
190 (html-images (unless arg
(cdr html-and-images
)))
191 (html (org-mime-apply-html-hook
193 (format org-mime-fixedwith-wrap body
)
194 (car html-and-images
)))))
195 (delete-region html-start html-end
)
197 (goto-char html-start
)
198 (insert (org-mime-multipart body html
)
199 (mapconcat 'identity html-images
"\n")))))
201 (defun org-mime-org-export (fmt body tmp-file
)
202 "Org-Export BODY to format FMT with the file name set to
203 TMP-FILE during export."
206 (insert org-mime-default-header
)
208 (write-file tmp-file
)
209 (org-load-modules-maybe)
210 (unless org-local-vars
211 (setq org-local-vars
(org-get-local-variables)))
213 (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
214 (list 'let org-local-vars
215 (list (intern (concat "org-export-as-" fmt
))
216 nil nil nil
''string t
)))
217 (if (string= fmt
"org") (length org-mime-default-header
) 0)))))
219 (defun org-mime-apply-html-hook (html)
220 (if org-mime-html-hook
223 (goto-char (point-min))
224 (run-hooks 'org-mime-html-hook
)
228 (defun org-mime-org-buffer-htmlize ()
229 "Export the current org-mode buffer to HTML using
230 `org-export-as-html' and package the results into an email
231 handling with appropriate MIME encoding."
234 (let* ((region-p (org-region-active-p))
235 (current-file (buffer-file-name (current-buffer)))
236 (html-start (or (and region-p
(region-beginning))
238 (goto-char (point-min)))))
239 (html-end (or (and region-p
(region-end))
241 (temp-body-file (make-temp-file "org-mime-export"))
242 (raw-body (buffer-substring html-start html-end
))
243 (body (with-temp-buffer
245 (write-file temp-body-file
)
246 (org-export-as-org nil nil nil
'string t
)))
247 (org-link-file-path-type 'absolute
)
248 ;; because we probably don't want to export a huge style file
249 (org-export-htmlize-output-type 'inline-css
)
250 ;; to hold attachments for inline html images
251 (html-and-images (org-mime-replace-images
252 (org-export-as-html nil nil nil
'string t
)
254 (html-images (cdr html-and-images
))
255 (html (org-mime-apply-html-hook (car html-and-images
))))
256 ;; dump the exported html into a fresh message buffer
257 (reporter-compose-outgoing)
258 (goto-char (point-max))
259 (prog1 (insert (org-mime-multipart body html
)
260 (mapconcat 'identity html-images
"\n"))
261 (delete-file temp-body-file
))))