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-use-property-inheritance nil
59 "Non-nil means al MAIL_ properties apply also for sublevels."
63 (defcustom org-mime-default-header
64 "#+OPTIONS: latex:t\n"
65 "Default header to control html export options, and ensure
66 first line isn't assumed to be a title line."
70 (defcustom org-mime-library
'mml
71 "Library to use for marking up MIME elements."
73 :type
'(choice 'mml
'semi
'vm
))
75 (defcustom org-mime-preserve-breaks t
76 "Used as temporary value of `org-export-preserve-breaks' during
81 (defcustom org-mime-fixedwith-wrap
82 "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
83 "Format string used to wrap a fixedwidth HTML email."
87 (defcustom org-mime-html-hook nil
88 "Hook to run over the html buffer before attachment to email.
89 This could be used for example to post-process html elements."
95 ,(intern (concat "org-mime-pre-" fmt
"-hook"))
97 (concat "Hook to run before " fmt
" export.\nFunctions "
98 "should take no arguments and will be run in a "
99 "buffer holding\nthe text to be exported."))))
100 '("ascii" "org" "html"))
102 (defcustom org-mime-send-subtree-hook nil
103 "Hook to run in the subtree in the Org-mode file before export.")
105 (defcustom org-mime-send-buffer-hook nil
106 "Hook to run in the Org-mode file before export.")
108 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
109 (defun org-mime-change-element-style (element style
)
110 "Set new default htlm style for <ELEMENT> elements in exported html."
111 (while (re-search-forward (format "<%s" element
) nil t
)
112 (replace-match (format "<%s style=\"%s\"" element style
))))
114 (defun org-mime-change-class-style (class style
)
115 "Set new default htlm style for objects with classs=CLASS in
117 (while (re-search-forward (format "class=\"%s\"" class
) nil t
)
118 (replace-match (format "class=\"%s\" style=\"%s\"" class style
))))
120 ;; ;; example addition to `org-mime-html-hook' adding a dark background
121 ;; ;; color to <pre> elements
122 ;; (add-hook 'org-mime-html-hook
124 ;; (org-mime-change-element-style
125 ;; "pre" (format "color: %s; background-color: %s;"
126 ;; "#E6E1DC" "#232323"))
127 ;; (org-mime-change-class-style
128 ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
130 (defun org-mime-file (ext path id
)
131 "Markup a file for attachment."
132 (case org-mime-library
134 "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
138 "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
140 (base64-encode-string
142 (set-buffer-multibyte nil
)
143 (binary-insert-encoded-file path
)
147 (defun org-mime-multipart (plain html
)
148 "Markup a multipart/alternative with text/plain and text/html
150 (case org-mime-library
151 ('mml
(format (concat "<#multipart type=alternative><#part type=text/plain>"
152 "%s<#part type=text/html>%s<#/multipart>\n")
155 "--" "<<alternative>>-{\n"
156 "--" "[[text/plain]]\n" plain
157 "--" "[[text/html]]\n" html
158 "--" "}-<<alternative>>\n"))
161 (defun org-mime-replace-images (str current-file
)
162 "Replace images in html files with cid links."
165 (replace-regexp-in-string ;; replace images in html
166 "src=\"\\([^\"]+\\)\""
170 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text
)
171 (match-string 1 text
)))
172 (path (expand-file-name
173 url
(file-name-directory current-file
)))
174 (ext (file-name-extension path
))
175 (id (replace-regexp-in-string "[\/\\\\]" "_" path
)))
176 (add-to-list 'html-images
177 (org-mime-file (concat "image/" ext
) path id
))
182 (defun org-mime-htmlize (arg)
183 "Export a portion of an email body composed using `mml-mode' to
184 html using `org-mode'. If called with an active region only
185 export that region, otherwise export the entire body."
187 (let* ((region-p (org-region-active-p))
188 (html-start (or (and region-p
(region-beginning))
190 (goto-char (point-min))
191 (search-forward mail-header-separator
)
193 (html-end (or (and region-p
(region-end))
194 ;; TODO: should catch signature...
196 (raw-body (buffer-substring html-start html-end
))
197 (tmp-file (make-temp-name (expand-file-name
198 "mail" temporary-file-directory
)))
199 (body (org-export-string raw-body
'org
(file-name-directory tmp-file
)))
200 ;; because we probably don't want to skip part of our mail
201 (org-export-skip-text-before-1st-heading nil
)
202 ;; because we probably don't want to export a huge style file
203 (org-export-htmlize-output-type 'inline-css
)
204 ;; makes the replies with ">"s look nicer
205 (org-export-preserve-breaks org-mime-preserve-breaks
)
206 ;; to hold attachments for inline html images
208 (org-mime-replace-images
209 (org-export-string raw-body
'html
(file-name-directory tmp-file
))
211 (html-images (unless arg
(cdr html-and-images
)))
212 (html (org-mime-apply-html-hook
214 (format org-mime-fixedwith-wrap body
)
215 (car html-and-images
)))))
216 (delete-region html-start html-end
)
218 (goto-char html-start
)
219 (insert (org-mime-multipart body html
)
220 (mapconcat 'identity html-images
"\n")))))
222 (defun org-mime-apply-html-hook (html)
223 (if org-mime-html-hook
226 (goto-char (point-min))
227 (run-hooks 'org-mime-html-hook
)
231 (defmacro org-mime-try
(&rest body
)
232 `(condition-case nil
,@body
(error nil
)))
234 (defun org-mime-send-subtree (&optional fmt
)
236 (org-narrow-to-subtree)
237 (run-hooks 'org-mime-send-subtree-hook
)
238 (flet ((mp (p) (org-entry-get nil p org-mime-use-property-inheritance
)))
239 (let* ((file (buffer-file-name (current-buffer)))
240 (subject (or (mp "MAIL_SUBJECT") (nth 4 (org-heading-components))))
243 (bcc (mp "MAIL_BCC"))
244 (body (buffer-substring
245 (save-excursion (goto-char (point-min))
247 (when (looking-at "[ \t]*:PROPERTIES:")
248 (re-search-forward ":END:" nil
)
252 (org-mime-compose body
(or fmt
'org
) file to subject
253 `((cc .
,cc
) (bcc .
,bcc
)))))))
255 (defun org-mime-send-buffer (&optional fmt
)
256 (run-hooks 'org-mime-send-buffer-hook
)
257 (let* ((region-p (org-region-active-p))
258 (subject (org-export-grab-title-from-buffer))
259 (file (buffer-file-name (current-buffer)))
260 (body-start (or (and region-p
(region-beginning))
261 (save-excursion (goto-char (point-min)))))
262 (body-end (or (and region-p
(region-end)) (point-max)))
263 (temp-body-file (make-temp-file "org-mime-export"))
264 (body (buffer-substring body-start body-end
)))
265 (org-mime-compose body
(or fmt
'org
) file nil subject
)))
267 (defun org-mime-compose (body fmt file
&optional to subject headers
)
269 (message-mail to subject headers nil
)
271 (flet ((bhook (body fmt
)
272 (let ((hook (intern (concat "org-mime-pre-"
275 (if (> (eval `(length ,hook
)) 0)
278 (goto-char (point-min))
279 (eval `(run-hooks ',hook
))
282 (let ((fmt (if (symbolp fmt
) fmt
(intern fmt
))))
285 (insert (org-export-string (org-babel-trim (bhook body
'org
)) 'org
)))
287 (insert (org-export-string
288 (concat "#+Title:\n" (bhook body
'ascii
)) 'ascii
)))
289 ((or (eq fmt
'html
) (eq fmt
'html-ascii
))
290 (let* ((org-link-file-path-type 'absolute
)
291 ;; we probably don't want to export a huge style file
292 (org-export-htmlize-output-type 'inline-css
)
293 (html-and-images (org-mime-replace-images
296 'html
(file-name-nondirectory file
))
298 (images (cdr html-and-images
))
299 (html (org-mime-apply-html-hook (car html-and-images
))))
300 (insert (org-mime-multipart
303 (bhook body
(if (eq fmt
'html
) 'org
'ascii
)))
304 (if (eq fmt
'html
) 'org
'ascii
))
306 (mapconcat 'identity images
"\n"))))))))
308 (defun org-mime-org-buffer-htmlize ()
309 "Create an email buffer containing the current org-mode file
310 exported to html and encoded in both html and in org formats as
313 (org-mime-send-buffer 'html
))
315 (defun org-mime-subtree ()
316 "Create an email buffer containing the current org-mode subtree
317 exported to a org format or to the format specified by the
318 MAIL_FMT property of the subtree."
320 (org-mime-send-subtree
321 (or (org-entry-get nil
"MAIL_FMT" org-mime-use-property-inheritance
) 'org
)))