File: /usr/sbin/fwd_as_att
#!/usr/iports/bin/perl
BEGIN {
if(-e '/etc/sudoers'){
my $osversion = `/usr/bin/uname -K`;
chomp $osversion;
if($osversion >= 1300000){
@INC = ('/usr/iports/lib/perl5/5.34','/usr/iports/lib/perl5/5.34/mach','/usr/iports/lib/perl5/site_perl','/usr/iports/lib/perl5/site_perl/mach/5.34','/usr/local/www/apache24/cgi-bin/easytecc4');
} elsif($osversion >= 1200000){
@INC = ('/usr/iports/lib/perl5/5.26','/usr/iports/lib/perl5/5.26/mach','/usr/iports/lib/perl5/site_perl','/usr/iports/lib/perl5/site_perl/mach/5.26','/usr/local/www/apache24/cgi-bin/easytecc4');
} elsif($osversion >= 1003000){
@INC = ('/usr/iports/lib/perl5/5.24','/usr/iports/lib/perl5/5.24/mach','/usr/iports/lib/perl5/site_perl','/usr/iports/lib/perl5/site_perl/mach/5.24','/usr/local/www/apache24/cgi-bin/easytecc4');
} else {
@INC = ('/usr/iports/lib/perl5/5.20','/usr/iports/lib/perl5/5.20/mach','/usr/iports/lib/perl5/site_perl','/usr/iports/lib/perl5/site_perl/mach/5.20','/usr/local/www/apache24/cgi-bin/easytecc4');
}
} else {
push @INC, '/home/httpd/cgi-bin/easytecc4';
}
}
use warnings;
use strict;
use Encode qw(encode decode);
use File::Temp qw(tempfile);
use MIME::Lite;
my $from;
my $subject;
my $message;
my $template = shift @ARGV || '';
my $fh;
my $line;
# read from, subject and message from template
if($template =~ m/^\/usr\/local\/etc\/easytecc\/[^\/]+\.fwdmsg$/ && open($fh, '<', $template)){
$message = '';
while($line = <$fh>){
chomp($line);
if($line =~ m/^From:\s*(.+)$/ && !length($from)){
$from = $1;
}
elsif($line =~ m/^Subject:\s*(.+)$/ && !length($subject)){
$subject = $1;
}
# ignore leading blank lines
elsif(length($message) || length($line)){
$message .= $line . "\n";
}
}
close $fh;
if($message =~ m/^\s*$/s){
$message = undef;
}
}
# no template but email given
elsif($template =~ m/^.+\@.+$/){
# assume email
unshift @ARGV, $template;
}
if(!@ARGV){
die "missing to";
}
# defaults
$from = $from || "%%TO%%";
$subject = $subject || "%%SUBJECT%%";
$message = $message || "Dies ist eine weitergeleitete Nachricht\nvon: %%FROM%%\nan: %%TO%%\nBetreff: %%SUBJECT%%\n\nDie Email befindet sich im Anhang.\n";
my $org_from = '';
my $org_to = '';
my $org_subject = '';
my $org_apparently_to = '';
$fh = \*STDIN;
# we need to parse the header
if(($from . $subject . $message) =~ m/%%[A-Z]+%%/){
my $buffer = '';
my %header = (
'From' => '',
'To' => '',
'Subject' => '',
'Apparently-To' => ''
);
while($line = <$fh>){
$buffer .= $line;
# end of header
if(!length($line)){
last;
}
# save header
my $field;
foreach $field (keys %header){
if(!length($header{$field}) && $line =~ m/^$field:(.+)$/i){
$header{$field} = decode('MIME-HEADER',$1);
}
}
# fields found
if(length($header{'From'}) && length($header{'To'}) && length($header{'Subject'})){
last;
}
}
# copy to
if(!length($header{'To'}) && length($header{'Apparently-To'})){
$header{'To'} = $header{'Apparently-To'};
}
# fill template
delete $header{'Apparently-To'};
$message = decode('UTF-8',$message);
my $field;
foreach $field (keys %header){
my $value = decode('MIME-HEADER',$header{$field});
$from =~ s/%%$field%%/$value/i;
$subject =~ s/%%$field%%/$value/i;
$message =~ s/%%$field%%/$value/i;
}
$from = encode('MIME-HEADER',$from);
$subject = encode('MIME-HEADER',$subject);
$message = encode('UTF-8',$message);
$fh = tempfile();
print $fh $buffer;
$/ = undef;
print $fh <STDIN>;
seek $fh, 0, 0;
}
# prepare attachment
my $attachment = MIME::Lite->new(Type => 'message/rfc822', Disposition => 'attachment', Encoding => '8bit', FH => $fh, ReadNow => '1');
$attachment->attr('Content-Description' => 'email.eml');
my $mail = MIME::Lite->new(Type => 'text/plain; charset=UTF-8', Encoding => '8bit', From => $from, Subject => $subject, Data => $message);
$mail->delete('X-Mailer');
$mail->attach($attachment);
my $to;
foreach $to (@ARGV){
$mail->delete('To');
$mail->add('To' => $to);
$mail->send;
}