From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Eric Schulte" Subject: Re: Re: Sending org buffer as mail? Date: Fri, 17 Dec 2010 03:02:01 -0700 Message-ID: <87wrn8emzq.fsf@gmail.com> References: <4D09C7F2.8020806@gmail.com> <87sjxy168c.fsf@gmail.com> <4D09D3BB.3020508@gmail.com> <87bp4lxl60.fsf@fastmail.fm> <871v5hw5go.fsf@gmail.com> <87k4j8rj11.fsf@gmail.com> <4D0B25FD.1030708@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from [140.186.70.92] (port=33919 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PTX8n-0005JV-Ix for emacs-orgmode@gnu.org; Fri, 17 Dec 2010 05:02:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PTX8m-0001gU-D8 for emacs-orgmode@gnu.org; Fri, 17 Dec 2010 05:02:21 -0500 Received: from mail-iy0-f169.google.com ([209.85.210.169]:55391) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PTX8m-0001gF-7i for emacs-orgmode@gnu.org; Fri, 17 Dec 2010 05:02:20 -0500 Received: by iyj17 with SMTP id 17so370888iyj.0 for ; Fri, 17 Dec 2010 02:02:19 -0800 (PST) In-Reply-To: <4D0B25FD.1030708@gmail.com> (Rainer M. Krug's message of "Fri, 17 Dec 2010 09:57:33 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Rainer M Krug Cc: Oscar Carlsson , Matt Lundin , emacs-orgmode@gnu.org, Jeff Horn --=-=-= Content-Type: text/plain Rainer M Krug writes: > On 12/17/2010 07:48 AM, Eric Schulte wrote: > Hi eric > >> I'm attaching a new version of org-mime.el which incorporates Matt's >> function below. There are now two new functions, `org-mime-org-buffer' >> and `org-mime-org-subtree' each of which takes a format argument >> specifying the format of the final email, one of 'org, 'ascii, or 'html. > > This sounds perfect. > > So now a *subtree* or *org buffer* can be send as > *org*, *ascii or *html*. > > Right? > Correct, all one need do is to write an interactive wrapping function similar to the example in my previous email. Note: In the version attached to this email the functions are renamed to `org-mime-send-buffer' and `org-mime-send-subtree'. > > I just would like to suggest two more things: > > 1) to be able to mail the subtree or buffer as an attached pdf. > > Thinking of it, this could also be used for the other formats which > result in one file, e.g. ascii, taskjuggle and I think all others except > of the html? > Sounds like a good idea, this shouldn't be overly hard to implement using the existing attachment features of org-mime. > > 2) the format of the inline text (and the attached one?) could be > specified as a property, as a property - this would make sending the > email consistent. > I'm attaching another version of org-mime which provides the org-mime-subtree interactive command which uses the MAIL_FMT property to determine the format of the mail message to populate. Thanks -- Eric --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=org-mime.el Content-Transfer-Encoding: quoted-printable ;;; org-mime.el --- org html export for text/html MIME emails ;; Copyright (C) 2010 Eric Schulte ;; Author: Eric Schulte ;; Keywords: mime, mail, email, html ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php ;; Version: 0.01 ;;; License: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; WYSWYG, html mime composition using org-mode ;; ;; For mail composed using the orgstruct-mode minor mode, this ;; provides a function for converting all or part of your mail buffer ;; to embedded html as exported by org-mode. Call `org-mime-htmlize' ;; in a message buffer to convert either the active region or the ;; entire buffer to html. ;; ;; Similarly the `org-mime-org-buffer-htmlize' function can be called ;; from within an org-mode buffer to convert the buffer to html, and ;; package the results into an email handling with appropriate MIME ;; encoding. ;; ;; you might want to bind this to a key with something like the ;; following message-mode binding ;;=20 ;; (add-hook 'message-mode-hook ;; (lambda () ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize))) ;; ;; and the following org-mode binding ;;=20 ;; (add-hook 'org-mode-hook ;; (lambda () ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize))) ;;; Code: (require 'cl) (defcustom org-mime-default-header "#+OPTIONS: latex:t\n" "Default header to control html export options, and ensure first line isn't assumed to be a title line." :group 'org-mime :type 'string) (defcustom org-mime-library 'mml "Library to use for marking up MIME elements." :group 'org-mime :type '(choice 'mml 'semi 'vm)) (defcustom org-mime-preserve-breaks t "Used as temporary value of `org-export-preserve-breaks' during mime encoding." :group 'org-mime :type 'boolean) (defcustom org-mime-fixedwith-wrap "
\n%s
\n" "Format string used to wrap a fixedwidth HTML email." :group 'org-mime :type 'string) (defcustom org-mime-html-hook nil "Hook to run over the html buffer before attachment to email. This could be used for example to post-process html elements." :group 'org-mime :type 'hook) ;; example hook, for setting a dark background in
 elements
