A question on mu4e and Org interaction

393 views
Skip to first unread message

Marcin Borkowski

unread,
Oct 29, 2017, 11:25:27 AM10/29/17
to mu-di...@googlegroups.com
Hi there,

what is the current state of affairs with respect to sending HTML emails
with mu4e via Org-export? I.e., I want formatting/images/LaTeX
equations in my email, and I want Org-mode to take care of converting
the message to HTML?

I found this:
https://vxlabs.com/2015/01/28/sending-emails-with-math-and-source-code/,
but I don't know whether this is still relevant.

TIA,

--
Marcin Borkowski

Matt Price

unread,
Nov 16, 2017, 10:12:01 AM11/16/17
to mu-discuss
I think that org-mime is currently broken. I use a copy of the old org-mime, from about 2015, to convert messages from org to HTML before sending, but I only do this programmatically with a script that I use for grading student papers. At present I have a hard time doing the conversion manually and so I don't bother, falling back to Thunderbird for the rare emails when I really need HTML. 

I can share that code when I'm at the right ocmputer if someone thinks it's of interest.  But it's very poorly written. 

nils.s...@gmail.com

unread,
Nov 21, 2017, 5:16:57 AM11/21/17
to mu-discuss
Mi Marcin,

I am using a different approach. It works more or less well.
Less, becaue I can only write one html mail without being disturbed by the question if I like to resend the mail.
There somehow seems to be a problem with indexing of the html mails.

