Mail Filter Example
This sample code is taken from the sendmail.org distribution, the sendmail/milter/README file:
Note that this filter may not be thread safe on some operating systems. You should check your system man pages for the functions used below to verify the functions are thread safe.
/* A trivial filter that logs all email to a file. */
#include <sts/types.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sysexits.h> #include <unistd.h> #include "libmilter/mfapi.h"
#ifndef true typedef int bool;
#define false 0
#define true 1
#endif /* ! true */
struct mlfiPriv
{
char *mlfi_fname;
FILE *mlfi_fp;
}; |
|
#define MLFIPRIV | ((struct mlfiPriv *) smfi_getpriv(ctx)) |
extern sfsistat
mlfi_cleanup(SMFICTX *, bool); sfsistat mlfi_envfrom(ctx, envfrom)
SMFICTX *ctx; char **envfrom;
{
struct mlfiPriv *priv; int fd =
/* allocate some private memory */ priv = malloc(sizeof *priv);
if (priv == NULL)
{
/* can't accept this message right now */ return SMFIS_TEMPFAIL;
}
memset(priv, '\0', sizeof *priv);
/* open a file to store this message */
{free(priv);
return SMFIS_TEMPFAIL;
}
if ((fd =
{
if (fd >= 0)
(void) close(fd);
return SMFIS_TEMPFAIL;
}
/* save the private data */ smfi_setpriv(ctx, priv);
/* continue processing */ return SMFIS_CONTINUE;
}
sfsistat
mlfi_header(ctx, headerf, headerv) SMFICTX *ctx;
char *headerf; char *headerv;
{
/* write the header to the log file */
/* continue processing */ return SMFIS_CONTINUE;
}
sfsistat mlfi_eoh(ctx)
SMFICTX *ctx;
{
/* output the blank line between the header and the body */