Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-mime.el
blobe9114195a776d2fa6317ed42d009df2a706976cd
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
8 ;; Version: 0.01
10 ;; This file is not part of GNU Emacs.
12 ;;; License:
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)
17 ;; any later version.
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.
29 ;;; Commentary:
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
42 ;; encoding.
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
48 ;; (lambda ()
49 ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
51 ;; and the following org-mode binding
53 ;; (add-hook 'org-mode-hook
54 ;; (lambda ()
55 ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
57 ;;; Code:
58 (require 'cl)
60 (defcustom org-mime-use-property-inheritance nil
61 "Non-nil means al MAIL_ properties apply also for sublevels."
62 :group 'org-mime
63 :type 'boolean)
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."
69 :group 'org-mime
70 :type 'string)
72 (defcustom org-mime-library 'mml
73 "Library to use for marking up MIME elements."
74 :group 'org-mime
75 :type '(choice 'mml 'semi 'vm))
77 (defcustom org-mime-preserve-breaks t
78 "Used as temporary value of `org-export-preserve-breaks' during
79 mime encoding."
80 :group 'org-mime
81 :type 'boolean)
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."
86 :group 'org-mime
87 :type 'string)
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."
92 :group 'org-mime
93 :type 'hook)
95 (mapc (lambda (fmt)
96 (eval `(defcustom
97 ,(intern (concat "org-mime-pre-" fmt "-hook"))
98 nil
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
118 exported html."
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
125 ;; (lambda ()
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")
137 ext path id))
138 ('semi (concat
139 (format (concat "--[[%s\nContent-Disposition: "
140 "inline;\nContent-ID: <%s>][base64]]\n")
141 ext id)
142 (base64-encode-string
143 (with-temp-buffer
144 (set-buffer-multibyte nil)
145 (binary-insert-encoded-file path)
146 (buffer-string)))))
147 ('vm "?")))
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>"
155 plain
156 (when images "<#multipart type=related>")
157 "<#part type=text/html>"
158 html
159 images
160 (when images "<#/multipart>\n")
161 "<#/multipart>\n"))
162 ('semi (concat
163 "--" "<<alternative>>-{\n"
164 "--" "[[text/plain]]\n" plain
165 (when images (concat "--" "<<alternative>>-{\n"))
166 "--" "[[text/html]]\n" html
167 images
168 (when images (concat "--" "}-<<alternative>>\n"))
169 "--" "}-<<alternative>>\n"))
170 ('vm "?")))
172 (defun org-mime-replace-images (str current-file)
173 "Replace images in html files with cid links."
174 (let (html-images)
175 (cons
176 (replace-regexp-in-string ;; replace images in html
177 "src=\"\\([^\"]+\\)\""
178 (lambda (text)
179 (format
180 "src=\"cid:%s\""
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))
189 id)))
190 str)
191 html-images)))
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."
197 (interactive "P")
198 (let* ((region-p (org-region-active-p))
199 (html-start (or (and region-p (region-beginning))
200 (save-excursion
201 (goto-char (point-min))
202 (search-forward mail-header-separator)
203 (+ (point) 1))))
204 (html-end (or (and region-p (region-end))
205 ;; TODO: should catch signature...
206 (point-max)))
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
221 (html-and-images
222 (org-mime-replace-images
223 (org-export-string raw-body 'html (file-name-directory tmp-file))
224 tmp-file))
225 (html-images (unless arg (cdr html-and-images)))
226 (html (org-mime-apply-html-hook
227 (if arg
228 (format org-mime-fixedwith-wrap body)
229 (car html-and-images)))))
230 (delete-region html-start html-end)
231 (save-excursion
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
238 (with-temp-buffer
239 (insert html)
240 (goto-char (point-min))
241 (run-hooks 'org-mime-html-hook)
242 (buffer-string))
243 html))
245 (defmacro org-mime-try (&rest body)
246 `(condition-case nil ,@body (error nil)))
248 (defun org-mime-send-subtree (&optional fmt)
249 (save-restriction
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))))
255 (to (mp "MAIL_TO"))
256 (cc (mp "MAIL_CC"))
257 (bcc (mp "MAIL_BCC"))
258 (body (buffer-substring
259 (save-excursion (goto-char (point-min))
260 (forward-line 1)
261 (when (looking-at "[ \t]*:PROPERTIES:")
262 (re-search-forward ":END:" nil)
263 (forward-char))
264 (point))
265 (point-max))))
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)
282 (require 'message)
283 (message-mail to subject headers nil)
284 (message-goto-body)
285 (flet ((bhook (body fmt)
286 (let ((hook (intern (concat "org-mime-pre-"
287 (symbol-name fmt)
288 "-hook"))))
289 (if (> (eval `(length ,hook)) 0)
290 (with-temp-buffer
291 (insert body)
292 (goto-char (point-min))
293 (eval `(run-hooks ',hook))
294 (buffer-string))
295 body))))
296 (let ((fmt (if (symbolp fmt) fmt (intern fmt))))
297 (cond
298 ((eq fmt 'org)
299 (insert (org-export-string (org-babel-trim (bhook body 'org)) 'org)))
300 ((eq fmt 'ascii)
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
308 (org-export-string
309 (bhook body 'html)
310 'html (file-name-nondirectory file))
311 file))
312 (images (cdr html-and-images))
313 (html (org-mime-apply-html-hook (car html-and-images))))
314 (insert (org-mime-multipart
315 (org-export-string
316 (org-babel-trim
317 (bhook body (if (eq fmt 'html) 'org 'ascii)))
318 (if (eq fmt 'html) 'org 'ascii))
319 html)
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
325 mime alternatives."
326 (interactive)
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."
333 (interactive)
334 (org-mime-send-subtree
335 (or (org-entry-get nil "MAIL_FMT" org-mime-use-property-inheritance) 'org)))
337 (provide 'org-mime)