#============================================================
# MyMailApplication
#============================================================
package MyMailApplication;
use MyApplication;
@ISA = qw(MyApplication);
use strict;
use MyMail;
use Mail::MailFile;
#==========================================
# 大域変数
#==========================================
my $DefaultSendmail = '/usr/sbin/sendmail';
my $SourceCharCode = Jcode::getcode('月火水木金土日 ');
#============================================================
# 静的関数
#============================================================
#============================================================
# 変数等取得関数
#============================================================
#============================================================
# コンストラクタ、デストラクタ
#============================================================
BEGIN { }
sub new
{
my ($module, $app) = @_;
my $this = {};
bless $this;
# $this->SUPER::new(@_);
return $this;
}
sub DESTROY
{
my $this = shift;
# $this->SUPER::DESTROY(@_);
}
#============================================================
# 継承クラスで定義しなおす関数
#============================================================
#============================================================
# 一般関数
#============================================================
sub IsValidEMailAddress {
my ($App, $EMail) = @_;
Utils::IsValidEMailAddress($EMail, 1, 1);
}
sub CheckSendmail
{
my ($this, $sendmail) = @_;
return 1 if(!$this->pParams()->{SendMail});
return $this->{SendmailChecked} if(defined $this->{SendmailChecked});
$sendmail = $this->Sendmail() if(!defined $sendmail);
$sendmail =~ s/\s.*$//;
if(!-e $sendmail) {
$this->{SendmailChecked} = 0;
if($sendmail =~ /^smtp:/i) {
return 1;
}
else {
$this->H3("Error!!: [$sendmail] does not exist.\n");
return 0;
}
}
$this->{SendmailChecked} = 1;
return 1;
}
sub PrintMail
{
my ($this, $text) = @_;
$this->HR();
$this->mlPrintRawHTML('en' => "Mail that has been sent:
",
'jp' => "送信するメール:
",
);
$this->print("$text\n");
$this->HR();
}
sub SendMail
{
my ($this, $text, $sendmail, $TempPath, $PrintMail) = @_;
return $this->SendMail2($text, $sendmail, $TempPath, $PrintMail);
}
sub SendMail2
{
my ($this, $text, $sendmail, $TempPath, $PrintMail) = @_;
#$PrintMail = 1;
# $this->H1("aa");
my $pParams = $this->pParams();
$PrintMail = $pParams->{PrintMail} if(!defined $PrintMail);
$PrintMail = $pParams->{ShowSendingMail} if(!defined $PrintMail and defined $pParams->{ShowSendingMail});
$TempPath = $pParams->{TempPath} if(!defined $TempPath);
my $DontSendMail = (defined $pParams->{DontSendMail})? $pParams->{DontSendMail} : 0;
#$this->H1("PM[$PrintMail][$TempPath][$DontSendMail]");
#$this->print("s1: [$sendmail] printmail[$PrintMail]\n");
$sendmail = $this->Sendmail() if(!defined $sendmail);
#$this->print("s2: [$sendmail]\n");
if(!$this->CheckSendmail($sendmail)) {
$this->H2("Error in SendMail: Can not find sendmail [$sendmail].");
return -1;
}
#$this->print("S: $sendmail, $TempPath\n");
#$sendmail = "smtp:localhost";
Jcode::convert(\$text, 'iso-2022-jp');
if($sendmail =~ /^smtp:\s*(.*)\s*$/i) {
my $SMTPServer = $1;
#$this->print("sendmail: $sendmail : $SMTPServer\n");
my $m = new MailFile;
my @text = split(/[\r\n]/, $text);
my ($pHeader, $pBody) = $m->SplitHeaderFromString(\@text, "jis", 0);
my $header = join("\n", @$pHeader);
my $Body = join("\n", @$pBody);
my $From = $m->GetHeaderFromString('From', 'jis', 0, \@text);
my $To = $m->GetHeaderFromString('To', 'jis', 0, \@text);
my $Cc = $m->GetHeaderFromString('Cc', 'jis', 0, \@text);
my $Bcc = $m->GetHeaderFromString('Bcc', 'jis', 0, \@text);
my $Subject = $m->GetHeaderFromString('Subject', 'jis', 0, \@text);
my $ContentType = $m->GetHeaderFromString('Content-Type', 'jis', 0, \@text);
my $charset;
if($ContentType =~ /charset\s*=\s*([\"\w\-]*)/si) {
$charset = lc $1;
Utils::DelQuote($charset);
}
if($PrintMail) {
$this->print("sendmail: $sendmail : $SMTPServer\n");
$this->print("Charset: $charset\n");
$this->print("Server: $SMTPServer
\n");
$this->print("Subject: $Subject\n");
$this->print("From: $From\n");
$this->print("To: $To\n");
$this->print("Cc: $Cc\n");
$this->print("ContentType: $ContentType\n");
$this->print("Body: $Body\n");
}
return 1 if($DontSendMail);
my $mail = new MyMail('smtp', $SMTPServer, 'Perl Mail 1.0');
if($mail->SendMail($To, $Cc, $Bcc, $From, $Subject, $Body, $charset)) {
return 1;
}
else {
$this->H2("Error in MyMailApplication::SendMail: Can not send the mail to [$To] via [$SMTPServer].");
return -1;
}
}
elsif($sendmail =~ /smail/i) {
#$this->print("s: $sendmail
\n");
#$this->print("TempPath[$TempPath]\n");
#$this->print("t: $text
\n");
if($TempPath eq '') {
$this->print("Error in MyMailApplication::SendMail: TempPath should be specified for \$sendmail='smail'
\n");
return;
}
my $m = new MailFile;
my @text = split(/[\r\n]/, $text);
my ($pHeader, $pBody) = $m->SplitHeaderFromString(\@text, "jis", 0);
my $ContentType = $m->GetHeaderFromString('Content-Type', 'jis', 0, \@text);
my $charset;
if($ContentType =~ /charset\s*=\s*([\"\w\-]*)/si) {
$charset = lc $1;
Utils::DelQuote($charset);
}
my $header = join("\n", @$pHeader);
my %Hash;
$Hash{Body} = join("\n", @$pBody);
$Hash{From} = $m->GetHeaderFromString('From', 'jis', 0, \@text);
$Hash{From} =~ s/\(.*?\)//;
#$this->print("From[$Hash{From}]\n");
$Hash{To} = $m->GetHeaderFromString('To', 'jis', 0, \@text);
$Hash{Cc} = $m->GetHeaderFromString('Cc', 'jis', 0, \@text);
$Hash{Bcc} = $m->GetHeaderFromString('Bcc', 'jis', 0, \@text);
$Hash{Subject} = $m->GetHeaderFromString('Subject', 'jis', 0, \@text);
my @cc = split(/\s*,\s*/, $Hash{Cc});
for(my $i = 0 ; $i < @cc ; $i++) {
if($Hash{To} eq '') {
$Hash{To} = "CC:$cc[$i]";
}
else {
$Hash{To} .= ",CC:$cc[$i]";
}
}
my @bcc = split(/\s*,\s*/, $Hash{Bcc});
for(my $i = 0 ; $i < @bcc ; $i++) {
if($Hash{To} eq '') {
$Hash{To} = "BCC:$bcc[$i]";
}
else {
$Hash{To} .= ",BCC:$bcc[$i]";
}
}
#$Hash{Subject} = "test8";
#$Hash{To} = "tkamiya\@msl.titech.ac.jp";
#$Hash{From} = "tkamiya\@lucid.msl.titech.ac.jp";
#$Hash{Body} = "test";
if($charset =~ /iso-2022-jp/i) {
Jcode::convert(\$Hash{Body}, "jis");
}
$Hash{FilePath} = $TempPath; #"d:/temp/a.txt";
my $ToBeDeleted = 0;
if(-d $Hash{FilePath}) {
$Hash{FilePath} = Utils::MakePath($Hash{FilePath}, "mail.temp", '/', 0);
}
if(!-e $Hash{FilePath}) {
$ToBeDeleted = 1;
}
elsif($Hash{FilePath} =~ /\.txt/i) {
for(my $i = 0 ; $i < 100 ; $i++) {
my $s = $Hash{FilePath};
$s =~ s/\.txt/$i.txt/i;
if(!-e $s) {
$Hash{FilePath} = $s;
$ToBeDeleted = 1;
last;
}
}
}
open(OUT, ">$Hash{FilePath}");
print OUT $Hash{Body};
close(OUT);
my $T = new Template;
$Hash{From} = Utils::DelSpace($Hash{From});
$Hash{FilePath} =~ s/\//\\/g if($^O eq 'MSWin32');
my $cmd = $T->ReplaceByHash($sendmail, \%Hash, '{', '}');
#$this->print("MailFilePath[$Hash{FilePath}]\n");
#$this->print("cmd[$cmd]\n");
if($PrintMail) {
#if(1) {
$this->print("command : [$cmd]
\n");
$this->print("DontSendMail: [$DontSendMail]
\n");
$this->print("sendmail: [$sendmail]
\n");
$this->print("FilePath: [$Hash{FilePath}]\n");
$this->print("Subject : [$Hash{Subject}]
\n");
$this->print("From : [$Hash{From}]
\n");
$this->print("To : [$Hash{To}]
\n");
$this->print("Cc : [$Hash{Cc}]
\n");
$this->print("Bcc : [$Hash{Bcc}]
\n");
$this->print("Body: [$Hash{Body}]
\n");
}
return 1 if($DontSendMail);
#$this->print("cmd[$cmd]\n");
# system($cmd);
my $ret = Utils::ExecuteWithPipe($cmd, 'sjis', $pParams->{WebCharCode}, 0, 1, 0, $this);
unlink($Hash{FilePath}) if($ToBeDeleted);
}
else {
#$this->print("sendmail: $sendmail : $PrintMail\n");
my $m = new MailFile;
my @text = split(/[\r\n]/, $text);
my ($pHeader, $pBody) = $m->SplitHeaderFromString(\@text, "jis", 0);
my $ContentType = $m->GetHeaderFromString('Content-Type', 'jis', 0, \@text);
my $charset;
if($ContentType =~ /charset\s*=\s*([\"\w\-]*)/si) {
$charset = lc $1;
Utils::DelQuote($charset);
}
if($charset =~ /iso-2022-jp/i) {
Jcode::convert(\$text, "jis");
}
my $command = "| $sendmail -t ";
if($PrintMail) {
#if(1) {
$this->print("command: $command
\n");
$this->print("DontSendMail: [$DontSendMail]
\n");
$this->print("sendmail: $sendmail
\n");
$this->print("ContentType: $ContentType
\n");
$this->print("charset: $charset
\n");
$this->print("body: $text
\n");
}
return 1 if($DontSendMail);
if(!open(OUTFILE, $command)) {
$this->H2("Error in SendMail: Can not use sendmail [$command].");
return -1;
}
print OUTFILE $text;
close(OUTFILE);
}
return 1;
}
1;