(defun org-mime-change-element-style (element style)
  "Set new default htlm style for  elements in exported html."
  (while (re-search-forward (format "<%s" element) nil t)
    (replace-match (format "<%s style=3D\"%s\"" element style))))

(defun org-mime-change-class-style (class style)
  "Set new default htlm style for objects with classs=3DCLASS in
exported html."
  (while (re-search-forward (format "class=3D\"%s\"" class) nil t)
    (replace-match (format "class=3D\"%s\" style=3D\"%s\"" class style))))

;; ;; example addition to `org-mime-html-hook' adding a dark background
;; ;; color to 
 elements
;; (add-hook 'org-mime-html-hook
;;           (lambda ()
;;             (org-mime-change-element-style
;;              "pre" (format "color: %s; background-color: %s;"
;;                            "#E6E1DC" "#232323"))
;; 	    (org-mime-change-class-style
;;              "verse" "border-left: 2px solid gray; padding-left: 4px;")))

(defun org-mime-file (ext path id)
  "Markup a file for attachment."
  (case org-mime-library
    ('mml (format
           "<#part type=3D\"%s\" filename=3D\"%s\" id=3D\"<%s>\">\n<#/part>=
\n"
           ext path id))
    ('semi (concat
            (format
             "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base6=
4]]\n"
             ext id)
            (base64-encode-string
             (with-temp-buffer
               (set-buffer-multibyte nil)
               (binary-insert-encoded-file path)
               (buffer-string)))))
    ('vm "?")))

(defun org-mime-multipart (plain html)
  "Markup a multipart/alternative with text/plain and text/html
  alternatives."
  (case org-mime-library
    ('mml (format (concat "<#multipart type=3Dalternative><#part type=3Dtex=
t/plain>"
                          "%s<#part type=3Dtext/html>%s<#/multipart>\n")
                  plain html))
    ('semi (concat
            "--" "<>-{\n"
            "--" "[[text/plain]]\n" plain
            "--" "[[text/html]]\n"  html
            "--" "}-<>\n"))
    ('vm "?")))

(defun org-mime-replace-images (str current-file)
  "Replace images in html files with cid links."
  (let (html-images)
    (cons
     (replace-regexp-in-string ;; replace images in html
      "src=3D\"\\([^\"]+\\)\""
      (lambda (text)
        (format
         "src=3D\"cid:%s\""
         (let* ((url (and (string-match "src=3D\"\\([^\"]+\\)\"" text)
                          (match-string 1 text)))
                (path (expand-file-name
                       url (file-name-directory current-file)))
                (ext (file-name-extension path))
                (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
           (add-to-list 'html-images
                        (org-mime-file (concat "image/" ext) path id))
           id)))
      str)
     html-images)))

(defun org-mime-htmlize (arg)
  "Export a portion of an email body composed using `mml-mode' to
