%% \section{zmailer(3)}

{\bf Usage}
\begin{verbatim}
#include <stdio.h>
#include <zmailer.h>

FILE *mail_open(char *);

int mail_priority;

int mail_abort(FILE *);

int mail_close(FILE *);

int mail_close_alternate(FILE *mfp, char *where, char *suffix);

char *mail_alloc(unsigned int);

int mail_free(char *);

char *mail_host();
\end{verbatim}


At linkage time use {\tt -lzmailer}.

{\tt mail\_open()} will return a {\tt FILE *} to a message
file that should be written to by the application. This
message file contains three parts: the message envelope,
the message header, and the message body. The exact format of
these components depend on the message protocol,
which must be specified as the parameter to {\tt mail\_open()}.
The choices are predetermined by the capabilities of the
mailer, and are defined in the header file. The known
possibilities are:

\begin{description}
\item[ {\tt MSG\_RFC822}] \mbox{}

this is the only format supported 
by default by the mailer. The message headers and body in this format
are defined by the DARPA Request For Comments 822 and 1123. 
The message envelope syntax is similar to the message header syntax.



\item[ {\tt MSG\_FAX}] \mbox{}

intended for fax transmissions.



\item[ {\tt MSG\_UUCP}] \mbox{}

intended for old style UUCP format message headers



\item[ {\tt MSG\_X400}] \mbox{}

intended for X.400(88) messages.

\end{description}


The {\tt mail\_open()} routine will look for {\tt \$FULLNAME} 
and {\tt \$PRETTYLOGIN} environment variables and translate 
them into message envelope data for use by the mailer if it generates 
a sender address header for the message.

Note that the return value from the {\tt mail\_open()} routine
corresponds to the return value of an {\tt fopen(3)}, and similarly
the return values from {\tt mail\_abort()} and {\tt mail\_close()} 
correspond to the return value of {\tt fclose(3)}.

The {\tt mail\_priority} variable has a default value of 0, 
and is used on scanning Zmailer configuration variable 
{\tt \$ROUTERDIRS}, which tells alternate router directories 
under the {\tt \$POSTOFFICE} directory. At value 0, 
{\tt \$ROUTERDIRS} variable is not used. At higher values, 
successive directory from {\tt \$ROUTERDIRS} is taken. 
See below about Z-Environment.

The {\tt mail\_close\_alternate(3)} can be used to send currently
open message file to some alternate destination, and is
used at {\em smtpserver(8)\/} to send some quick-action requests
directly to the {\em scheduler(8)\/}.

The {\tt mail\_alloc()} and {\tt mail\_free()} routines are used 
to provide memory space for internal data structures. The versions
of these routines in the library simply call {\tt malloc(3)} and
{\tt free(3)} but an application may override them if desired.

Similarly the {\tt mail\_host()} routine is intended to return a
unique string for each host, by default the hostname, and
this too is intended to be overridden by an application
that may already have this information available in some form.

{\bf Envelope header lines}

The message envelope headers are used to carry meta-information
about the message.  The goal is to carry transport-envelope 
information separate from message (RFC-822) headers, and body.
At first the message starts with a set of envelope headers 
(*-prefix denotes optional):

\begin{verbatim}
 *external \n
 *rcvdfrom %s@%s (%s) \n
 *bodytype %s \n
 *with %s \n
 *identinfo %s \n
\end{verbatim}

Either: \\
% {\tt\ \ \ from {\(<\)}\%s{\(>\)} \(\backslash\)n} \\
\verb!  from <%s> \n! \\
Or: \\
\verb!  channel error \n! \\
% {\tt\ \ \ channel error \(\backslash\)n}

\begin{verbatim}
 *envid %s \n
 *notaryret %s \n
\end{verbatim}


Then for each recipient pairs of:
\begin{verbatim}
 *todsn [NOTIFY=...] [ORCPT=...] \n
 to <%s> \n
\end{verbatim}


Just before the data starts, a magic entry:
\begin{verbatim}
 env-end \n
\end{verbatim}


Then starts the message RFC-822 headers, and below it, the body.

{\bf Example}

\begin{verbatim}
 ... set up signal handlers ...
 FILE *mfp = mail_open(MSG_RFC822,0);
 if (mfp != NULL) {
      ... output the mail message to mfp ...
 } else
      ... error handling for not being able to open the file ...
 if (some application processing went wrong
           || we took an interrupt)
      (void) mail_abort(mfp);
 else if (mail_close(mfp) == EOF)
      ... error handling if something went wrong ...
\end{verbatim}


{\bf Environment variables}

\begin{description}


\item[ {\tt \$FULLNAME}] \mbox{}

variable defines textual fullname, 
for example: ``Sample User''



\item[ {\tt \$PRETTYLOGIN}] \mbox{}

variable defines 
{\tt user@node} format of what user wants to claim as 
his/her own address (it must match those of mail router accepts.)

\end{description}


{\bf Z-environment variables}
\begin{description}


\item[ {\tt \$POSTOFFICE}] \mbox{}

defines the directory where all 
\$POSTOFFICE functions are. 
Example: {\tt \$POSTOFFICE=/var/spool/postoffice/}



\item[ {\tt \$ROUTERDIRS}] \mbox{}

defines a `:' separated list of alternate 
router directories. If these are defined at all, they must exist, 
if alternate queueing priority mechanism is desired to be used. 

Example: {\tt \$ROUTERDIRS=router1:router2:router3:router4}

\end{description}
