home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


sendmail

sendmailSearch this book
Previous: 6.5 Things to Try Chapter 7 Next: 7.2 Defining Macros
 

7. Macros

One of the major strengths of the sendmail.cf file is that it allows text to be referenced symbolically. This is very similar to how variables in the Bourne and C shells are used:

REMOTE=mailhost          
<- Bourne shell

set REMOTE=mailhost      
<- C shell

D{REMOTE}mailhost        
<- sendmail.cf file

The statements above all cause a variable with the name REMOTE to be assigned the value mailhost .

The expression for using (referencing) the value stored in REMOTE is similar for all three:

$REMOTE              
<- Bourne shell

$REMOTE              
<- C shell

${REMOTE}            
<- sendmail.cf file

That is, all of the above expressions yield the value stored in REMOTE , in this case the text mailhost . Once you define the value of REMOTE as mailhost , you can use the expression ${REMOTE} anywhere you need to use the text mailhost .

7.1 Overview

Macros can greatly simplify your work. They allow you to represent text symbolically and to define it in one central place. Changes in that text are automatically propagated to the rest of the file. For example, consider the following definition in your sendmail.cf file:

D{REMOTE}mailhost

If you use the expression ${REMOTE} anywhere in that file, a single change to the definition of D{REMOTE}mailhost changes the value of ${REMOTE} throughout the entire file.

Here is the format for defining macros:

D

Xtext

The letter D must begin the line. The D is immediately followed by the name of the macro, here X , with no intervening space. The name is then followed (again with no intervening space) by the text (value) for the macro's definition. The value is all the text up to the end of the line. [1]

[1] Trailing whitespace is automatically stripped in V8.7 but not in earlier versions. Leading whitespace is not, so the D , name, and text must be right smack against each other.

Macros may have single-character names or multicharacter names. Multicharacter names must always be enclosed in curly braces. Single-character names may be enclosed in curly braces if you desire. Prior to V8.7 you could use single characters only without curly braces.

DRmailhost            
<- single-character name (prior to V8.7)

D{R}mailhost          
<- same beginning with V8.7

D{REMOTE}mailhost     
<- multicharacter name (beginning with V8.7)

Except for header and delivery agent configuration commands, [2] macros, in general, are expanded (the text replaces the symbol) when the configuration file is read. Consequently, you must define them before you use them:

[2] And configuration commands for which macros make no sense and are ignored.

D{ROLE}son
S${ROLE}                
<- use the text value ``son''

D{ROLE}mother
S${ROLE}                
<- use the text value ``mother''

Here, ROLE was first given the value son . When the first S command is processed, sendmail replaces the expression ${ROLE} with its current value to form

Sson

Then ROLE is redefined and given a different value, that of mother . The second S command is processed, and its ${ROLE} is replaced with the new value:

Smother

Please note that this is very bad style. In general, each macro should be defined only once to avoid confusion.