28.3. Mail Aliases

You can define mail aliases in a file named ~/.mailrc. These are short mnemonic names which stand for mail addresses or groups of mail addresses. Like many other mail programs, Emacs expands aliases when they occur in the To, From, CC, BCC, and Reply-to fields, plus their Resent- variants.

To define an alias in ~/.mailrc, write a line in the following format:

alias shortaddress fulladdresses

Here fulladdresses stands for one or more mail addresses for shortaddress to expand into. Separate multiple addresses with spaces; if an address contains a space, quote the whole address with a pair of double-quotes.

For instance, to make maingnu stand for gnu@gnu.org plus a local address of your own, put in this line:

alias maingnu gnu@gnu.org local-gnu

Emacs also recognizes include commands in .mailrc files. They look like this:

source filename

The file ~/.mailrc is used primarily by other mail-reading programs; it can contain various other commands. Emacs ignores everything in it except for alias definitions and include commands.

Another way to define a mail alias, within Emacs alone, is with the define-mail-alias command. It prompts for the alias and then the full address. You can use it to define aliases in your .emacs file, like this:

(define-mail-alias "maingnu" "gnu@gnu.org")

define-mail-alias records aliases by adding them to a variable named mail-aliases. If you are comfortable with manipulating Lisp lists, you can set mail-aliases directly. The initial value of mail-aliases is t, which means that Emacs should read .mailrc to get the proper value.

You can specify a different file name to use instead of ~/.mailrc by setting the variable mail-personal-alias-file.

Normally, Emacs expands aliases when you send the message. You do not need to expand mail aliases before sending the message, but you can expand them if you want to see where the mail will actually go. To do this, use the command M-x expand-mail-aliases; it expands all mail aliases currently present in the mail headers that hold addresses.

If you like, you can have mail aliases expand as abbrevs, as soon as you type them in (Chapter 26). To enable this feature, execute the following:

(add-hook 'mail-mode-hook 'mail-abbrevs-setup)

This can go in your .emacs file. Section 32.2.3. If you use this feature, you must use define-mail-abbrev instead of define-mail-alias; the latter does not work with this package. Note that the mail abbreviation package uses the variable mail-abbrevs instead of mail-aliases, and that all alias names are converted to lower case.

The mail abbreviation package also provides the C-c C-a (mail-interactive-insert-alias) command, which reads an alias name (with completion) and inserts its definition at point. This is useful when editing the message text itself or a header field such as Subject in which Emacs does not normally expand aliases.

Note that abbrevs expand only if you insert a word-separator character afterward. However, you can rebind C-n and M-> to cause expansion as well. Here's how to do that:

(add-hook 'mail-mode-hook
          (lambda ()
            (substitute-key-definition
              'next-line 'mail-abbrev-next-line
              mail-mode-map global-map)
            (substitute-key-definition
              'end-of-buffer 'mail-abbrev-end-of-buffer
              mail-mode-map global-map)))