<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - Web Application Framework for PHP
 *
 * PHP version 4 (>= 4.3.x) or 5
 *
 * Copyright (C) 2006 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_MailSendmail.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

define('SYL_MAIL_SENDMAIL_PATH', '/usr/lib/sendmail');

/**
 *  Sendmailメール送信クラス
 *
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_MailSendmail.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_MailSendmail extends SyL_Mail
{
    /**
     * Sendmailパス
     *
     * @access private
     * @var string
     */
    var $sendmail_path = SYL_MAIL_SENDMAIL_PATH;

    /**
     * コンストラクタ
     *
     * @access private
     */
    function SyL_MailSendmail()
    {
        parent::SyL_Mail();
    }

    /**
     * Sendmailコマンドパスをセット
     * 
     * @access public
     * @param string Sendmailコマンドパス
     */
    function setSendmailPath($sendmail_path)
    {
        $this->sendmail_path = $sendmail_path;
    }

    /**
     * メール送信前処理
     *
     * @access public
     * @param array メール送信属性
     * @param bool 完全初期化フラグ
     * @return OK: true, NG: false
     */
    function init($attributes=array(), $all=false)
    {
        $attributes = array_change_key_case($attributes, CASE_LOWER);
        parent::init($attributes, $all);
        // Sendmailパス
        if (isset($attributes['sendmail'])) {
            $this->sendmail_path = $attributes['sendmail'];
        }
        return true;
    }

    /**
     * メール送信実行
     *
     * @access public
     * @return OK: null, NG: SyL_Error_Parameterオブジェクト
     */
    function send()
    {
        if (!parent::send()) {
            return false;
        }

        // Path必須
        if ($this->sendmail_path == '') {
            $this->error_message = "[SyL error] Sendmail path not found";
            return false;
        }

        // メールヘッダを含む全文取得
        $data = $this->getHeaderMessage($this->body);

       // リターンパスのセット
       if ($this->headers['MAIL_FROM'] != '') {
           $additional_parameters = ' -f' . $this->headers['MAIL_FROM'];
       } else {
           $additional_parameters = '';
       }

        // メール送信実行 ----------------------------------------------------------

        $pmail = popen($this->sendmail_path . ' -t -i' . $additional_parameters, 'w');
        if (!$pmail) {
            $this->error_message = "[SyL error] Sendmail command execute error";
            return false;
        }
        fputs($pmail, $data);
        pclose($pmail);

        // 初期化
        $this->init();

        return true;
    }
}

?>
