Add support for sending rich-text emails with markdown

This commit is contained in:
Jeremy Dormitzer 2020-04-22 09:51:45 -04:00
parent 7d851aa259
commit 3b7d86919a

View File

@ -3739,6 +3739,67 @@ Then configure it:
(cdar (mu4e-message-field msg :from))))))))) (cdar (mu4e-message-field msg :from)))))))))
#+end_src #+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 "<html>\n<head></head>\n<body>\n%s\n</body></html>\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: Global keybindings:
#+begin_src emacs-lisp #+begin_src emacs-lisp
(leader-def-key "am" #'mu4e) (leader-def-key "am" #'mu4e)