a function to org-mime-subtree then org-mime-HTMLize?

I'm trying to streamline my email-sending tasks into my org-mode workflow, and I’d like a way to quickly email a subtree to someone.

I'm trying to create a function that will:

1. org-mime-subtree
2. insert my signature (including a link)
3. org-mime-HTMLize

Here's what I have

(defun org-mime-subtree-and-HTMLize ()
"org-mime-subtree and HTMLize"
(interactive)
(org-mime-subtree)
(insert "\nBest,\nS.\n---\n
[[http://www.newyorkwritersintensive.com/][newyorkwritersintensive.com]]")
(org-mime-htmlize))


When I run it, I get the error "wrong number of arguments."

• The function org-mime-htmlize requires an argument, which appears should have been optional for non-interactive use, but is mandatory. There are conditions for if there is an arg or if there is no arg, however, the function will not work within elisp unless it has an arg. It appears that a simple t would suffice. So try it with t and see if that fixes your issue: (org-mime-htmlize t). However, I have never used this library and just Googled the function, so I'm not sure if that is all you need. – lawlist Jun 28 '15 at 6:25
• Hmm, doing (org-mime-htmlize t) changes the whole message to monospace font. I just want a function that will do the same thing as I get when I manually run org-mime-subtree and org-mime-htmlize. – incandescentman Jun 28 '15 at 7:03
• In that case, you may wish to rewrite the function org-mime-htmlize so that you can use it in elisp without the mandatory argument -- i.e., you will need to make the argument optional. To do that, you can copy the entire function over to your user customization file -- place a preceding require statement followed by the new function with the same name using &optional arg instead of arg -- like this: (require 'org-mime) (defun org-mime-htmlize (&optional arg) . . . [insert the rest of the entire function without making any changes] Save your changes and restart Emacs. – lawlist Jun 28 '15 at 14:46
• Now that you have rewritten the function, you no longer need to use (org-mime-htmlize t) -- i.e., just use (org-mime-htmlize) and you should no longer encounter an error message "wrong number of arguments". I suspect that the arg (which is now optional thanks to the rewrite of the function) being set to t is what causes the behavior you wish to avoid. – lawlist Jun 28 '15 at 20:25
• Here's the code if you want to use it for an official answer. pastebin.com/tV3pJaCW – incandescentman Jun 28 '15 at 21:19

The issue described by the original poster is caused by using org-mime-htmlize on a non-interactive basis -- i.e., the single argument is mandatory (when using said function on a non-interactive basis), rather than optional. The solution is to rewrite the function so that the argument is optional:

(require 'org-mime)

(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))
(+ (point) 1))))
(html-end (or (and region-p (region-end))
;; TODO: should catch signature...
(point-max)))
(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 mime-send-mail ()
"org-mime-subtree and HTMLize"
(interactive)
(org-mark-subtree)
(org-mime-subtree)
(insert "\nBest,\nS.\n")
(org-mime-htmlize))