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