Exim Internet Mailer



Chapter 12 - Embedded Perl


EXIM_PERL = perl.o

in your Local/Makefile and then build Exim in the normal way.

1. Setting up so Perl can be used



perl_startup = do '/etc/exim.pl'


  • Setting perl_at_start (a boolean option) in the configuration requests a startup when Exim is entered.

  • The command line option -ps also requests a startup when Exim is entered, overriding the setting of perl_at_start.


2. Calling Perl subroutines





The return value of the Perl subroutine is evaluated in a scalar context before it is passed back to Exim to be inserted into the expanded string. If the return value is undef, the expansion is forced to fail in the same way as an explicit “fail” on an if or lookup item. If the subroutine aborts by obeying Perl’s die function, the expansion fails with the error message that was passed to die.

3. Calling Exim functions from Perl

Within any Perl code called from Exim, the function Exim::expand_string() is available to call back into Exim’s string expansion function. For example, the Perl code



If the string expansion is forced to fail by a “fail” item, the result of Exim::expand_string() is undef. If there is a syntax error in the expansion string, the Perl call from the original expansion string fails with an appropriate error message, in the same way as if die were used.

Two other Exim functions are available for use from within Perl code. Exim::debug_write() writes a string to the standard error stream if Exim’s debugging is enabled. If you want a newline at the end, you must supply it. Exim::log_write() writes a string to Exim’s main log, adding a leading timestamp. In this case, you should not supply a terminating newline.

4. Use of standard output and error by Perl

You should not write to the standard error or output streams from within your Perl code, as it is not defined how these are set up. In versions of Exim before 4.50, it is possible for the standard output or error to refer to the SMTP connection during message reception via the daemon. Writing to this stream is certain to cause chaos. From Exim 4.50 onwards, the standard output and error streams are connected to /dev/null in the daemon. The chaos is avoided, but the output is lost.

The Perl warn statement writes to the standard error stream by default. Calls to warn may be embedded in Perl modules that you use, but over which you have no control. When Exim starts up the Perl interpreter, it arranges for output from the warn statement to be written to the Exim main log. You can change this by including appropriate Perl magic somewhere in your Perl code. For example, to discard warn output completely, you need this:

$SIG{__WARN__} = sub { };

Whenever a warn is obeyed, the anonymous subroutine is called. In this example, the code for the subroutine is empty, so it does nothing, but you can include any Perl code that you like. The text of the warn message is passed as the first subroutine argument.

<-previousTable of Contentsnext->