Sambar Server Documentation

Mail Routing Rules
Pro Server Only


Overview
The WebMail interface provides a mechanism that allows users to automatically route mail messages to folders or CGI scripts as well as forward and delete mail messages. Routing rules allow wild-card pattern matching based on Subject, To, From, Cc mail headers.

The CGI script execution follows the same mechanism as POST scripts executed from an HTTP server. The CONTENT_LENGTH environment variable is set indicating the mail message content length. In addition, there are several other environment variables set that correspond with the mail message: MBOX, TO, FROM, CC, MIME, REPLYTO, SUBJECT and SPOOLNAME. The following perl script demonstrates how to process a mail message. Note: The original mail file found in mail/mbox/spool is passed to the the CGI script in the SPOOLNAME environment variable.

#
# Perl-based Mail CGI Script
#
# Copyright 2001 Tod Sambar
# All rights reserved.
#


#
# PARSE THE CGI FORM
#
	$content_len = $ENV{'CONTENT_LENGTH'};
	$mbox = $ENV{'MBOX'};
	$to = $ENV{'TO'};
	$from = $ENV{'FROM'};
	$cc = $ENV{'CC'};
	$mime = $ENV{'MIME'};
	$replyto = $ENV{'REPLYTO'};
	$subject = $ENV{'SUBJECT'};

#
# Write out the mail details to the file
#
	$filename = "mail.tmp";
	open(FILE, ">$filename") || exit(1);
	binmode FILE;

	print FILE "CONTENT_LENGTH ".$content_len."\n";
	print FILE "MBOX ".$mbox."\n";
	print FILE "TO ".$to."\n";
	print FILE "FROM ".$from."\n";
	print FILE "CC ".$cc."\n";
	print FILE "MIME ".$mime."\n";
	print FILE "REPLYTO ".$replyto."\n";
	print FILE "SUBJECT ".$subject."\n";
	print FILE "\n";
	close FILE;

#
# DONE
#
exit(0);

Router CGI Script
In addition to CGI script execution during the delivery of individual messages, system administrators can access the mail message prior to delivery to any mailbox (or outgoing mail delivery), by providing a script for the Router CGI Script in the config/mail.ini configuration file. This CGI script will be passed the SPOOLNAME environment variable, but no content will be passed to STDIN. Should the administrator wish to delete the spool file so it is not delivered to any users (external or internal), the CGI script can remove the file and the server will continue to the next mail message. Otherwise, the mail spool file can be logged, scanned or re-written as desired by the system administrator.

A default bin/mailcgi.pl Router CGI Script is included which utilizes SpamAssasin to analyze mail messages and route them accordingly.

The following example illustrates a simple Router CGI Script that logs the spool file to a temporary file and then deletes the spool file. Important! This will result in no mail being delivered to either internal or external users (due to the unlink() call).

#
# Perl-based Mailbox Router CGI
#
# Copyright 2002 Tod Sambar
# All rights reserved.
#


# MAIL SPOOLNAME
my $spoolname = $ENV{'SPOOLNAME'};

#
# Log the spool file name
#
$filename = "router.tmp";
open(FILE, ">>$filename") || exit(1);
binmode FILE;

print FILE "SPOOLNAME ".$spoolname."\n";
close FILE;

#
# DELETE the mail message (no delivery to any user)
#
unlink($spoolname);

# DONE
exit(0);


© 2001-2002 Sambar Technologies. All rights reserved. Terms of Use.