#############################################################
#  LeoBoard ver.5000 / LB5000 / 雷傲超級論壇 ver.5000
#
#  版權所有: 雷傲工作室(原藍寶石軟體工作室)
#
#  製作人  : 山鷹糊 (Shining Hu)
#            花無缺 (Ifairy Han)
#           
#  網站位址: http://www.CGIer.com/      CGI 編程者之家
#	        http://www.LeoBoard.com/   雷傲論壇支援網站
#	        http://www.leoBBS.com/     本論壇直通車
#            
#############################################################

if ($emailtype eq "smtp_mail") {
    if ($SMTP_SERVER ne "") {
        require "SendMail.pm.pl";
        eval("use MIME::Base64;");
        eval("use MIME::QuotedPrint;");
    }
    else { $emailfunctions = "off"; }
}
elsif ($emailtype eq "esmtp_mail") { 
    if (($SMTP_SERVER eq "")||($SMTPUSER eq "")) {
        $emailfunctions = "off";
    }
}
elsif (($emailtype eq "send_mail")&&($SEND_MAIL eq "")) { $emailfunctions = "off"; }

sub sendmail {
    my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;
    $smtp =~ s/^\s+//g;
    $smtp =~ s/\s+$//g;
    $SMTP_PORT = 25 if (($SMTP_PORT !~ /^\d+$/)||($SMTP_PORT eq ""));
    $to =~ s/\\//isg;
    $to =~ s/[ \a\f\e\0\r\t]+//isg;
    $to =~ s/\,$//isg;
    $to =~ s/\,/\, /isg;
#    $to =~ s/\@/\\\@/g;

    if ($to ne "") {
	$fromaddr =~ s/.*<([^\s]*?)>/$1/;
	$replyaddr =~ s/.*<([^\s]*?)>/$1/;
	$replyaddr =~ s/^([^\s]+).*/$1/;
	$message.= "\n=============================================\n";
	$message.= "LB5000 論壇　技術支援：http://www.cgier.com/\n";
	$message.= "LB5000 論壇　技術範例：http://www.leoBBS.com/\n";
   	$message.= "此郵件由雷傲超級論壇（LeoBoard.com）傳送... \n";
   	
	if ($emailtype eq "blat_mail") {
	    &blat_mail($to, $fromaddr,$replyaddr, $subject, $message);
	}
	elsif ($emailtype eq "send_mail") {
	    if ($SEND_MAIL ne "") {
		&send_mail($to, $fromaddr,$replyaddr, $subject, $message);
	    }
	}
	elsif ($emailtype eq "smtp_mail") {
	    if (($smtp ne "")&&($smtp !~ /localhost/i)) {
		&smtp_mail($to, $fromaddr,$replyaddr, $subject, $message, $smtp);
	    }
	}
	elsif ($emailtype eq "esmtp_mail") {
	    if (($smtp ne "")&&($smtp !~ /localhost/i)&&($SMTPUSER ne "")) {
	        $message =~ s/\n/\r/isg;
		&smtpmail($to, $fromaddr,$replyaddr, $subject, $message, $smtp);
	    }
	}
    }
}

sub send_mail {
    my ($to, $fromaddr,$replyaddr, $subject, $message)=@_;
    my @to1 = split (/\, /,$to);
    foreach $to (@to1) {
	chomp $to;
	next if ($to eq "");
	open (MAIL,"| $SEND_MAIL -t");
	print MAIL qq~To: $to\n~;
	print MAIL qq~From: $fromaddr\n~;
	print MAIL qq~Reply-to: $replyaddr\n~ if $replyaddr;
	print MAIL "X-Mailer: LeoBoard Sendmail Mail Sender!\n";
	print MAIL "Subject: $subject\n\n";
	print MAIL "$message";
	print MAIL "\n.";
	close(MAIL);
    }
}
sub smtp_mail {
    my ($address, $from,$replyaddr,$subject, $body,$smtp) = @_;
    $body=~s/\n/<br>/isg;
    my(@recipient) = split (/\, /,$address);
    my $renum = @recipient;
    my($sender)   = $from;
    my($subject)  = $subject;
    my($recipient) = pop(@recipient);
    my($sm) = new SendMail($smtp, $SMTP_PORT);
    $sm->From($sender);
    $sm->To($recipient);
    $sm->Bcc(@recipient) if ($renum > 0);
    $sm->ReplyTo($replyaddr);
    $sm->Subject($subject);
    $sm->setMailHeader("X-Mailer", "LeoboardMailer http://www.leoboard.com");
    $sm->setMailHeader("Content-Type","text/html; charset=\"big5\"");
    $sm->setMailBody($body);
    if ($sm->sendMail() != 0) {
       return(1);
    }
    else { return(0); }
}
   
