Frequently, I find myself having to send code snippets to teammates, etc.

Therefore, I decided to write the following elisp to simplify the process. I am using mutt as the email client, but the only issue with this is the fact that the code is not properly formatted in the email.

(defun email_mark (addr subj)
   (interactive "sEnter email address: \nsEnter email subject: ")
   (let ((fname "~/.emacs.d/files/email.txt"))
     (write-region (region-beginning) (region-end) fname)
     (shell-command (format "mutt -e 'set content_type=text/html' -s '%s' -- '%s' < %s" subj addr fname))))

(global-set-key "\C-x\m" 'email_mark)

Screenshot of email sent using this function: enter image description here

Any suggestions?

  • The write-region call works for me and reproduces indentation exactly (I assume you're using spaces rather than tabs?). Hence, I'm guessing your problem is in the call to format. – Dan Feb 3 '15 at 0:54
  • Just updated my question. The screenshot of the email I receive shows how the code is not properly formatted in the email. – gkumar7 Feb 3 '15 at 1:01
  • Snt it as text/plain, not as HTML? – Alan Shutko Feb 3 '15 at 3:42
  • Yes, you are correct. Actually, was playing with a few settings in mutt, but relatively no improvement. – gkumar7 Feb 3 '15 at 5:14

You might want to use HTML for that. I advise you to use Org-mode's syntax to write your email and then use the org-mode's HTML exporter. Because you are a lucky guy, you can use org-mime to do just that! From the description

org-mime can be used to send HTML email using Org-mode HTML export. 

After doing more research into what my options are, I decided to instead use markdown and gimli to create a pdf which I could then attach to the resulting email.

Gimli: https://github.com/walle/gimli

I also added an "Email body: " prompt so that the user could incorporate a message instead of just the pdf attachment. Nevertheless, I am open to any more suggestions (possibly instead of having the code attached as a pdf, include it in the body of the email?

-> actually was able to use html to do this, but without syntax highlighting [for those who are interested, I used google-code-prettify: http://code.google.com/p/google-code-prettify/ ]).

(defun email_mark (addr subj body)
   (interactive "sEnter email address: \nsEnter email subject: \nsEnter email body: ")
   (let* ((default-directory "/files/")
          (fname "email.md")
          (fname-no-ext (substring fname 0 -3))
          (pdf-file (concat fname-no-ext ".pdf"))
          (snip (buffer-substring (region-beginning) (region-end)))
          (lang (if (equal "emacs-lisp" (substring (format "%s" major-mode) 0 -5)) "lisp")))
     (write-region (format "```%s\n%s\n```" lang snip) (region-end) fname)
     (shell-command (format "gimli -f %s -outputdir %s" (concat default-directory fname) default-directory))
     (shell-command (format "echo '%s' | mutt -s '%s' -a '%s' -- '%s'" body subj (concat default-directory pdf-file) addr))))

(global-set-key "\C-x\m" 'email_mark)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.