source: branches/feature-module-update/data/class/SC_SendMail.php @ 16503

Revision 16503, 8.7 KB checked in by nanasess, 17 years ago (diff)

Pear::Mail のメールバックエンド(mail|sendmail|smtp) に対応

Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8require_once(dirname(__FILE__) . '/../module/Mail/Mail.php');
9require_once(dirname(__FILE__) . '/../module/Mail/mime.php');
10
11//--- テキスト/HTML メール送信
12class SC_SendMail {
13
14    var $to;            //  送信先
15    var $subject;       //  題名
16    var $body;          //  本文
17    var $cc;            // CC
18    var $bcc;           // BCC
19    var $replay_to;     // replay_to
20    var $return_path;   // return_path
21    var $arrEncode;
22    var $objMailMime;
23    var $arrTEXTEncode;
24    var $arrHTMLEncode;
25    var $objMail;
26
27    // コンストラクタ
28    function SC_SendMail() {
29        $this->to = "";
30        $this->subject = "";
31        $this->body = "";
32        $this->cc = "";
33        $this->bcc = "";
34        $this->replay_to = "";
35        $this->return_path = "";
36        $this->arrEncode = array();
37        $this->backend = MAIL_BACKEND;
38        $this->host = SMTP_HOST;
39        $this->port = SMTP_PORT;
40        $this->objMailMime = new Mail_mime();
41        mb_language( "Japanese" );
42
43        //-- PEAR::Mailを使ってメール送信オブジェクト作成
44        $this->objMail =& Mail::factory($this->backend,
45                                        $this->getBackendParams($this->backend));
46    }
47
48    // 宛先の設定
49    function setTo($to, $to_name = "") {
50        $this->to = $this->getNameAddress($to_name, $to);
51    }
52
53    // 送信元の設定
54    function setFrom($from, $from_name = "") {
55        $this->from = $this->getNameAddress($from_name, $from);
56    }
57
58    // CCの設定
59    function setCc($cc, $cc_name = "") {
60        if($cc != "") {
61            $this->cc = $this->getNameAddress($cc_name, $cc);
62        }
63    }
64
65    // BCCの設定
66    function setBCc($bcc) {
67        if($bcc != "") {
68            $this->bcc = $bcc;
69        }
70    }
71
72    // Reply-Toの設定
73    function setReplyTo($reply_to) {
74        if($reply_to != "") {
75            $this->reply_to = $reply_to;
76        }
77    }
78
79    // Return-Pathの設定
80    function setReturnPath($return_path) {
81        $this->return_path = $return_path;
82    }
83
84    // 件名の設定
85    function setSubject($subject) {
86        $this->subject = mb_encode_mimeheader($subject);
87    }
88
89    // 本文の設定
90    function setBody($body) {
91        $this->body = mb_convert_encoding($body, "JIS", CHAR_CODE);
92    }
93
94    // SMTPサーバの設定
95    function setHost($host) {
96        $this->host = $host;
97        $arrHost = array(
98                'host' => $this->host,
99                'port' => $this->port
100        );
101        //-- PEAR::Mailを使ってメール送信オブジェクト作成
102        $this->objMail =& Mail::factory("smtp", $arrHost);
103
104    }
105
106    // SMTPポートの設定
107    function setPort($port) {
108        $this->port = $port;
109        $arrHost = array(
110                'host' => $this->host,
111                'port' => $this->port
112        );
113        //-- PEAR::Mailを使ってメール送信オブジェクト作成
114        $this->objMail =& Mail::factory("smtp", $arrHost);
115    }
116
117    // 名前<メールアドレス>の形式を生成
118    function getNameAddress($name, $mail_address) {
119            if($name != "") {
120                // 制御文字を変換する。
121                $_name = $name;
122                $_name = ereg_replace("<","<", $_name);
123                $_name = ereg_replace(">",">", $_name);
124                if(OS_TYPE != 'WIN') {
125                    // windowsでは文字化けするので使用しない。
126                    $_name = mb_convert_encoding($_name,"JIS",CHAR_CODE);
127                }
128                $_name = mb_encode_mimeheader($_name);
129                $name_address = "\"". $_name . "\"<" . $mail_address . ">";
130            } else {
131                $name_address = $mail_address;
132            }
133            return $name_address;
134    }
135
136    function setItem( $to, $subject, $body, $fromaddress, $from_name, $reply_to="", $return_path="", $errors_to="", $bcc="", $cc ="" ) {
137        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
138    }
139
140    function setItemHtml( $to, $subject, $body, $fromaddress, $from_name, $reply_to="", $return_path="", $errors_to="", $bcc="", $cc ="" ) {
141        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
142    }
143
144    /*  ヘッダ等を格納
145         $to            -> 送信先メールアドレス
146         $subject       -> メールのタイトル
147         $body          -> メール本文
148         $fromaddress   -> 送信元のメールアドレス
149         $header        -> ヘッダー
150         $from_name     -> 送信元の名前(全角OK)
151         $reply_to      -> reply_to設定
152         $return_path   -> return-pathアドレス設定(エラーメール返送用)
153         $cc            -> カーボンコピー
154         $bcc           -> ブラインドカーボンコピー
155    */
156    function setBase( $to, $subject, $body, $fromaddress, $from_name, $reply_to="", $return_path="", $errors_to="", $bcc="", $cc ="" ) {
157        // 宛先設定
158        $this->to = $to;
159        // 件名設定
160        $this->setSubject($subject);
161        // 本文設定(iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する)
162        $this->setBody($body);
163        // 送信元設定
164        $this->setFrom($fromaddress, $from_name);
165        // 返信先設定
166        $this->setReplyTo($reply_to);
167        // CC設定
168        $this->setCc($cc);
169        // BCC設定
170        $this->setBcc($bcc);
171
172        // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
173        if($errors_to != "") {
174            $this->return_path = $errors_to;
175        } else if($return_path != "") {
176            $this->return_path = $return_path;
177        } else {
178            $this->return_path = $fromaddress;
179        }
180    }
181
182    // ヘッダーを返す
183    function getBaseHeader() {
184        //-- 送信するメールの内容と送信先
185        $arrHeader['MIME-Version'] = '1.0';
186        $arrHeader['To'] = $this->to;
187        $arrHeader['Subject'] = $this->subject;
188        $arrHeader['From'] = $this->from;
189        $arrHeader['Return-Path'] = $this->return_path;
190
191        if($this->reply_to != "") {
192            $arrHeader['Reply-To'] = $this->reply_to;
193        }
194
195        if($this->cc != "") {
196            $arrHeader['Cc'] = $this->cc;
197        }
198
199        if($this->bcc != "") {
200            $arrHeader['Bcc'] = $this->bcc;
201        }
202        return $arrHeader;
203    }
204
205    // ヘッダーを返す
206    function getTEXTHeader() {
207        $arrHeader = $this->getBaseHeader();
208        $arrHeader['Content-Type'] = "text/plain; charset=\"ISO-2022-JP\"";
209        $arrHeader['Content-Transfer-Encoding'] = "7bit";
210        return $arrHeader;
211    }
212
213    // ヘッダーを返す
214    function getHTMLHeader() {
215        $arrHeader = $this->getBaseHeader();
216        $arrHeader['Content-Type'] = "text/html; charset=\"ISO-2022-JP\"";
217        $arrHeader['Content-Transfer-Encoding'] = "ISO-2022-JP";
218        return $arrHeader;
219    }
220
221    //  TXTメール送信を実行する
222    function sendMail() {
223        $header = $this->getTEXTHeader();
224        // メール送信
225        $result = $this->objMail->send($this->to, $header, $this->body);
226        if (PEAR::isError($result)) {
227            GC_Utils_Ex::gfPrintLog($result->getMessage());
228            GC_Utils_Ex::gfDebugLog($header);
229            return false;
230        }
231        return true;
232    }
233
234    // HTMLメール送信を実行する
235    function sendHtmlMail() {
236        $header = $this->getHTMLHeader();
237        // メール送信
238        $result = $this->objMail->send($this->to, $header, $this->body);
239        if (PEAR::isError($result)) {
240            GC_Utils_Ex::gfPrintLog($result->getMessage());
241            GC_Utils_Ex::gfDebugLog($header);
242            return false;
243        }
244        return true;
245    }
246
247    /**
248     * メーラーバックエンドに応じたパラメータを返す.
249     *
250     * @param string $backend Pear::Mail のバックエンド
251     * @return array メーラーバックエンドに応じたパラメータの配列
252     */
253    function getBackendParams($backend) {
254
255        switch ($backend) {
256        case "mail":
257            return array();
258            break;
259
260        case "sendmail":
261            $arrParams = array('sendmail_path' => '/usr/bin/sendmail',
262                               'sendmail_args' => '-i'
263                               );
264            break;
265
266        case "smtp":
267        default:
268            $arrParams = array(
269                               'host' => $this->host,
270                               'port' => $this->port
271                               );
272        }
273    }
274}
275?>
Note: See TracBrowser for help on using the repository browser.