<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * メール送信基本クラス
 * tyamamoto
 */
require_once('Mail.php');
class commonMail {

	/*
	 * メール送信処理
	 * @param $from:送信者
	 * @param $body:メール本文
	 * @param $subject:メール件名
	 * @param $to:送信先(Strings or Array)
	 * @return boolean
	 */
	function sendMail($to, $subject=null,$msg=null, $from) {
		if(!$from || !$to) {
			return false;
		}

		//設定
		$enc = "utf-8";
		mb_internal_encoding($enc);
		mb_language("Japanese");
		$params["sendmail_path"] = "/usr/sbin/sendmail";

		$header["From"] = mb_convert_encoding($from, "JIS", $enc);
		$subject = str_replace("\r\n", "\r", $subject);
		$subject = str_replace("\\n", "\r", $subject);
		$header["Subject"] = mb_encode_mimeheader($subject);

		$msg = str_replace("\r\n", "\n", $msg);
		$msg = str_replace("\\n", "\n", $msg);
		$msg = mb_convert_encoding($msg, "JIS", $enc);

		$mailobj =& Mail::factory("sendmail", $params);

		if($to!=null&&is_array($to)) {
			foreach($to as $val) {
				$header["To"] = $val;
				$recipients = $val;
				$return = $mailobj->send($recipients, $header,$msg);
				if(PEAR::isError($return)) {
					return false;
				}
			}
			return true;
		} else if($to!=null) {
			$header["To"] = $to;
			$recipients = $to;
			$return = $mailobj->send($recipients, $header,$msg);
			if(PEAR::isError($return)) {
				return false;
			}
			return true;
		} else {
			return false;
		}
	}
}



?>