use MIME::Base64;

sub S_Mail{
my($to,$from,$fromname,$mailbody,$act,$ref,$g_l,$i);
my($amount,$price,$name,$num,$op,@op,$opline,$page,$sub,$pt,$image,$weight,$pack,$stock);
my($odrID,$a_total,$subtotal,$tax,$total,$date,$time,$ctref);
$to=$_[0];
$from=$_[1];
$fromname=$_[2];
$sub=$_[3];
$mailbody=$_[4];
$act=$_[5];

$ref=$_[6];
$odrID=$_[7];
$a_total=$_[8];
$subtotal=$_[9];
$tax=$_[10];
$total=$_[11];
$date=$_[12];
$time=$_[13];

$fromname=&MakeFromName($fromname,$from);
if($sub){
	$sub=&MakeSub($sub);
}
$mailbody=&MakeMailbody($mailbody);

if(!open(MAIL, "| $CF::sendmail -t -i")){
	&error("メール送信できません。sendmailのパス等を確認してください。");
}
print MAIL "To: $to\n";
print MAIL "From: $fromname\n";
print MAIL "Subject: $sub\n";
print MAIL "MIME-Version: 1.0\n";
print MAIL "X-Mailer: mailer\n";
if($act eq "shop" && $CF::csv){
	print MAIL "Content-Type: multipart/mixed; boundary=\"kugiri\"\n\n";
	print MAIL "--kugiri\n";
}
print MAIL "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n";
print MAIL "Content-Transfer-Encoding: 7bit\n\n";
print MAIL "$mailbody\n";
if($act eq "shop" && $CF::csv){
	$csvfile=$CF::csvfile;
	print MAIL "--kugiri\n";
	print MAIL "Content-Type: application/octet-stream; name=\"${csvfile}.csv\"\n";
	print MAIL "Content-Transfer-Encoding: base64\n";
	print MAIL "Content-Disposition: attachment; filename=\"${csvfile}.csv\"\n\n";
	$g_l="";
	$i=0;
	foreach(@{$ref}){
		$i++;
		if($i==1){
			$g_l.="$odrID,$FORM{'name'},$FORM{'kana'},$FORM{'postal'},$FORM{'pref'},$FORM{'address'},$FORM{'address2'},$FORM{'tel'},$FORM{'email'},$date,$time,$FORM{'pay'},$FORM{'deliver'},";
		}
		else{
			$g_l.=",,,,,,,,,,,,,";
		}
		$ctref=&Functions::SplitCart($_);
		$op=${$ctref}{'op'};
		$op=~s/:/ /g;
		if($op){
			${$ctref}{'name'}.=" ".'('.$op.')';
		}
		$g_l.="${$ctref}{'number'},${$ctref}{'name'},${$ctref}{'price'},${$ctref}{'amount'},";
		if($i==1){
			$g_l.="$a_total,$subtotal,$tax,$total\n";
		}
		else{
			$g_l.=",,,\n";
		}
	}

	$g_l = encode_base64($g_l);
	print MAIL "$g_l\n";
	print MAIL "\n--kugiri--\n";
}
close(MAIL);

}

sub MakeFromName{
my($fromname,$from);
$fromname=$_[0];
$from=$_[1];

if($fromname){
	$fromname=encode('jis',decode('utf8',$fromname));
	$fromname=&mail64encode($fromname);
	$fromname="$fromname <$from>";
}
else{
	$fromname=$from;
}

return $fromname;

}

sub MakeSub{
my($sub);
$sub=$_[0];
$sub=encode('jis',decode('utf8',$sub));
$sub=&mail64encode($sub);
return $sub;
}

sub MakeMailbody{
my($mailbody);
$mailbody=$_[0];

$mailbody=encode('jis',decode('utf8',$mailbody),Encode::FB_XMLCREF);

$mailbody=~s/\e\(B&#xff5e;\e\$B/\e\$B\x21\x41/g;
$mailbody=~s/\e\(B&#xff5e;/\e\$B\x21\x41\e\(B/g;
$mailbody=~s/&#xff5e;\e\$B/\e\$B\x21\x41/g;
$mailbody=~s/&#xff5e;/\e\$B\x21\x41\e\(B/g;

$mailbody=~s/\e\(B\\x\{ff5e\}\e\$B/\e\$B\x21\x41/g;
$mailbody=~s/\e\(B\\x\{ff5e\}/\e\$B\x21\x41\e\(B/g;
$mailbody=~s/\\x\{ff5e\}\e\$B/\e\$B\x21\x41/g;
$mailbody=~s/\\x\{ff5e\}/\e\$B\x21\x41\e\(B/g;

$mailbody=~s/\e\(B&#xff0d;\e\$B/\e\$B\x21\x3e/g;
$mailbody=~s/\e\(B&#xff0d;/\e\$B\x21\x3e\e\(B/g;
$mailbody=~s/&#xff0d;\e\$B/\e\$B\x21\x3e/g;
$mailbody=~s/&#xff0d;/\e\$B\x21\x3e\e\(B/g;

return $mailbody;
}


sub mail64encode {
my($tmp_bun) = $_[0];
$tmp_bun = encode_base64($tmp_bun,"");
return("=?ISO-2022-JP?B?${tmp_bun}?=");
}



1;
