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