How to use yasnippets to produce email templates in Emacs

  |   Source

CREATED: <2013-05-01 Wed>

UPDATED: <2015-11-13 Fri>

Problem

As a freelancer, I need reply agent's email frequently.

So I have several issues to resolve:

  • Avoid writing multiple mails with similar content
  • Avoid attaching same resume again and again
  • Get the agent's name right. This is not easy since I'm living in Sydney

Solution

Yasnippet is the best solution for email templates because you can embed Lisp code in the template.

Templates

I publicize my .emacs.d at github. But email templates are my private stuff. So I place them in different location.

That's why I place templates at ~/my-yasnippets/message-mode/.

Here is a sample template to reply my agents (jobok.yasnippet):

# -*- mode: snippet -*-
# name: email for OK job
# key: jobok
# --
Hi ${1:`(my-yas-get-first-name-from-to-field)`},

Thank you very much for providing me this great opportunity.

Unfortunately I'm occupied now and will not consider new job for the time being.

Currently I'm working for ${2:COMPANY_NAME} as a contract developer. However, the
contract will be over ${3:END_OF_CONTRACT} and I will be available then.

So keep in touch.

Attached is my latest CV.
$0

<#part type="application/msword" filename="~/org/cv/cv.doc" disposition=attachment description=resume>
<#/part>

`(my-yas-get-first-name-from-to-field)` is a Lisp function embedded in the snippet.

It will automatically fetch the first name of agent,

(defun my-yas-get-first-name-from-to-field ()
  (let ((rlt "AGENT_NAME") str)
    (save-excursion
      (goto-char (point-min))
      ;; first line in email could be some hidden line containing NO to field
      (setq str (buffer-substring-no-properties (point-min) (point-max))))
    (if (string-match "^To: \"\\([^ ,]+\\)" str)
        (setq rlt (match-string 1 str)))
    (message "rlt=%s" rlt)
    rlt))

Emacs setup

Add following code into .emacs to make yasnippet load the emacs templates:

(require 'yasnippet)
(setq my-yasnippets (expand-file-name "~/my-yasnippets"))
(if (and  (file-exists-p my-yasnippets) (not (member my-yasnippets yas-snippet-dirs)))
    (add-to-list 'yas-snippet-dirs my-yasnippets))
;; yasnippet setup code should be AFTER
(yas-global-mode 1)
Comments powered by Disqus