html using `org-mode'.  If called with an active region only
export that region, otherwise export the entire body."
  (interactive "P")
  (let* ((region-p (org-region-active-p))
         (html-start (or (and region-p (region-beginning))
                         (save-excursion
                           (goto-char (point-min))
                           (search-forward mail-header-separator)
                           (+ (point) 1))))
         (html-end (or (and region-p (region-end))
                       ;; TODO: should catch signature...
                       (point-max)))
         (raw-body (buffer-substring html-start html-end))
         (tmp-file (make-temp-name (expand-file-name
				    "mail" temporary-file-directory)))
         (body (org-export-string raw-body 'org (file-name-directory tmp-fi=
le)))
         ;; because we probably don't want to skip part of our mail
         (org-export-skip-text-before-1st-heading nil)
         ;; because we probably don't want to export a huge style file
         (org-export-htmlize-output-type 'inline-css)
         ;; makes the replies with ">"s look nicer
         (org-export-preserve-breaks org-mime-preserve-breaks)
         ;; to hold attachments for inline html images
         (html-and-images
          (org-mime-replace-images
           (org-export-string raw-body 'html (file-name-directory tmp-file))
           tmp-file))
         (html-images (unless arg (cdr html-and-images)))
         (html (org-mime-apply-html-hook
                (if arg
                    (format org-mime-fixedwith-wrap body)
                  (car html-and-images)))))
    (delete-region html-start html-end)
    (save-excursion
      (goto-char html-start)
      (insert (org-mime-multipart body html)
              (mapconcat 'identity html-images "\n")))))

(defun org-mime-apply-html-hook (html)
  (if org-mime-html-hook
      (with-temp-buffer
        (insert html)
        (goto-char (point-min))
        (run-hooks 'org-mime-html-hook)
        (buffer-string))
    html))

(defmacro org-mime-try (&rest body)
  `(condition-case nil ,@body (error nil)))

(defun org-mime-send-subtree (&optional fmt)
  (save-restriction
    (org-narrow-to-subtree)
    (let* ((file (buffer-file-name (current-buffer)))
	   (subject (nth 4 (org-heading-components)))
	   (to (org-entry-get nil "MAIL_TO"))
	   (cc (org-entry-get nil "MAIL_CC"))
	   (bcc (org-entry-get nil "MAIL_BCC"))
	   (body (buffer-substring
		  (save-excursion (goto-char (point-min))
				  (forward-line 1)
				  (when (looking-at "[ \t]*:PROPERTIES:")
				    (re-search-forward ":END:" nil)
				    (forward-char))
				  (point))
		  (point-max))))
      (org-mime-compose body (or fmt 'org) file to subject
			`((cc . ,cc) (bcc . ,bcc))))))

(defun org-mime-send-buffer (&optional fmt)
  (let* ((region-p (org-region-active-p))
	 (subject (org-export-grab-title-from-buffer))
         (file (buffer-file-name (current-buffer)))
         (body-start (or (and region-p (region-beginning))
                         (save-excursion (goto-char (point-min)))))
         (body-end (or (and region-p (region-end)) (point-max)))
	 (temp-body-file (make-temp-file "org-mime-export"))
	 (body (buffer-substring body-start body-end)))
    (org-mime-compose body (or fmt 'org) file nil subject)))

(defun org-mime-compose (body fmt file &optional to subject headers)
  (require 'message)
  (message-mail to subject headers nil)
  (message-goto-body)
  (case fmt
    ('org (insert (org-export-string (org-babel-trim body) 'org)))
    ('ascii (insert (org-export-string (concat "#+Title:\n" body)'ascii)))
    ('html (let* ((org-link-file-path-type 'absolute)
		  ;; we probably don't want to export a huge style file
		  (org-export-htmlize-output-type 'inline-css)
		  (html-and-images (org-mime-replace-images
				    (org-export-string
				     body 'html (file-name-nondirectory file))
				    file))
		  (images (cdr html-and-images))
		  (html (org-mime-apply-html-hook (car html-and-images))))
	     (insert (org-mime-multipart
		      (org-export-string (org-babel-trim body) 'org) html)
		     (mapconcat 'identity images "\n"))))))

(defun org-mime-subtree ()
  (interactive)
  (org-mime-send-subtree (or (org-entry-get nil "MAIL_FMT") 'org)))

(provide 'org-mime)

--=-=-=
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

--=-=-=--