#============================================================
# MyMail
#============================================================
package MyMail;
use Common;
@ISA = qw(Common);
use strict;
use Jcode;
use Net::SMTP;
use Mail::Mailer;
use MIME::Base64;
use MIME::Lite;
my $Domain = "ac.jp";
#============================================================
# 変数等取得関数
#============================================================
sub SMTPServer { my($this)=@_; return $this->{SMTPServer}; }
sub SetSMTPServer { my($this,$s)=@_; return $this->{SMTPServer}=$s; }
sub SendMailMethod { my($this)=@_; return $this->{SendMailMethod}; }
sub SetSendMailMethod { my($this,$m)=@_; return $this->{SendMailMethod}=$m; }
sub MailerName { my($this)=@_; return $this->{MailerName}; }
sub SetMailerName { my($this,$m)=@_; return $this->{MailerName}=$m; }
#============================================================
# コンストラクタ、デストラクタ
#============================================================
BEGIN { }
sub new
{
my ($module, $sendmethod, $smtpserver, $mailername) = @_;
my $this = {};
bless $this;
$this->SetSendMailMethod($sendmethod);
$this->SetSMTPServer($smtpserver);
$this->SetMailerName($mailername);
return $this;
}
sub DESTROY
{
my $this = shift;
# $this->SUPER::DESTROY(@_);
}
sub SendMailAttached
{
my ($this, $To, $Cc, $Bcc, $From, $Subject, $Body, $AttachedFile, $charset) = @_;
my $SpecifyFullAttachedPath = 0;
$charset = "iso-8859-1" if($charset eq '');
my $MailMethod = $this->SendMailMethod();
$MailMethod = 'smtp' if($MailMethod eq '');
my $SMTPServer = $this->SMTPServer();
my $MailerName = $this->MailerName();
if(Jcode::getcode($Subject) ne 'ascii') {
Jcode::convert(\$Subject, "jis");
$Subject = '=?ISO-2022-JP?B?' . encode_base64($Subject, '') . '?=';
}
if(Jcode::getcode($From) ne 'ascii') {
Jcode::convert(\$From, "jis");
$Subject = '=?ISO-2022-JP?B?' . encode_base64($From, '') . '?=';
}
if(Jcode::getcode($To) ne 'ascii') {
Jcode::convert(\$To, "jis");
$Subject = '=?ISO-2022-JP?B?' . encode_base64($To, '') . '?=';
}
if(Jcode::getcode($Body) ne 'ascii') {
Jcode::convert(\$Body, 'jis');
}
my $JISAttachedFile = $AttachedFile;
my $code = Jcode::getcode($AttachedFile);
my ($drive, $directory, $filename, $ext, $lastdir, $filebody);
if($code ne 'ascii' and $code ne 'euc') {
Jcode::convert(\$JISAttachedFile, "euc");
($drive, $directory, $filename, $ext, $lastdir, $filebody)
= Deps::SplitFilePath($JISAttachedFile);
Jcode::convert(\$JISAttachedFile, "jis");
}
else {
($drive, $directory, $filename, $ext, $lastdir, $filebody)
= Deps::SplitFilePath($JISAttachedFile);
}
my $Type = "text/plain; charset=\"$charset\"";
$Type = 'multipart/mixed' if($AttachedFile ne '');
my @Array = (
"From" => $From,
"To" => $To,
"Subject" => $Subject,
"Type" => $Type,
"Date" => Utils::BuildDateString(time(), "SMTP+0900"),
"Encoding" => "7bit",
"MIME-Version" => "1.0",
);
if($Cc ne '') {
if(Jcode::getcode($Cc) ne 'ascii') {
Jcode::convert(\$Cc, "jis");
$Subject = '=?ISO-2022-JP?B?' . encode_base64($Cc, '') . '?=';
}
@Array = (@Array,
"Cc" => $Cc
);
}
if($Bcc ne '') {
if(Jcode::getcode($Bcc) ne 'ascii') {
Jcode::convert(\$Bcc, "jis");
$Subject = '=?ISO-2022-JP?B?' . encode_base64($Bcc, '') . '?=';
}
@Array = (@Array,
"Bcc" => $Bcc
);
}
if($AttachedFile eq '') {
@Array = (@Array,
"Data" => $Body
);
}
my $msg = new MIME::Lite(@Array);
$msg->replace("X-Mailer" => $MailerName);
$msg->delete("Content-disposition");
$msg->replace("Content-disposition" => "");
MIME::Lite->field_order('content-type', 'content-disposition',
'content-transfer-encoding', 'mime-version',
'x-mailer', 'message-id',
'subject', 'from', 'to', 'cc');
#multipartの場合
if($AttachedFile ne '') {
$msg->attach(
Type => "text/plain; charset=\"$charset\"",
Data => $Body,
);
my $EncodedFileName = '=?ISO-2022-JP?B?' . encode_base64($filename, '') . '?=';
#print "Filename: $filename
\n";
#print "Encoded Filename: $EncodedFileName
\n";
if($SpecifyFullAttachedPath) {
#パスを指定してファイルも読み込ませる場合
$msg->attach(
Type => 'application/octet-stream',
binmode => 1,
Encoding => 'base64',
Path => $AttachedFile,
Filename => $EncodedFileName,
Disposition => 'attachment',
);
}
else {
#パスを指定しない場合
my $Content = JFile::new->ReadFile($AttachedFile, "b");
$msg->attach(
Type => 'application/octet-stream',
Data => $Content,
Filename => $EncodedFileName,
Disposition => 'attachment',
);
}
# テキスト部の追加
if(1) {
$msg->attach(
# $msg->append(
Type => 'TEXT',
Data => '添付テスト'
);
}
}
#送信方法の指定
MIME::Lite->send('smtp', $SMTPServer, Timeout => 60);
#送信
return $msg->send();
}
sub SendMail
{
my ($this, $to, $cc, $bcc, $from, $subject, $body, $charset) = @_;
$charset = "iso-8859-1" if($charset eq '');
#print "charset: $charset
\n";
my $MailMethod = $this->SendMailMethod();
my $SMTPServer = $this->SMTPServer();
my $MailerName = $this->MailerName();
if($charset ne "iso-8859-1") {
$to = Jcode->new($to)->h2z->jis;
$from = Jcode->new($from)->h2z->jis;
$subject = Jcode->new($subject)->h2z->jis;
$body = Jcode->new($body)->h2z->jis;
$to = Jcode->new($to)->mime_encode;
$from = Jcode->new($from)->mime_encode;
$subject = Jcode->new($subject)->mime_encode;
}
#print "aa: $MailMethod, Server => $SMTPServer
\n";
#print "to: $to
\n";
#print "from: $to
\n";
#print "subject: $subject
\n";
my $mailer = new Mail::Mailer($MailMethod, Server => $SMTPServer);
#print "e [$SMTPServer]
\n";
if(!$mailer) {
print "Error in MyMail::SendMail: Can not open [$SMTPServer].
\n";
return 0;
}
$mailer->open(
{
"MIME-Version" => "1.0",
"Content-Transfer-Encoding" => "7bit",
"Content-Type" => "text/plain; charset=\"$charset\"",
"X-Mailer" => $MailerName,
To => $to,
Cc => $cc,
Bcc => $bcc,
From => $from,
Subject => $subject,
}
);
print $mailer $body;
$mailer->close;
return 1;
}
sub SendMail2
{
my ($this, $to, $cc, $from, $subject, $body, $charset) = @_;
$charset = "iso-8859-1" if($charset eq '');
#print "charset: $charset
\n";
my $SMTPServer = $this->SMTPServer();
my $MailerName = $this->MailerName();
#サーバと接続する
my($server) = new Net::SMTP($SMTPServer, Hello => $Domain);
unless ($server) {
print "Can not connect to the mail server [$SMTPServer]
\n";
return 0;
}
my $recipients;
foreach ($cc) {
next unless $_;
$recipients = $recipients.',' if $recipients;
$recipients = $recipients.$_;
}
#メッセージの送信
$server->mail($from);
$server->to($to);
$server->recipient($recipients);
$server->data();
$server->datasend("Content-Type: text/plain; charset=$charset\n");
$server->datasend("To: $to\n");
$server->datasend("Cc: $cc\n");
# $server->datasend("Bcc: $Arg{Bcc}\n") if ($Arg{Bcc});
$server->datasend("From: $from\n");
$server->datasend("Subject: $subject\n");
#ヘッダの終わり
$server->datasend("\n");
$server->datasend($body);
$server->dataend();
#サーバとの接続をクローズ
$server->quit();
return 1;
}
sub SendMail3
{
my %Arg;
# サーバと接続する
my($server) = new Net::SMTP($Arg{Server}, Hello => $Domain);
unless ($server) {
exitError(qq(メールサーバ[$Arg{Server}]に接続できません。));
}
my $recipients;
foreach ($Arg{Cc}, $Arg{Bcc}) {
next unless $_;
$recipients = $recipients.',' if $recipients;
$recipients = $recipients.$_;
}
# メッセージの送信
$server->mail($Arg{From});
$server->to($Arg{To});
$server->recipient($recipients);
$server->data();
$server->datasend("Content-Type: text/plain; charset=iso-2022-jp\n");
$server->datasend("To: $Arg{To}\n");
$server->datasend("Cc: $Arg{Cc}\n");
$server->datasend("Bcc: $Arg{Bcc}\n") if ($Arg{Bcc});
$server->datasend("From: $Arg{From}\n");
$server->datasend("Subject: $Arg{Subject}\n");
$server->datasend("\n"); # ヘッダの終わり
$server->datasend($Arg{Message});
$server->dataend();
# サーバとの接続をクローズ
$server->quit();
}
1;