Fix TBLFM line handling
[org-mode/org-tableheadings.git] / contrib / lisp / org-mime.el
blob197c71226a9f45cc28a8c64c014042baad389759
1 ;;; org-mime.el --- org html export for text/html MIME emails
3 ;; Copyright (C) 2010-2015 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 (if (and images (> (length images) 0))
167 (concat "--" "<<related>>-{\n"
168 "--" "[[text/html]]\n" html
169 images
170 "--" "}-<<related>>\n")
171 (concat "--" "[[text/html]]\n" html
172 images))
173 "--" "}-<<alternative>>\n"))
174 ('vm "?")))
176 (defun org-mime-replace-images (str current-file)
177 "Replace images in html files with cid links."
178 (let (html-images)
179 (cons
180 (replace-regexp-in-string ;; replace images in html
181 "src=\"\\([^\"]+\\)\""
182 (lambda (text)
183 (format
184 "src=\"cid:%s\""
185 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
186 (match-string 1 text)))
187 (path (expand-file-name
188 url (file-name-directory current-file)))
189 (ext (file-name-extension path))
190 (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
191 (add-to-list 'html-images
192 (org-mime-file (concat "image/" ext) path id))
193 id)))
194 str)
195 html-images)))
197 (defun org-mime-htmlize (arg)
198 "Export a portion of an email body composed using `mml-mode' to
199 html using `org-mode'. If called with an active region only
200 export that region, otherwise export the entire body."
201 (interactive "P")
202 (require 'ox-org)
203 (require 'ox-html)
204 (let* ((region-p (org-region-active-p))
205 (html-start (or (and region-p (region-beginning))
206 (save-excursion
207 (goto-char (point-min))
208 (search-forward mail-header-separator)
209 (+ (point) 1))))
210 (html-end (or (and region-p (region-end))
211 ;; TODO: should catch signature...
212 (point-max)))
213 (raw-body (concat org-mime-default-header
214 (buffer-substring html-start html-end)))
215 (tmp-file (make-temp-name (expand-file-name
216 "mail" temporary-file-directory)))
217 (body (org-export-string-as raw-body 'org t))
218 ;; because we probably don't want to export a huge style file
219 (org-export-htmlize-output-type 'inline-css)
220 ;; makes the replies with ">"s look nicer
221 (org-export-preserve-breaks org-mime-preserve-breaks)
222 ;; dvipng for inline latex because MathJax doesn't work in mail
223 (org-html-with-latex 'dvipng)
224 ;; to hold attachments for inline html images
225 (html-and-images
226 (org-mime-replace-images
227 (org-export-string-as raw-body 'html t) tmp-file))
228 (html-images (unless arg (cdr html-and-images)))
229 (html (org-mime-apply-html-hook
230 (if arg
231 (format org-mime-fixedwith-wrap body)
232 (car html-and-images)))))
233 (delete-region html-start html-end)
234 (save-excursion
235 (goto-char html-start)
236 (insert (org-mime-multipart
237 body html (mapconcat 'identity html-images "\n"))))))
239 (defun org-mime-apply-html-hook (html)
240 (if org-mime-html-hook
241 (with-temp-buffer
242 (insert html)
243 (goto-char (point-min))
244 (run-hooks 'org-mime-html-hook)
245 (buffer-string))
246 html))
248 (defmacro org-mime-try (&rest body)
249 `(condition-case nil ,@body (error nil)))
251 (defun org-mime-send-subtree (&optional fmt)
252 (save-restriction
253 (org-narrow-to-subtree)
254 (run-hooks 'org-mime-send-subtree-hook)
255 (let* ((mp (lambda (p) (org-entry-get nil p org-mime-use-property-inheritance)))
256 (file (buffer-file-name (current-buffer)))
257 (subject (or (funcall mp "MAIL_SUBJECT") (nth 4 (org-heading-components))))
258 (to (funcall mp "MAIL_TO"))
259 (cc (funcall mp "MAIL_CC"))
260 (bcc (funcall mp "MAIL_BCC"))
261 (body (buffer-substring
262 (save-excursion (goto-char (point-min))
263 (forward-line 1)
264 (when (looking-at "[ \t]*:PROPERTIES:")
265 (re-search-forward ":END:" nil)
266 (forward-char))
267 (point))
268 (point-max))))
269 (org-mime-compose body (or fmt 'org) file to subject
270 `((cc . ,cc) (bcc . ,bcc))))))
272 (defun org-mime-send-buffer (&optional fmt)
273 (run-hooks 'org-mime-send-buffer-hook)
274 (let* ((region-p (org-region-active-p))
275 (file (buffer-file-name (current-buffer)))
276 (subject (if (not file) (buffer-name (buffer-base-buffer))
277 (file-name-sans-extension
278 (file-name-nondirectory file))))
279 (body-start (or (and region-p (region-beginning))
280 (save-excursion (goto-char (point-min)))))
281 (body-end (or (and region-p (region-end)) (point-max)))
282 (temp-body-file (make-temp-file "org-mime-export"))
283 (body (buffer-substring body-start body-end)))
284 (org-mime-compose body (or fmt 'org) file nil subject)))
286 (defun org-mime-compose (body fmt file &optional to subject headers)
287 (require 'message)
288 (message-mail to subject headers nil)
289 (message-goto-body)
290 (let ((bhook
291 (lambda (body fmt)
292 (let ((hook (intern (concat "org-mime-pre-"
293 (symbol-name fmt)
294 "-hook"))))
295 (if (> (eval `(length ,hook)) 0)
296 (with-temp-buffer
297 (insert body)
298 (goto-char (point-min))
299 (eval `(run-hooks ',hook))
300 (buffer-string))
301 body))))
302 (fmt (if (symbolp fmt) fmt (intern fmt))))
303 (cond
304 ((eq fmt 'org)
305 (require 'ox-org)
306 (insert (org-export-string-as
307 (org-babel-trim (funcall bhook body 'org)) 'org t)))
308 ((eq fmt 'ascii)
309 (require 'ox-ascii)
310 (insert (org-export-string-as
311 (concat "#+Title:\n" (funcall bhook body 'ascii)) 'ascii t)))
312 ((or (eq fmt 'html) (eq fmt 'html-ascii))
313 (require 'ox-ascii)
314 (require 'ox-org)
315 (let* ((org-link-file-path-type 'absolute)
316 ;; we probably don't want to export a huge style file
317 (org-export-htmlize-output-type 'inline-css)
318 (html-and-images
319 (org-mime-replace-images
320 (org-export-string-as (funcall bhook body 'html) 'html t) file))
321 (images (cdr html-and-images))
322 (html (org-mime-apply-html-hook (car html-and-images))))
323 (insert (org-mime-multipart
324 (org-export-string-as
325 (org-babel-trim
326 (funcall bhook body (if (eq fmt 'html) 'org 'ascii)))
327 (if (eq fmt 'html) 'org 'ascii) t)
328 html)
329 (mapconcat 'identity images "\n")))))))
331 (defun org-mime-org-buffer-htmlize ()
332 "Create an email buffer containing the current org-mode file
333 exported to html and encoded in both html and in org formats as
334 mime alternatives."
335 (interactive)
336 (org-mime-send-buffer 'html))
338 (defun org-mime-subtree ()
339 "Create an email buffer containing the current org-mode subtree
340 exported to a org format or to the format specified by the
341 MAIL_FMT property of the subtree."
342 (interactive)
343 (org-mime-send-subtree
344 (or (org-entry-get nil "MAIL_FMT" org-mime-use-property-inheritance) 'org)))
346 (provide 'org-mime)