sendmail ハック

/usr/sbin/sendmail と /usr/lib/sendmail にこのプログラムを置くと、sendmailqmail がインストールされていなくてもメールが送れちゃいます。日本語もオーケーです。'mail.hogehoge.com' の部分は適宜変更してください。

#!/usr/bin/perl -w

use strict;
use Email::Send;
use Email::MIME;
use Email::MIME::Creator;
use Jcode;

&main();

sub email {
    my $email = $_[1] ? {@_} : $_[0];
    my(%tmp) = @{$email->{header}};
    foreach (keys %tmp){
        $tmp{$_} = Jcode::convert($tmp{$_},'jis','euc');
    }
    $email->{body} = Jcode::convert($email->{body},'jis','euc');
    $email->{header} =[%tmp];
    $email = Email::MIME->create(%{$email});
    # SMTP server address
    my @args = qw(SMTP mail.hogehoge.com);
    my $class;
    unless ( $class = shift @args ) {
        $class = 'SMTP';
        unshift @args, 'localhost';
    }
    send $class => $email, @args;
}

sub main {
    my $hash;
    
    foreach my $line (<STDIN>) {
        if ($line =~ /^To: /) {
            $line =~ s/^To: //g;
            chomp $line;
            $hash->{To} = $line;
        } elsif ($line =~ /^From: /) {
            $line =~ s/^From: //g;
            chomp $line;
            $hash->{From} = $line;
        } elsif ($line =~ /^Subject: /) {
            $line =~ s/^Subject: //g;
            chomp $line;
            $hash->{Subject} = $line;
        } elsif ($line =~ /^In-Reply-To: /) {
            # do nothing
        } elsif ($line =~ /^\n$/ && !exists $hash->{body}) {
            $hash->{body} = '';
        } elsif (exists $hash->{body}) {
            $hash->{body} .= $line;
        }
    }
    
    die "Error: null field 'To'" unless exists $hash->{To};
    die "Error: null field 'From'" unless exists $hash->{From};
    die "Error: null field 'Subject'" unless exists $hash->{Subject};
    die "Error: null field 'body'" unless exists $hash->{body};
    
    &email(
        header => [
            From    => $hash->{From},
            To      => $hash->{To},
            Subject => $hash->{Subject}
        ],
        body => $hash->{body}
    );
}