From 3b7d86919a0345b9257a8ee5ac5d6d033cfbaaa3 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 22 Apr 2020 09:51:45 -0400 Subject: [PATCH] Add support for sending rich-text emails with markdown --- emacs/init.org | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/emacs/init.org b/emacs/init.org index cbe0b79..38432b6 100755 --- a/emacs/init.org +++ b/emacs/init.org @@ -3739,6 +3739,67 @@ Then configure it: (cdar (mu4e-message-field msg :from))))))))) #+end_src +Support sending rich-text emails via Markdown: +#+BEGIN_SRC emacs-lisp + (defun multipart-html-message (plain html) + "Creates a multipart HTML email with a text part and an html part." + (concat "<#multipart type=alternative>\n" + "<#part type=text/plain>" + plain + "<#part type=text/html>\n" + html + "<#/multipart>\n")) + + (defun convert-message-to-markdown () + (interactive) + (unless (executable-find "pandoc") + (error "Pandoc not found, unable to convert message")) + (let* ((begin + (save-excursion + (goto-char (point-min)) + (search-forward mail-header-separator))) + (end (point-max)) + (html-buf (generate-new-buffer "*mail-md-output*")) + (exit-code + (call-process-region begin end "pandoc" nil html-buf nil + "--quiet" "-f" "gfm" "-t" "html")) + (html (format "\n\n\n%s\n\n" + (with-current-buffer html-buf + (buffer-substring (point-min) (point-max))))) + (raw-body (buffer-substring begin end))) + (when (not (= exit-code 0)) + (error "Markdown conversion failed, see %s" (buffer-name html-buf))) + (with-current-buffer html-buf + (set-buffer-modified-p nil) + (kill-buffer)) + (undo-boundary) + (delete-region begin end) + (save-excursion + (goto-char begin) + (newline) + (insert (multipart-html-message raw-body html))))) + + (defun message-md-send (&optional arg) + "Convert the current buffer and send it. + If given prefix arg ARG, skips markdown conversion." + (interactive "P") + (unless arg + (convert-message-to-markdown)) + (message-send)) + + (defun message-md-send-and-exit (&optional arg) + "Convert the current buffer and send it, then exit from mail buffer. + If given prefix arg ARG, skips markdown conversion." + (interactive "P") + (unless arg + (convert-message-to-markdown)) + (message-send-and-exit)) + + (define-key message-mode-map (kbd "C-c C-s") #'message-md-send) + (define-key message-mode-map (kbd "C-c C-c") #'message-md-send-and-exit) +#+END_SRC + + Global keybindings: #+begin_src emacs-lisp (leader-def-key "am" #'mu4e)