33.3 Mail Aliases

You can define mail aliases, which are short mnemonic names that stand for one or more mailing addresses. By default, mail aliases are defined in the file ~/.mailrc. You can specify a different file name to use, by setting the variable mail-personal-alias-file.

To define an alias in ~/.mailrc, write a line like this:

alias nick fulladdresses

This means that nick should expand into fulladdresses, where fulladdresses can be either a single address, or multiple addresses separated with spaces. 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

If an address contains a space, quote the whole address with a pair of double quotes, like this:

alias jsmith "John Q. Smith <none@example.com>"

Note that you need not include double quotes around individual parts of the address, such as the person’s full name. Emacs puts them in if they are needed. For instance, it inserts the above address as ‘"John Q. Smith" <none@example.com>’.

After editing the ~/.mailrc file, or if the file was modified outside of Emacs, you can update the mail aliases used by a running Emacs session with M-x rebuild-mail-abbrevs RET. This prompts for the name of the file to use, the default being the value of mail-personal-alias-file. A similar command merge-mail-abbrevs prompts for a file with mail aliases, then merges the aliases in that file with the existing ones.

Alternatively, you can use Emacs commands to define mail aliases. The command define-mail-abbrev prompts for the alias and the full address, and defines the alias to expand to the full address. Emacs will save the added aliases whenever it offers to save all files (for C-x s or C-x C-c), like it does with other abbrevs (see Saving Abbrevs).

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

source filename

The ~/.mailrc file is not unique to Emacs; many other mail-reading programs use it for mail aliases, and it can contain various other commands. However, Emacs ignores everything except alias definitions and include commands.

Mail aliases expand as abbrevs—that is to say, as soon as you type a word-separator character after an alias (see Abbrevs). This expansion takes place only within the ‘To’, ‘From’, ‘CC’, ‘BCC’, and ‘Reply-To’ header fields (plus their ‘Resent-’ variants); it does not take place in other header fields, such as ‘Subject’.

You can also insert an aliased address directly, using the command M-x mail-abbrev-insert-alias. This reads an alias name, with completion, and inserts its definition at point.

The command mail-abbrev-complete-alias completes on the mail alias preceding point.