Unix Network Admin   «Prev  Next»
Lesson 7Configuring sendmail with M4
ObjectiveUse M4 and a template file to create sendmail.cf automatically.

Configure sendmail with M4

`m4` is still used to create `sendmail.cf` automatically in many Unix and Unix-like systems, although its use is less common today due to the declining popularity of Sendmail in favor of simpler and more secure alternatives like Postfix and Exim.
✅ Summary:
  • sendmail.cf is the complex configuration file used by Sendmail.
  • Writing sendmail.cf manually is impractical due to its cryptic syntax.
  • Instead, administrators write a macro-based sendmail.mc file, and use the m4 macro processor to generate sendmail.cf.

🛠 Typical Workflow:
  1. Edit sendmail.mc (macro file):
    divert(-1)
    include(`/usr/share/sendmail/cf/m4/cf.m4')dnl
    VERSIONID(`custom config')dnl
    define(`SMART_HOST',`smtp.yourdomain.com')dnl
    ...
    
  2. Generate sendmail.cf using m4:
    m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
    
  3. Restart Sendmail:
    systemctl restart sendmail
        
🔍 Notes:
  • Most systems that still package Sendmail (e.g., RHEL, FreeBSD) include m4 as a dependency for this reason.
  • Modern configurations often use make in /etc/mail:
  cd /etc/mail
  make
  

⚠️ Caveat:
  • Many modern systems no longer use Sendmail by default.
    • Postfix is the default MTA on Red Hat-based and Debian-based systems now.
    • Postfix does not use m4.

✅ Conclusion: If you're still using Sendmail on a Unix system, yes, `m4` is still used to generate `sendmail.cf`. But in modern Unix environments, Sendmail is often replaced, so `m4` for `sendmail.cf` is more of a legacy practice.
Configuration files for sendmail versions 8.7 and higher can be assembled from a set of template files. The template files are written in a macro language called M4. M4 has been developed by the Free Software Foundation GNU project and can be obtained without charge from the GNU project. Most recent UNIX distributions include M4. Fortunately, it is not necessary to know much about how M4 works to use it to configure sendmail.
If you have installed the latest sendmail version yourself, then the M4 templates for the sendmail configuration file are organized in the subdirectory cf of the sendmail source directory. For future reference, let us call this directory SENDMAILSRC. On Linux, these files are located under /usr/lib/sendmail-cf, so you could use the following command to simplify the pathname:

SENDMAILSRC=/usr/lib/sendmail-cf

Assembling Configuration File

You create a sendmail.cf file by beginning with one of the template files (which have extension mc) in the SENDMAILSRC/cf directory. These files have names such as generic-solaris2.mc (for solaris) or redhat.mc (for RedHat linux).
Here is the file SENDMAILSRC/cf/generic-solaris2.mc:
#  This is a generic configuration file for
#  SunOS 5.x (a.k.a. Solaris 2.x)
#  It has support for local and SMTP mail
#  only.  If you want to customize it, 
#  copy it to a name appropriate for your 
#  environment and do the modifications there.
divert(0)dnl
VERSIONID(`@(#)generic-solaris2.mc 8.3 
(Berkeley) 3/23/96')
OSTYPE(solaris2)dnl
DOMAIN(generic)dnl
MAILER(local)dnl
MAILER(smtp)dnl

Notice the sequence of keywords (OSTYPE, DOMAIN, MAILER). You must customize this file by modifying the arguments to these keywords so that sendmail operates the way you want it to.
There are literally hundreds of potential keywords and arguments that you can place into a .mc file, reflecting the vast array of options and potential email configurations. See the README files in the SENDMAILSRC directory and its subdirectories or consult the sendmail FAQ for more information. The keywords fall into categories. Some examples include:
  1. FEATURE keyword statements turn on and off special features of sendmail.
  2. DOMAIN keyword statements control per-domain behavior for machines that handle multiple domains.
  3. MASQUERADE keywords control address rewriting and masquerading.
  4. MAILER keywords select particular mail delivery agents and transfer protocols.
Let us look at a specific Example Configuration File before attempting an exercise.

Example Configuration File

Suppose we want to modify the generic-solaris2.mc file so that our sendmail configuration forwards mail from our machine CLIENT.corporation.com, rewriting the outgoing mail so that it appears to be from [email protected], not [email protected].
We copy the file to a new file (our_configuration.mc) and add a “MASQUERADE_AS” directive to the file. We also change the VERSIONID keyword:
divert(0)dnl
VERSIONID(`Based on generic-solaris2.mc for 
corporation.com')
OSTYPE(solaris2)dnl
DOMAIN(generic)dnl
MASQUERADE_AS(corporation.com)
MAILER(local)dnl
MAILER(smtp)dnl

The file must also be in the cf/cf subdirectory. To create the sendmail.cf command, make sure that your current directory is
SENDMAILSRC/cf

and issue the following command:
m4 ../cf/m4/cf.m4 our_configuration.mc 
> newsendmail.cf

This command preloads M4, then uses the cf.m4 program to create a new file. Notice that you must enter the full path to all of the sendmail configuration files. After testing, you can then copy this file to the /etc/ directory, then kill and restart sendmail.

Configure m4 sendmail - Exercise

Click the Exercise link belowto create sendmail.cf using M4.
Configure m4 sendmail - Exercise

SEMrush Software 7 SEMrush Banner 7