sub blat_mail {
    my ($to, $fromaddr,$replyaddr, $subject, $message)=@_;
    my @to1 = split (/\, /,$to);
    $tempfile = "$lbdir" . "tempfile.txt";
    open(FILE,">$tempfile");
    print FILE "$message";
    close(FILE);
    foreach $to (@to1) {
	chomp $to;
	next if ($to eq "");
        open(MAIL,"|blat $tempfile -t \'$to\' -b \'$bccinfo\' -i \'$fromaddr\' -f \'$fromaddr\' -s \"$subject\"");
        close(MAIL);
    }
    unlink ("$tempfile");
}

sub smtpmail {
    use Socket;
    my ($address, $from,$replyaddr, $subject, $body,$smtp) = @_;
    $smtp =~ s/^\s+//g;
    $smtp =~ s/\s+$//g;
    $SMTP_PORT = 25 if (($SMTP_PORT !~ /^\d+$/)||($SMTP_PORT eq ""));
    
    my ($a, $i, $name, $aliases, $proto, $type, $len, $thataddr);
    my @to = split(/, /, $address);
foreach $i (@to) {

    #對位址進行解碼
    my $AF_INET = 2;
    my $SOCK_STREAM = 1;
    my $SOCKADDR = 'S n a4 x8';

    ($name, $aliases, $proto) = getprotobyname('tcp');
    ($name, $aliases, $SMTP_PORT) = getservbyname($SMTP_PORT, 'tcp') unless ($SMTP_PORT =~ /^\d+$/);
    ($name, $aliases, $type, $len, $thataddr) = gethostbyname($smtp);
    my $this = pack($SOCKADDR, $AF_INET, 0, $thisaddr);
    my $that = pack($SOCKADDR, $AF_INET, $SMTP_PORT, $thataddr);

    #開啟ESMTP的socket連接埠
    socket(S, $AF_INET, $SOCK_STREAM, $proto);
    bind(S, $this);
    connect(S, $that);

    select(S);
    $| = 1;
    select(STDOUT);
    $a = <S>;
    if ($a !~ /^2/)
    {
        close(S);
        undef $|;
        return 0;
    }

    #進行ESMTP身份驗證
    print S "EHLO localhost\n";
    $a = <S>;
    print S "AUTH LOGIN\n";
    $a = <S>;
    my $encode_smtpuser = Base64encode($SMTPUSER);    #用來驗證的會員名稱必須經過Base64編碼後發往伺服器
    print S "$encode_smtpuser\n";
    $a = <S>;
    my $encode_smtppass = Base64encode($SMTPPASS);    #用來驗證的密碼必須經過Base64編碼後發往伺服器
    print S "$encode_smtppass\n";
    $a = <S>;

    #傳送郵件頭部資料
    print S qq~MAIL FROM: $from\n~;
    $a = <S>;
    print S "RCPT TO: $i\n";
    $a = <S>;

    #傳送郵件本文
    print S "DATA\n";
    print S "From: $from\n";
    print S "To: $i\n";
    print S "Subject: $subject\n";
    print S "Reply-To: $replyaddr\n" if ($replyaddr);
    print S "X-Mailer: Leoboard Mailer http://www.leoboard.com\n";
    print S "Content-Type: text/html;charset=big5\n";
    print S $body;
    print S "\n";
    print S "\n\n";
    print S ".\n";
    $a = <S>;

    print S "QUIT\n";
    $a = <S>;
    close(S);
    undef $|;
}
    return 1;
}

sub Base64encode    #Base64編碼函數
{
    my $res = "";
    while ($_[0] =~ /(.{1,45})/gs)
    {
        $res .= substr(pack('u', $1), 1);
        chop($res);
    }
    $res =~ tr| -_|A-Za-z0-9+/|;
    my $padding = (3 - length($_[0]) % 3) % 3;
    $res =~ s/.{$padding}$/'=' x $padding/e if ($padding);
    return $res;
}

1;
