1 ;;; org-mime.el --- org html export for text/html MIME emails
3 ;; Copyright (C) 2010-2013 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: mime, mail, email, html
7 ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php
10 ;; This file is not part of GNU Emacs.
14 ;; This program is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; WYSWYG, html mime composition using org-mode
33 ;; For mail composed using the orgstruct-mode minor mode, this
34 ;; provides a function for converting all or part of your mail buffer
35 ;; to embedded html as exported by org-mode. Call `org-mime-htmlize'
36 ;; in a message buffer to convert either the active region or the
37 ;; entire buffer to html.
39 ;; Similarly the `org-mime-org-buffer-htmlize' function can be called
40 ;; from within an org-mode buffer to convert the buffer to html, and
41 ;; package the results into an email handling with appropriate MIME
44 ;; you might want to bind this to a key with something like the
45 ;; following message-mode binding
47 ;; (add-hook 'message-mode-hook
49 ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
51 ;; and the following org-mode binding
53 ;; (add-hook 'org-mode-hook
55 ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
60 (defcustom org-mime-use-property-inheritance nil
61 "Non-nil means al MAIL_ properties apply also for sublevels."
65 (defcustom org-mime-default-header
66 "#+OPTIONS: latex:t\n"
67 "Default header to control html export options, and ensure
68 first line isn't assumed to be a title line."
72 (defcustom org-mime-library
'mml
73 "Library to use for marking up MIME elements."
75 :type
'(choice 'mml
'semi
'vm
))
77 (defcustom org-mime-preserve-breaks t
78 "Used as temporary value of `org-export-preserve-breaks' during
83 (defcustom org-mime-fixedwith-wrap
84 "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
85 "Format string used to wrap a fixedwidth HTML email."
89 (defcustom org-mime-html-hook nil
90 "Hook to run over the html buffer before attachment to email.
91 This could be used for example to post-process html elements."
97 ,(intern (concat "org-mime-pre-" fmt
"-hook"))
99 (concat "Hook to run before " fmt
" export.\nFunctions "
100 "should take no arguments and will be run in a "
101 "buffer holding\nthe text to be exported."))))
102 '("ascii" "org" "html"))
104 (defcustom org-mime-send-subtree-hook nil
105 "Hook to run in the subtree in the Org-mode file before export.")
107 (defcustom org-mime-send-buffer-hook nil
108 "Hook to run in the Org-mode file before export.")
110 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
111 (defun org-mime-change-element-style (element style
)
112 "Set new default htlm style for <ELEMENT> elements in exported html."
113 (while (re-search-forward (format "<%s" element
) nil t
)
114 (replace-match (format "<%s style=\"%s\"" element style
))))
116 (defun org-mime-change-class-style (class style
)
117 "Set new default htlm style for objects with classs=CLASS in
119 (while (re-search-forward (format "class=\"%s\"" class
) nil t
)
120 (replace-match (format "class=\"%s\" style=\"%s\"" class style
))))
122 ;; ;; example addition to `org-mime-html-hook' adding a dark background
123 ;; ;; color to <pre> elements
124 ;; (add-hook 'org-mime-html-hook
126 ;; (org-mime-change-element-style
127 ;; "pre" (format "color: %s; background-color: %s;"
128 ;; "#E6E1DC" "#232323"))
129 ;; (org-mime-change-class-style
130 ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
132 (defun org-mime-file (ext path id
)
133 "Markup a file for attachment."
134 (case org-mime-library
135 ('mml
(format (concat "<#part type=\"%s\" filename=\"%s\" "
136 "disposition=inline id=\"<%s>\">\n<#/part>\n")
139 (format (concat "--[[%s\nContent-Disposition: "
140 "inline;\nContent-ID: <%s>][base64]]\n")
142 (base64-encode-string
144 (set-buffer-multibyte nil
)
145 (binary-insert-encoded-file path
)
149 (defun org-mime-multipart (plain html
&optional images
)
150 "Markup a multipart/alternative with text/plain and text/html alternatives.
151 If the html portion of the message includes images wrap the html
152 and images in a multipart/related part."
153 (case org-mime-library
154 ('mml
(concat "<#multipart type=alternative><#part type=text/plain>"
156 (when images
"<#multipart type=related>")
157 "<#part type=text/html>"
160 (when images
"<#/multipart>\n")
163 "--" "<<alternative>>-{\n"
164 "--" "[[text/plain]]\n" plain
165 (when images
(concat "--" "<<alternative>>-{\n"))
166 "--" "[[text/html]]\n" html
168 (when images
(concat "--" "}-<<alternative>>\n"))
169 "--" "}-<<alternative>>\n"))
172 (defun org-mime-replace-images (str current-file
)
173 "Replace images in html files with cid links."
176 (replace-regexp-in-string ;; replace images in html
177 "src=\"\\([^\"]+\\)\""
181 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text
)
182 (match-string 1 text
)))
183 (path (expand-file-name
184 url
(file-name-directory current-file
)))
185 (ext (file-name-extension path
))
186 (id (replace-regexp-in-string "[\/\\\\]" "_" path
)))
187 (add-to-list 'html-images
188 (org-mime-file (concat "image/" ext
) path id
))
193 (defun org-mime-htmlize (arg)
194 "Export a portion of an email body composed using `mml-mode' to
195 html using `org-mode'. If called with an active region only
196 export that region, otherwise export the entire body."
198 (let* ((region-p (org-region-active-p))
199 (html-start (or (and region-p
(region-beginning))
201 (goto-char (point-min))
202 (search-forward mail-header-separator
)
204 (html-end (or (and region-p
(region-end))
205 ;; TODO: should catch signature...
207 (raw-body (concat org-mime-default-header
208 (buffer-substring html-start html-end
)))
209 (tmp-file (make-temp-name (expand-file-name
210 "mail" temporary-file-directory
)))
211 (body (org-export-string raw-body
'org
(file-name-directory tmp-file
)))
212 ;; because we probably don't want to skip part of our mail
213 (org-export-skip-text-before-1st-heading nil
)
214 ;; because we probably don't want to export a huge style file
215 (org-export-htmlize-output-type 'inline-css
)
216 ;; makes the replies with ">"s look nicer
217 (org-export-preserve-breaks org-mime-preserve-breaks
)
218 ;; dvipng for inline latex because MathJax doesn't work in mail
219 (org-export-with-LaTeX-fragments 'dvipng
)
220 ;; to hold attachments for inline html images
222 (org-mime-replace-images
223 (org-export-string raw-body
'html
(file-name-directory tmp-file
))
225 (html-images (unless arg
(cdr html-and-images
)))
226 (html (org-mime-apply-html-hook
228 (format org-mime-fixedwith-wrap body
)
229 (car html-and-images
)))))
230 (delete-region html-start html-end
)
232 (goto-char html-start
)
233 (insert (org-mime-multipart
234 body html
(mapconcat 'identity html-images
"\n"))))))
236 (defun org-mime-apply-html-hook (html)
237 (if org-mime-html-hook
240 (goto-char (point-min))
241 (run-hooks 'org-mime-html-hook
)
245 (defmacro org-mime-try
(&rest body
)
246 `(condition-case nil
,@body
(error nil
)))
248 (defun org-mime-send-subtree (&optional fmt
)
250 (org-narrow-to-subtree)
251 (run-hooks 'org-mime-send-subtree-hook
)
252 (flet ((mp (p) (org-entry-get nil p org-mime-use-property-inheritance
)))
253 (let* ((file (buffer-file-name (current-buffer)))
254 (subject (or (mp "MAIL_SUBJECT") (nth 4 (org-heading-components))))
257 (bcc (mp "MAIL_BCC"))
258 (body (buffer-substring
259 (save-excursion (goto-char (point-min))
261 (when (looking-at "[ \t]*:PROPERTIES:")
262 (re-search-forward ":END:" nil
)
266 (org-mime-compose body
(or fmt
'org
) file to subject
267 `((cc .
,cc
) (bcc .
,bcc
)))))))
269 (defun org-mime-send-buffer (&optional fmt
)
270 (run-hooks 'org-mime-send-buffer-hook
)
271 (let* ((region-p (org-region-active-p))
272 (subject (org-export-grab-title-from-buffer))
273 (file (buffer-file-name (current-buffer)))
274 (body-start (or (and region-p
(region-beginning))
275 (save-excursion (goto-char (point-min)))))
276 (body-end (or (and region-p
(region-end)) (point-max)))
277 (temp-body-file (make-temp-file "org-mime-export"))
278 (body (buffer-substring body-start body-end
)))
279 (org-mime-compose body
(or fmt
'org
) file nil subject
)))
281 (defun org-mime-compose (body fmt file
&optional to subject headers
)
283 (message-mail to subject headers nil
)
285 (flet ((bhook (body fmt
)
286 (let ((hook (intern (concat "org-mime-pre-"
289 (if (> (eval `(length ,hook
)) 0)
292 (goto-char (point-min))
293 (eval `(run-hooks ',hook
))
296 (let ((fmt (if (symbolp fmt
) fmt
(intern fmt
))))
299 (insert (org-export-string (org-babel-trim (bhook body
'org
)) 'org
)))
301 (insert (org-export-string
302 (concat "#+Title:\n" (bhook body
'ascii
)) 'ascii
)))
303 ((or (eq fmt
'html
) (eq fmt
'html-ascii
))
304 (let* ((org-link-file-path-type 'absolute
)
305 ;; we probably don't want to export a huge style file
306 (org-export-htmlize-output-type 'inline-css
)
307 (html-and-images (org-mime-replace-images
310 'html
(file-name-nondirectory file
))
312 (images (cdr html-and-images
))
313 (html (org-mime-apply-html-hook (car html-and-images
))))
314 (insert (org-mime-multipart
317 (bhook body
(if (eq fmt
'html
) 'org
'ascii
)))
318 (if (eq fmt
'html
) 'org
'ascii
))
320 (mapconcat 'identity images
"\n"))))))))
322 (defun org-mime-org-buffer-htmlize ()
323 "Create an email buffer containing the current org-mode file
324 exported to html and encoded in both html and in org formats as
327 (org-mime-send-buffer 'html
))
329 (defun org-mime-subtree ()
330 "Create an email buffer containing the current org-mode subtree
331 exported to a org format or to the format specified by the
332 MAIL_FMT property of the subtree."
334 (org-mime-send-subtree
335 (or (org-entry-get nil
"MAIL_FMT" org-mime-use-property-inheritance
) 'org
)))