source: branches/feature-module-update/data/class/GC_SendMail.php @ 16303

Revision 16303, 7.0 KB checked in by naka, 19 years ago (diff)

GC_SendMailメソッド追加

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