updated names in test-ob-shell
[org-mode.git] / contrib / lisp / org-mime.el
blobb0007ac8af3ba17cd5cce84ff34ea59f5c32526f
1 ;;; org-mime.el --- org html export for text/html MIME emails
3 ;; Copyright (C) 2010-2014 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. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
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
40 ;; encoding.
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
46 ;; (lambda ()
47 ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
49 ;; and the following org-mode binding
51 ;; (add-hook 'org-mode-hook
52 ;; (lambda ()
53 ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
55 ;;; Code:
56 (require 'cl)
58 (declare-function org-export-string-as "ox"
59 (string backend &optional body-only ext-plist))
61 (defcustom org-mime-use-property-inheritance nil
62 "Non-nil means al MAIL_ properties apply also for sublevels."
63 :group 'org-mime
64 :type 'boolean)
66 (defcustom org-mime-default-header
67 "#+OPTIONS: latex:t\n"
68 "Default header to control html export options, and ensure
69 first line isn't assumed to be a title line."
70 :group 'org-mime
71 :type 'string)
73 (defcustom org-mime-library 'mml
74 "Library to use for marking up MIME elements."
75 :group 'org-mime
76 :type '(choice 'mml 'semi 'vm))
78 (defcustom org-mime-preserve-breaks t
79 "Used as temporary value of `org-export-preserve-breaks' during
80 mime encoding."
81 :group 'org-mime
82 :type 'boolean)
84 (defcustom org-mime-fixedwith-wrap
85 "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
86 "Format string used to wrap a fixedwidth HTML email."
87 :group 'org-mime
88 :type 'string)
90 (defcustom org-mime-html-hook nil
91 "Hook to run over the html buffer before attachment to email.
92 This could be used for example to post-process html elements."
93 :group 'org-mime
94 :type 'hook)
96 (mapc (lambda (fmt)
97 (eval `(defcustom
98 ,(intern (concat "org-mime-pre-" fmt "-hook"))
99 nil
100 (concat "Hook to run before " fmt " export.\nFunctions "
101 "should take no arguments and will be run in a "
102 "buffer holding\nthe text to be exported."))))
103 '("ascii" "org" "html"))
105 (defcustom org-mime-send-subtree-hook nil
106 "Hook to run in the subtree in the Org-mode file before export.")
108 (defcustom org-mime-send-buffer-hook nil
109 "Hook to run in the Org-mode file before export.")
111 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
112 (defun org-mime-change-element-style (element style)
113 "Set new default htlm style for <ELEMENT> elements in exported html."
114 (while (re-search-forward (format "<%s" element) nil t)
115 (replace-match (format "<%s style=\"%s\"" element style))))
117 (defun org-mime-change-class-style (class style)
118 "Set new default htlm style for objects with classs=CLASS in
119 exported html."
120 (while (re-search-forward (format "class=\"%s\"" class) nil t)
121 (replace-match (format "class=\"%s\" style=\"%s\"" class style))))
123 ;; ;; example addition to `org-mime-html-hook' adding a dark background
124 ;; ;; color to <pre> elements
125 ;; (add-hook 'org-mime-html-hook
126 ;; (lambda ()
127 ;; (org-mime-change-element-style
128 ;; "pre" (format "color: %s; background-color: %s;"
129 ;; "#E6E1DC" "#232323"))
130 ;; (org-mime-change-class-style
131 ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
133 (defun org-mime-file (ext path id)
134 "Markup a file for attachment."
135 (case org-mime-library
136 ('mml (format (concat "<#part type=\"%s\" filename=\"%s\" "
137 "disposition=inline id=\"<%s>\">\n<#/part>\n")
138 ext path id))
139 ('semi (concat
140 (format (concat "--[[%s\nContent-Disposition: "
141 "inline;\nContent-ID: <%s>][base64]]\n")
142 ext id)
143 (base64-encode-string
144 (with-temp-buffer
145 (set-buffer-multibyte nil)
146 (binary-insert-encoded-file path)
147 (buffer-string)))))
148 ('vm "?")))
150 (defun org-mime-multipart (plain html &optional images)
151 "Markup a multipart/alternative with text/plain and text/html alternatives.
152 If the html portion of the message includes images wrap the html
153 and images in a multipart/related part."
154 (case org-mime-library
155 ('mml (concat "<#multipart type=alternative><#part type=text/plain>"
156 plain
157 (when images "<#multipart type=related>")
158 "<#part type=text/html>"
159 html
160 images
161 (when images "<#/multipart>\n")
162 "<#/multipart>\n"))
163 ('semi (concat
164 "--" "<<alternative>>-{\n"
165 "--" "[[text/plain]]\n" plain
166 (when images (concat "--" "<<alternative>>-{\n"))
167 "--" "[[text/html]]\n" html
168 images
169 (when images (concat "--" "}-<<alternative>>\n"))
170 "--" "}-<<alternative>>\n"))
171 ('vm "?")))
173 (defun org-mime-replace-images (str current-file)
174 "Replace images in html files with cid links."
175 (let (html-images)
176 (cons
177 (replace-regexp-in-string ;; replace images in html
178 "src=\"\\([^\"]+\\)\""
179 (lambda (text)
180 (format
181 "src=\"cid:%s\""
182 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
183 (match-string 1 text)))
184 (path (expand-file-name
185 url (file-name-directory current-file)))
186 (ext (file-name-extension path))
187 (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
188 (add-to-list 'html-images
189 (org-mime-file (concat "image/" ext) path id))
190 id)))
191 str)
192 html-images)))
194 (defun org-mime-htmlize (arg)
195 "Export a portion of an email body composed using `mml-mode' to
196 html using `org-mode'. If called with an active region only
197 export that region, otherwise export the entire body."
198 (interactive "P")
199 (require 'ox-org)
200 (require 'ox-html)
201 (let* ((region-p (org-region-active-p))
202 (html-start (or (and region-p (region-beginning))
203 (save-excursion
204 (goto-char (point-min))
205 (search-forward mail-header-separator)
206 (+ (point) 1))))
207 (html-end (or (and region-p (region-end))
208 ;; TODO: should catch signature...
209 (point-max)))
210 (raw-body (concat org-mime-default-header
211 (buffer-substring html-start html-end)))
212 (tmp-file (make-temp-name (expand-file-name
213 "mail" temporary-file-directory)))
214 (body (org-export-string-as raw-body 'org t))
215 ;; because we probably don't want to export a huge style file
216 (org-export-htmlize-output-type 'inline-css)
217 ;; makes the replies with ">"s look nicer
218 (org-export-preserve-breaks org-mime-preserve-breaks)
219 ;; dvipng for inline latex because MathJax doesn't work in mail
220 (org-html-with-latex 'dvipng)
221 ;; to hold attachments for inline html images
222 (html-and-images
223 (org-mime-replace-images
224 (org-export-string-as raw-body 'html t) 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 (require 'ox-org)
300 (insert (org-export-string-as
301 (org-babel-trim (bhook body 'org)) 'org t)))
302 ((eq fmt 'ascii)
303 (require 'ox-ascii)
304 (insert (org-export-string-as
305 (concat "#+Title:\n" (bhook body 'ascii)) 'ascii t)))
306 ((or (eq fmt 'html) (eq fmt 'html-ascii))
307 (require 'ox-ascii)
308 (require 'ox-org)
309 (let* ((org-link-file-path-type 'absolute)
310 ;; we probably don't want to export a huge style file
311 (org-export-htmlize-output-type 'inline-css)
312 (html-and-images
313 (org-mime-replace-images
314 (org-export-string-as (bhook body 'html) 'html t) file))
315 (images (cdr html-and-images))
316 (html (org-mime-apply-html-hook (car html-and-images))))
317 (insert (org-mime-multipart
318 (org-export-string-as
319 (org-babel-trim
320 (bhook body (if (eq fmt 'html) 'org 'ascii)))
321 (if (eq fmt 'html) 'org 'ascii) t)
322 html)
323 (mapconcat 'identity images "\n"))))))))
325 (defun org-mime-org-buffer-htmlize ()
326 "Create an email buffer containing the current org-mode file
327 exported to html and encoded in both html and in org formats as
328 mime alternatives."
329 (interactive)
330 (org-mime-send-buffer 'html))
332 (defun org-mime-subtree ()
333 "Create an email buffer containing the current org-mode subtree
334 exported to a org format or to the format specified by the
335 MAIL_FMT property of the subtree."
336 (interactive)
337 (org-mime-send-subtree
338 (or (org-entry-get nil "MAIL_FMT" org-mime-use-property-inheritance) 'org)))
340 (provide 'org-mime)