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

Revision 16330, 7.1 KB checked in by naka, 16 years ago (diff)

mdl_speedmail開発

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->host = SMTP_HOST;
38        $this->port = SMTP_PORT;
39        $this->objMailMime = new Mail_mime();
40        mb_language( "Japanese" );
41        $arrHost = array(   
42                'host' => $this->host,
43                'port' => $this->port
44        );
45        //-- PEAR::Mailを使ってメール送信オブジェクト作成
46        $this->objMail =& Mail::factory("smtp", $arrHost);
47    }
48   
49    // 宛先の設定
50    function setTo($to, $to_name = "") {
51        $this->to = $this->getNameAddress($to_name, $to);
52    }
53   
54    // 送信元の設定
55    function setFrom($from, $from_name = "") {
56        $this->from = $this->getNameAddress($from_name, $from);
57    }
58   
59    // CCの設定
60    function setCc($cc, $cc_name = "") {
61        if($cc != "") {
62            $this->cc = $this->getNameAddress($cc_name, $cc);
63        }
64    }
65   
66    // BCCの設定
67    function setBCc($bcc) {
68        if($bcc != "") {
69            $this->bcc = $bcc;
70        }
71    }
72   
73    // Reply-Toの設定
74    function setReplyTo($reply_to) {
75        if($reply_to != "") {
76            $this->reply_to = $reply_to;           
77        }       
78    }
79   
80    // Return-Pathの設定
81    function setReturnPath($return_path) {
82        $this->return_path = $return_path;
83    }   
84   
85    // 件名の設定
86    function setSubject($subject) {
87        $this->subject = mb_encode_mimeheader($subject);
88    }
89   
90    // 本文の設定
91    function setBody($body) {
92        $this->body = mb_convert_encoding($body, "JIS", CHAR_CODE);
93    }
94   
95    // SMTPサーバの設定
96    function setHost($host) {
97        $this->host = $host;
98        $arrHost = array(   
99                'host' => $this->host,
100                'port' => $this->port
101        );
102        //-- PEAR::Mailを使ってメール送信オブジェクト作成
103        $this->objMail =& Mail::factory("smtp", $arrHost);
104       
105    }
106   
107    // SMTPポートの設定
108    function setPort($port) {
109        $this->port = $port;
110        $arrHost = array(   
111                'host' => $this->host,
112                'port' => $this->port
113        );
114        //-- PEAR::Mailを使ってメール送信オブジェクト作成
115        $this->objMail =& Mail::factory("smtp", $arrHost);     
116    }
117   
118    // 名前<メールアドレス>の形式を生成
119    function getNameAddress($name, $mail_address) {
120            if($name != "") {
121                // 制御文字を変換する。               
122                $_name = $name;             
123                $_name = ereg_replace("<","<", $_name);
124                $_name = ereg_replace(">",">", $_name);
125                if(OS_TYPE != 'WIN') {
126                    // windowsでは文字化けするので使用しない。
127                    $_name = mb_convert_encoding($_name,"JIS",CHAR_CODE);   
128                }
129                $_name = mb_encode_mimeheader($_name);
130                $name_address = "\"". $_name . "\"<" . $mail_address . ">";
131            } else {
132                $name_address = $mail_address;
133            }
134            return $name_address;
135    }
136   
137    function setItem( $to, $subject, $body, $fromaddress, $from_name, $reply_to="", $return_path="", $errors_to="", $bcc="", $cc ="" ) {
138        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
139    }
140   
141    function setItemHtml( $to, $subject, $body, $fromaddress, $from_name, $reply_to="", $return_path="", $errors_to="", $bcc="", $cc ="" ) {
142        $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc);
143    }
144
145    /*  ヘッダ等を格納
146         $to            -> 送信先メールアドレス
147         $subject       -> メールのタイトル
148         $body          -> メール本文
149         $fromaddress   -> 送信元のメールアドレス
150         $header        -> ヘッダー
151         $from_name     -> 送信元の名前(全角OK)
152         $reply_to      -> reply_to設定
153         $return_path   -> return-pathアドレス設定(エラーメール返送用)
154         $cc            -> カーボンコピー
155         $bcc           -> ブラインドカーボンコピー
156    */     
157    function setBase( $to, $subject, $body, $fromaddress, $from_name, $reply_to="", $return_path="", $errors_to="", $bcc="", $cc ="" ) {
158        // 宛先設定
159        $this->to = $to;   
160        // 件名設定
161        $this->setSubject($subject);
162        // 本文設定(iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する)
163        $this->setBody($body);
164        // 送信元設定
165        $this->setFrom($fromaddress, $from_name);
166        // 返信先設定
167        $this->setReplyTo($reply_to);
168        // CC設定
169        $this->setCc($cc);
170        // BCC設定
171        $this->setBcc($bcc);
172       
173        // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
174        if($errors_to != "") {
175            $this->return_path = $errors_to;
176        } else if($return_path != "") {
177            $this->return_path = $return_path;
178        } else {
179            $this->return_path = $fromaddress;
180        }
181    }
182   
183    // ヘッダーを返す
184    function getBaseHeader() {
185        //-- 送信するメールの内容と送信先
186        $arrHeader['MIME-Version'] = '1.0';
187        $arrHeader['To'] = $this->to;
188        $arrHeader['Subject'] = $this->subject;
189        $arrHeader['From'] = $this->from;
190        $arrHeader['Return-Path'] = $this->return_path;
191       
192        if($this->reply_to != "") {
193            $arrHeader['Reply-To'] = $this->reply_to;
194        }
195       
196        if($this->cc != "") {
197            $arrHeader['Cc'] = $this->cc;
198        }
199       
200        if($this->bcc != "") {     
201            $arrHeader['Bcc'] = $this->bcc;
202        }
203        return $arrHeader;
204    }
205   
206    // ヘッダーを返す
207    function getTEXTHeader() {
208        $arrHeader = $this->getBaseHeader();
209        $arrHeader['Content-Type'] = "text/plain; charset=\"ISO-2022-JP\"";
210        $arrHeader['Content-Transfer-Encoding'] = "7bit";   
211        return $arrHeader;
212    }
213   
214    // ヘッダーを返す
215    function getHTMLHeader() {
216        $arrHeader = $this->getBaseHeader();
217        $arrHeader['Content-Type'] = "text/html; charset=\"ISO-2022-JP\"";
218        $arrHeader['Content-Transfer-Encoding'] = "ISO-2022-JP";   
219        return $arrHeader;
220    }
221   
222    //  TXTメール送信を実行する
223    function sendMail() {
224        $header = $this->getTEXTHeader();
225        // メール送信
226        $result = $this->objMail->send($this->to, $header, $this->body);           
227        if (PEAR::isError($result)) {
228            GC_Utils_Ex::gfPrintLog($result->getMessage());
229            GC_Utils_Ex::gfDebugLog($header);
230            return false;       
231        }
232        return true;
233    }
234   
235    // HTMLメール送信を実行する
236    function sendHtmlMail() {
237        $header = $this->getHTMLHeader();
238        // メール送信
239        $result = $this->objMail->send($this->to, $header, $this->body);           
240        if (PEAR::isError($result)) {
241            GC_Utils_Ex::gfPrintLog($result->getMessage());
242            GC_Utils_Ex::gfDebugLog($header);   
243            return false;
244        }
245        return true;
246    }
247}
248?>
Note: See TracBrowser for help on using the repository browser.