But take a look a the code:
;; Org-mode integration
(require 'org-mime)
(setq org-mime-library '
mml)

(defun org-mime-htmlize (&optional 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")
 
(require 'ox-org)
  (require '
ox-html)
 
(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 (concat org-mime-default-header
                           
(buffer-substring html-start html-end)))
         
(tmp-file (make-temp-name (expand-file-name
                                   
"mail" temporary-file-directory)))
         
(body (org-export-string-as raw-body 'org t))
         ;; 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)
         ;; dvipng for inline latex because MathJax doesn'
t work in mail
         
(org-html-with-latex 'dvipng)
         ;; to hold attachments for inline html images
         (html-and-images
          (org-mime-replace-images
           (org-export-string-as raw-body '
html t) 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-org-buffer-htmlize ()
  "Create an email buffer containing the current org-mode file
      exported to html and encoded in both html and in org formats as
      mime alternatives."
  (interactive)
  (org-mime-send-buffer '
html)
 
(message-goto-to))

(defun org-mime-subtree ()
 
"Create an email buffer containing the current org-mode subtree
  exported to a org format or to the format specified by the
  MAIL_FMT property of the subtree."

 
(interactive)
 
(org-mime-send-subtree
   
(or (org-entry-get nil "MAIL_FMT" org-mime-use-property-inheritance) 'org))
  (message-goto-to))

(defun htmlize-and-send ()
  "When in an org-mu4e-compose-org-mode message, htmlize and send it."
  (interactive)
  (when (member '
org~mu4e-mime-switch-headers-or-body post-command-hook)
   
(org-mime-htmlize)
   
(message-send-and-exit)
   
(switch-to-buffer " *mu4e-main*")))

(defun org-mime-compose (body fmt file &optional to subject headers)
 
(require 'message)
  (let ((bhook
         (lambda (body fmt)
           (let ((hook (intern (concat "org-mime-pre-"
                                       (symbol-name fmt)
                                       "-hook"))))
             (if (> (eval `(length ,hook)) 0)
                 (with-temp-buffer
                   (insert body)
                   (goto-char (point-min))
                   (eval `(run-hooks '
,hook))
                   
(buffer-string))
               body
))))
       
(fmt (if (symbolp fmt) fmt (intern fmt)))
       
(files (org-element-map (org-element-parse-buffer) 'link
                 (lambda (link)
                   (when (string= (org-element-property :type link) "file")
                     (file-truename (org-element-property :path link)))))))
    (compose-mail to subject headers nil)
    (message-goto-body)
    (cond
     ((eq fmt '
org)
     
(require 'ox-org)
      (insert (org-export-string-as
               (org-babel-trim (funcall bhook body '
org)) 'org t)))
     ((eq fmt '
ascii)
     
(require 'ox-ascii)
      (insert (org-export-string-as
               (concat "#+Title:\n" (funcall bhook body '
ascii)) 'ascii t)))
     ((or (eq fmt '
html) (eq fmt 'html-ascii))
      (require '
ox-ascii)
     
(require 'ox-org)
      (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)
             
(org-html-with-latex 'dvisvgm)
             (html-and-images
              (org-mime-replace-images
               (org-export-string-as (funcall bhook body '
html) 'html t)))
             (images (cdr html-and-images))
             (html (org-mime-apply-html-hook (car html-and-images))))
        (insert (org-mime-multipart
                 (org-export-string-as
                  (org-babel-trim
                   (funcall bhook body (if (eq fmt '
html) 'org 'ascii)))
                 
(if (eq fmt 'html) 'org 'ascii) t)
                 html)
                (mapconcat '
identity images "\n")))))
   
(mapc #'mml-attach-file files)))

(add-hook 'org-ctrl-c-ctrl-c-hook 'htmlize-and-send t)

(define-key mu4e-compose-mode-map (kbd "C-c o") 'org-mu4e-compose-org-mode)
(global-set-key (kbd "C-c o") '
org-mu4e-compose-org-mode)

If you press C-c o in a message buffer in the message area. You start to compose an html mail using org-mode with all its features.
Unfortunately. I don't know where I found this function. I did not save the source. If anyone knows the author, please mention him.

I found a similar solution on http://kitchingroup.cheme.cmu.edu/blog/2016/10/29/Sending-html-emails-from-org-mode-with-org-mime/
But this is not exactly the same implementation.
If you find out how to modify the code, so that the sent emails are indexed in the correct way. Then let me know. This would be the perfect way to handle org-mode integration.

With regards,
Nils

nils.s...@gmail.com

unread,
Nov 22, 2017, 7:54:02 AM11/22/17
to mu-discuss
I forgot to mention that you also will have to set the following variable in your .emacs file:

;; when mail is sent, automatically convert org body to HTML
(setq org-mu4e-convert-to-html t)



nils.s...@gmail.com

unread,
Nov 27, 2017, 3:36:26 AM11/27/17
to mu-discuss
I went on searching and found the following pull request.
https://github.com/djcb/mu/pull/952
See also:
https://matt.hackinghistory.ca/2016/11/30/using-mu4e-and-org-mime-together/

So after updating mu4e and adapting the htmlize-and-send function according to the following webpage
https://matt.hackinghistory.ca/2016/11/18/sending-html-mail-with-mu4e/
I got rid of the "resend and save" problem. So right now I can send as much html mails as I like and they are all saved in the Sent folder.
This is really great.

For quick reference I post Matt's adapted version of the htmlize-and-send function:

(
defun htmlize-and-send ()
  "When in an org-mu4e-compose-org-mode message, htmlize and send it."
  (interactive)
  (when (member 'org~mu4e-mime-switch-headers-or-body post-command-hook)
    (org-mime-htmlize)
    (org-mu4e-compose-org-mode)
    (mu4e-compose-mode)
    (message-send-and-exit)))

To summarize: If you want to send html mails with org-mode and mu4e: Get the latest version of mu4e. Copy the above code to your .emacs (or your mu4e config file) and adapt the htmlize-and-send function according to Matt. Don't forget to set the org-mu4e-convert-to-html variable. And that's it.

I would like to say that this is really great and that I am glad that is working so perfectly right now. Thanks to everyone.
Greets,
Nils

Matt Price

unread,
Nov 27, 2017, 2:24:29 PM11/27/17
to mu-di...@googlegroups.com
So glad you found that post, Nils! Id forgotten I'd written it!

--
You received this message because you are subscribed to the Google Groups "mu-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mu-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages