1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2011 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 | //--- テキスト/HTML メール送信 |
---|
25 | class SC_SendMail { |
---|
26 | |
---|
27 | var $to; // 送信先 |
---|
28 | var $subject; // 題名 |
---|
29 | var $body; // 本文 |
---|
30 | var $cc; // CC |
---|
31 | var $bcc; // BCC |
---|
32 | var $replay_to; // replay_to |
---|
33 | var $return_path; // return_path |
---|
34 | var $objMail; |
---|
35 | |
---|
36 | // コンストラクタ |
---|
37 | function __construct() { |
---|
38 | $this->arrRecip = array(); |
---|
39 | $this->to = ''; |
---|
40 | $this->subject = ''; |
---|
41 | $this->body = ''; |
---|
42 | $this->cc = ''; |
---|
43 | $this->bcc = ''; |
---|
44 | $this->replay_to = ''; |
---|
45 | $this->return_path = ''; |
---|
46 | $this->backend = MAIL_BACKEND; |
---|
47 | $this->host = SMTP_HOST; |
---|
48 | $this->port = SMTP_PORT; |
---|
49 | mb_language('Japanese'); |
---|
50 | |
---|
51 | //-- PEAR::Mailを使ってメール送信オブジェクト作成 |
---|
52 | $this->objMail =& Mail::factory($this->backend, |
---|
53 | $this->getBackendParams($this->backend)); |
---|
54 | } |
---|
55 | |
---|
56 | // 送信先の設定 |
---|
57 | function setRecip($key, $recipient) { |
---|
58 | $this->arrRecip[$key] = $recipient; |
---|
59 | } |
---|
60 | |
---|
61 | // 宛先の設定 |
---|
62 | function setTo($to, $to_name = '') { |
---|
63 | if ($to != '') { |
---|
64 | $this->to = $this->getNameAddress($to_name, $to); |
---|
65 | $this->setRecip('To', $to); |
---|
66 | } |
---|
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 | $this->setRecip('Cc', $cc); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | // BCCの設定 |
---|
83 | function setBCc($bcc) { |
---|
84 | if ($bcc != '') { |
---|
85 | $this->bcc = $bcc; |
---|
86 | $this->setRecip('Bcc', $bcc); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | // Reply-Toの設定 |
---|
91 | function setReplyTo($reply_to) { |
---|
92 | if ($reply_to != '') { |
---|
93 | $this->reply_to = $reply_to; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | // Return-Pathの設定 |
---|
98 | function setReturnPath($return_path) { |
---|
99 | $this->return_path = $return_path; |
---|
100 | } |
---|
101 | |
---|
102 | // 件名の設定 |
---|
103 | function setSubject($subject) { |
---|
104 | $this->subject = mb_encode_mimeheader($subject, 'JIS', 'B', "\n"); |
---|
105 | $this->subject = str_replace("\x0D\x0A", "\n", $this->subject); |
---|
106 | $this->subject = str_replace("\x0D", "\n", $this->subject); |
---|
107 | $this->subject = str_replace("\x0A", "\n", $this->subject); |
---|
108 | } |
---|
109 | |
---|
110 | // 本文の設定 |
---|
111 | function setBody($body) { |
---|
112 | // iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する |
---|
113 | $this->body = mb_convert_encoding($body, 'JIS', CHAR_CODE); |
---|
114 | } |
---|
115 | |
---|
116 | // SMTPサーバーの設定 |
---|
117 | function setHost($host) { |
---|
118 | $this->host = $host; |
---|
119 | $arrHost = array( |
---|
120 | 'host' => $this->host, |
---|
121 | 'port' => $this->port |
---|
122 | ); |
---|
123 | //-- PEAR::Mailを使ってメール送信オブジェクト作成 |
---|
124 | $this->objMail =& Mail::factory('smtp', $arrHost); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | // SMTPポートの設定 |
---|
129 | function setPort($port) { |
---|
130 | $this->port = $port; |
---|
131 | $arrHost = array( |
---|
132 | 'host' => $this->host, |
---|
133 | 'port' => $this->port |
---|
134 | ); |
---|
135 | //-- PEAR::Mailを使ってメール送信オブジェクト作成 |
---|
136 | $this->objMail =& Mail::factory('smtp', $arrHost); |
---|
137 | } |
---|
138 | |
---|
139 | // 名前<メールアドレス>の形式を生成 |
---|
140 | function getNameAddress($name, $mail_address) { |
---|
141 | if ($name != '') { |
---|
142 | // 制御文字を変換する。 |
---|
143 | $_name = $name; |
---|
144 | $_name = mb_encode_mimeheader($_name, 'JIS', 'B', "\n"); |
---|
145 | $_name = str_replace('"', '\"', $_name); |
---|
146 | $name_address = sprintf('"%s" <%s>', $_name, $mail_address); |
---|
147 | } else { |
---|
148 | $name_address = $mail_address; |
---|
149 | } |
---|
150 | return $name_address; |
---|
151 | } |
---|
152 | |
---|
153 | function setItem($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') { |
---|
154 | $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc); |
---|
155 | } |
---|
156 | |
---|
157 | function setItemHtml($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') { |
---|
158 | $this->setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to, $return_path, $errors_to, $bcc, $cc); |
---|
159 | } |
---|
160 | |
---|
161 | /* ヘッダ等を格納 |
---|
162 | $to -> 送信先メールアドレス |
---|
163 | $subject -> メールのタイトル |
---|
164 | $body -> メール本文 |
---|
165 | $fromaddress -> 送信元のメールアドレス |
---|
166 | $header -> ヘッダー |
---|
167 | $from_name -> 送信元の名前(全角OK) |
---|
168 | $reply_to -> reply_to設定 |
---|
169 | $return_path -> return-pathアドレス設定(エラーメール返送用) |
---|
170 | $cc -> カーボンコピー |
---|
171 | $bcc -> ブラインドカーボンコピー |
---|
172 | */ |
---|
173 | function setBase($to, $subject, $body, $fromaddress, $from_name, $reply_to='', $return_path='', $errors_to='', $bcc='', $cc ='') { |
---|
174 | // 宛先設定 |
---|
175 | $this->setTo($to); |
---|
176 | // 件名設定 |
---|
177 | $this->setSubject($subject); |
---|
178 | // 本文設定 |
---|
179 | $this->setBody($body); |
---|
180 | // 送信元設定 |
---|
181 | $this->setFrom($fromaddress, $from_name); |
---|
182 | // 返信先設定 |
---|
183 | $this->setReplyTo($reply_to); |
---|
184 | // CC設定 |
---|
185 | $this->setCc($cc); |
---|
186 | // BCC設定 |
---|
187 | $this->setBcc($bcc); |
---|
188 | |
---|
189 | // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。 |
---|
190 | if ($errors_to != '') { |
---|
191 | $this->return_path = $errors_to; |
---|
192 | } else if ($return_path != '') { |
---|
193 | $this->return_path = $return_path; |
---|
194 | } else { |
---|
195 | $this->return_path = $fromaddress; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | // ヘッダーを返す |
---|
200 | function getBaseHeader() { |
---|
201 | //-- 送信するメールの内容と送信先 |
---|
202 | $arrHeader['MIME-Version'] = '1.0'; |
---|
203 | $arrHeader['To'] = $this->to; |
---|
204 | $arrHeader['Subject'] = $this->subject; |
---|
205 | $arrHeader['From'] = $this->from; |
---|
206 | $arrHeader['Return-Path'] = $this->return_path; |
---|
207 | if ($this->reply_to != '') { |
---|
208 | $arrHeader['Reply-To'] = $this->reply_to; |
---|
209 | } |
---|
210 | if ($this->cc != '') { |
---|
211 | $arrHeader['Cc'] = $this->cc; |
---|
212 | } |
---|
213 | if ($this->bcc != '') { |
---|
214 | $arrHeader['Bcc'] = $this->bcc; |
---|
215 | } |
---|
216 | $arrHeader['Date'] = date('D, j M Y H:i:s O'); |
---|
217 | $arrHeader['Content-Transfer-Encoding'] = '7bit'; |
---|
218 | return $arrHeader; |
---|
219 | } |
---|
220 | |
---|
221 | // ヘッダーを返す |
---|
222 | function getTEXTHeader() { |
---|
223 | $arrHeader = $this->getBaseHeader(); |
---|
224 | $arrHeader['Content-Type'] = 'text/plain; charset="ISO-2022-JP"'; |
---|
225 | return $arrHeader; |
---|
226 | } |
---|
227 | |
---|
228 | // ヘッダーを返す |
---|
229 | function getHTMLHeader() { |
---|
230 | $arrHeader = $this->getBaseHeader(); |
---|
231 | $arrHeader['Content-Type'] = 'text/html; charset="ISO-2022-JP"'; |
---|
232 | return $arrHeader; |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * メーラーバックエンドに応じた送信先を返す |
---|
237 | * |
---|
238 | * @return array|string メーラーバックエンドに応じた送信先 |
---|
239 | */ |
---|
240 | function getRecip() { |
---|
241 | switch ($this->backend) { |
---|
242 | // PEAR::Mail_mail#send は、(他のメーラーバックエンドと異なり) 第1引数を To: として扱う。Cc: や Bcc: は、ヘッダー情報から処理する。 |
---|
243 | case 'mail': |
---|
244 | return $this->to; |
---|
245 | |
---|
246 | case 'sendmail': |
---|
247 | case 'smtp': |
---|
248 | default: |
---|
249 | return $this->arrRecip; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * TXTメール送信を実行する. |
---|
255 | * |
---|
256 | * 設定された情報を利用して, メールを送信する. |
---|
257 | * |
---|
258 | * @return void |
---|
259 | */ |
---|
260 | function sendMail($isHtml = false) { |
---|
261 | $header = $isHtml ? $this->getHTMLHeader() : $this->getTEXTHeader(); |
---|
262 | $recip = $this->getRecip(); |
---|
263 | // メール送信 |
---|
264 | $result = $this->objMail->send($recip, $header, $this->body); |
---|
265 | if (PEAR::isError($result)) { |
---|
266 | GC_Utils_Ex::gfPrintLog($result->getMessage()); |
---|
267 | GC_Utils_Ex::gfDebugLog($header); |
---|
268 | return false; |
---|
269 | } |
---|
270 | return true; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * HTMLメール送信を実行する. |
---|
275 | * |
---|
276 | * @return void |
---|
277 | */ |
---|
278 | function sendHtmlMail() { |
---|
279 | return $this->sendMail(true); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * メーラーバックエンドに応じたパラメーターを返す. |
---|
284 | * |
---|
285 | * @param string $backend Pear::Mail のバックエンド |
---|
286 | * @return array メーラーバックエンドに応じたパラメーターの配列 |
---|
287 | */ |
---|
288 | function getBackendParams($backend) { |
---|
289 | switch ($backend) { |
---|
290 | case 'mail': |
---|
291 | $arrParams = array(); |
---|
292 | break; |
---|
293 | case 'sendmail': |
---|
294 | $arrParams = array( |
---|
295 | 'sendmail_path' => '/usr/bin/sendmail', |
---|
296 | 'sendmail_args' => '-i', |
---|
297 | ); |
---|
298 | break; |
---|
299 | case 'smtp': |
---|
300 | default: |
---|
301 | $arrParams = array( |
---|
302 | 'host' => $this->host, |
---|
303 | 'port' => $this->port, |
---|
304 | ); |
---|
305 | if (defined('SMTP_USER') |
---|
306 | && defined('SMTP_PASSWORD') |
---|
307 | && !SC_Utils_Ex::isBlank(SMTP_USER) |
---|
308 | && !SC_Utils_Ex::isBlank(SMTP_PASSWORD)) { |
---|
309 | $arrParams['auth'] = true; |
---|
310 | $arrParams['username'] = SMTP_USER; |
---|
311 | $arrParams['password'] = SMTP_PASSWORD; |
---|
312 | } |
---|
313 | break; |
---|
314 | } |
---|
315 | return $arrParams; |
---|
316 | } |
---|
317 | } |
---|