Skip to content

Instantly share code, notes, and snippets.

@TinoDidriksen
Created January 18, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TinoDidriksen/6b1b17548022dafb0573af75665ba95e to your computer and use it in GitHub Desktop.
Save TinoDidriksen/6b1b17548022dafb0573af75665ba95e to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*-
use bytes;
use warnings;
use strict;
use Carp qw(verbose);
use Getopt::Long;
use Sendmail::Milter 0.18 qw(:all);
my $rx = '\b(.+\@your\.domain\.com)\b';
my %cbs;
my %tos;
my $headers = '';
my $body = '';
$cbs{envrcpt} = sub {
my $ctx = shift;
foreach my $to (@_) {
foreach my $t ($to =~ m/$rx/g) {
chomp($t);
print "$$: To: $t\n";
$tos{$t} = 1;
}
}
SMFIS_CONTINUE;
};
$cbs{header} = sub {
if (!%tos) {
print "$$: No $rx - passing\n";
return SMFIS_ACCEPT;
}
print "$$: Collecting headers\n";
my $ctx = shift;
my @args = @_;
for(my $ctr=0;$ctr<=$#args;$ctr++) {
$headers .= $args[$ctr++];
$headers .= ": ".$args[$ctr]."\n";
}
SMFIS_CONTINUE;
};
$cbs{body} = sub {
print "$$: Collecting body\n";
my $ctx = shift;
foreach my $line (@_) {
chomp($line);
$body .= $line;
$body .= "\n";
}
SMFIS_CONTINUE;
};
$cbs{eom} = sub {
print "$$: End of mail\n";
my $ctx = shift;
open(FH, ">/tmp/mail.$$.milter") or die $!;
print FH $headers;
print FH "BCC: ".join(', ', keys(%tos))."\n";
print FH "\n";
print FH $body;
close(FH);
print "$$: Calling handler.php ...\n";
chdir('/tmp/');
`nohup handler.php </tmp/mail.$$.milter >/tmp/mail.$$.log 2>&1 &`;
print "$$: Discarding original\n";
SMFIS_DISCARD;
};
Sendmail::Milter::auto_setconn('translate');
Sendmail::Milter::register('translate', \%cbs, SMFI_CURR_ACTS);
Sendmail::Milter::main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment