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