| 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 | |
|---|
| 28 | var $to; // 送信先 |
|---|
| 29 | var $subject; // 題名 |
|---|
| 30 | var $body; // 本文 |
|---|
| 31 | var $cc; // CC |
|---|
| 32 | var $bcc; // BCC |
|---|
| 33 | var $replay_to; // replay_to |
|---|
| 34 | var $return_path; // return_path |
|---|
| 35 | var $objMail; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * コンストラクタ |
|---|
| 39 | * |
|---|
| 40 | * @return void |
|---|
| 41 | */ |
|---|
| 42 | function __construct() |
|---|
| 43 | { |
|---|
| 44 | $this->arrRecip = array(); |
|---|
| 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->backend = MAIL_BACKEND; |
|---|
| 53 | $this->host = SMTP_HOST; |
|---|
| 54 | $this->port = SMTP_PORT; |
|---|
| 55 | |
|---|
| 56 | // PEAR::Mailを使ってメール送信オブジェクト作成 |
|---|
| 57 | $this->objMail =& Mail::factory($this->backend, |
|---|
| 58 | $this->getBackendParams($this->backend)); |
|---|
| 59 | if (PEAR::isError($this->objMail)) { |
|---|
| 60 | // XXX 環境によっては文字エンコードに差異がないか些か心配 |
|---|
| 61 | trigger_error($this->objMail->getMessage(), E_USER_ERROR); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // 送信先の設定 |
|---|
| 66 | function setRecip($key, $recipient) |
|---|
| 67 | { |
|---|
| 68 | $this->arrRecip[$key] = $recipient; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | // 宛先の設定 |
|---|
| 72 | function setTo($to, $to_name = '') |
|---|
| 73 | { |
|---|
| 74 | if ($to != '') { |
|---|
| 75 | $this->to = $this->getNameAddress($to_name, $to); |
|---|
| 76 | $this->setRecip('To', $to); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // 送信元の設定 |
|---|
| 81 | function setFrom($from, $from_name = '') |
|---|
| 82 | { |
|---|
| 83 | $this->from = $this->getNameAddress($from_name, $from); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // CCの設定 |
|---|
| 87 | function setCc($cc, $cc_name = '') |
|---|
| 88 | { |
|---|
| 89 | if ($cc != '') { |
|---|
| 90 | $this->cc = $this->getNameAddress($cc_name, $cc); |
|---|
| 91 | $this->setRecip('Cc', $cc); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | // BCCの設定 |
|---|
| 96 | function setBCc($bcc) |
|---|
| 97 | { |
|---|
| 98 | if ($bcc != '') { |
|---|
| 99 | $this->bcc = $bcc; |
|---|
| 100 | $this->setRecip('Bcc', $bcc); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // Reply-Toの設定 |
|---|
| 105 | function setReplyTo($reply_to) |
|---|
| 106 | { |
|---|
| 107 | if ($reply_to != '') { |
|---|
| 108 | $this->reply_to = $reply_to; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // Return-Pathの設定 |
|---|
| 113 | function setReturnPath($return_path) |
|---|
| 114 | { |
|---|
| 115 | $this->return_path = $return_path; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | // 件名の設定 |
|---|
| 119 | function setSubject($subject) |
|---|
| 120 | { |
|---|
| 121 | $this->subject = mb_encode_mimeheader($subject, 'JIS', 'B', "\n"); |
|---|
| 122 | $this->subject = str_replace(array("\r\n", "\r"), "\n", $this->subject); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | // 本文の設定 |
|---|
| 126 | function setBody($body) |
|---|
| 127 | { |
|---|
| 128 | // iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する |
|---|
| 129 | $this->body = mb_convert_encoding($body, 'JIS', CHAR_CODE); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * 前方互換用 |
|---|
| 134 | * |
|---|
| 135 | * @deprecated 2.12.2 (#1912) |
|---|
| 136 | */ |
|---|
| 137 | 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 | * |
|---|
| 153 | * @deprecated 2.12.2 (#1912) |
|---|
| 154 | */ |
|---|
| 155 | function setPort($port) |
|---|
| 156 | { |
|---|
| 157 | trigger_error('前方互換用メソッドが使用されました。', E_USER_WARNING); |
|---|
| 158 | $this->port = $port; |
|---|
| 159 | $arrHost = array( |
|---|
| 160 | 'host' => $this->host, |
|---|
| 161 | 'port' => $this->port |
|---|
| 162 | ); |
|---|
| 163 | // PEAR::Mailを使ってメール送信オブジェクト作成 |
|---|
| 164 | $this->objMail =& Mail::factory('smtp', $arrHost); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // 名前<メールアドレス>の形式を生成 |
|---|
| 168 | function getNameAddress($name, $mail_address) |
|---|
| 169 | { |
|---|
| 170 | if ($name != '') { |
|---|
| 171 | // 制御文字を変換する。 |
|---|
| 172 | $_name = $name; |
|---|
| 173 | $_name = mb_encode_mimeheader($_name, 'JIS', 'B', "\n"); |
|---|
| 174 | $_name = str_replace('"', '\"', $_name); |
|---|
| 175 | $name_address = sprintf('"%s" <%s>', $_name, $mail_address); |
|---|
| 176 | } else { |
|---|
| 177 | $name_address = $mail_address; |
|---|
| 178 | } |
|---|
| 179 | return $name_address; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | 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 | 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 | 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 | } else if ($return_path != '') { |
|---|
| 225 | $this->return_path = $return_path; |
|---|
| 226 | } else { |
|---|
| 227 | $this->return_path = $fromaddress; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | // ヘッダーを返す |
|---|
| 232 | 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 | return $arrHeader; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | // ヘッダーを返す |
|---|
| 256 | function getTEXTHeader() |
|---|
| 257 | { |
|---|
| 258 | $arrHeader = $this->getBaseHeader(); |
|---|
| 259 | $arrHeader['Content-Type'] = 'text/plain; charset="ISO-2022-JP"'; |
|---|
| 260 | return $arrHeader; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | // ヘッダーを返す |
|---|
| 264 | function getHTMLHeader() |
|---|
| 265 | { |
|---|
| 266 | $arrHeader = $this->getBaseHeader(); |
|---|
| 267 | $arrHeader['Content-Type'] = 'text/html; charset="ISO-2022-JP"'; |
|---|
| 268 | return $arrHeader; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /** |
|---|
| 272 | * メーラーバックエンドに応じた送信先を返す |
|---|
| 273 | * |
|---|
| 274 | * @return array|string メーラーバックエンドに応じた送信先 |
|---|
| 275 | */ |
|---|
| 276 | function getRecip() |
|---|
| 277 | { |
|---|
| 278 | switch ($this->backend) { |
|---|
| 279 | // PEAR::Mail_mail#send は、(他のメーラーバックエンドと異なり) 第1引数を To: として扱う。Cc: や Bcc: は、ヘッダー情報から処理する。 |
|---|
| 280 | case 'mail': |
|---|
| 281 | return $this->to; |
|---|
| 282 | |
|---|
| 283 | case 'sendmail': |
|---|
| 284 | case 'smtp': |
|---|
| 285 | default: |
|---|
| 286 | return $this->arrRecip; |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * TXTメール送信を実行する. |
|---|
| 292 | * |
|---|
| 293 | * 設定された情報を利用して, メールを送信する. |
|---|
| 294 | * |
|---|
| 295 | * @return void |
|---|
| 296 | */ |
|---|
| 297 | function sendMail($isHtml = false) |
|---|
| 298 | { |
|---|
| 299 | $header = $isHtml ? $this->getHTMLHeader() : $this->getTEXTHeader(); |
|---|
| 300 | $recip = $this->getRecip(); |
|---|
| 301 | // メール送信 |
|---|
| 302 | $result = $this->objMail->send($recip, $header, $this->body); |
|---|
| 303 | if (PEAR::isError($result)) { |
|---|
| 304 | // XXX Windows 環境では SJIS でメッセージを受け取るようなので変換する。 |
|---|
| 305 | $msg = mb_convert_encoding($result->getMessage(), CHAR_CODE, 'auto'); |
|---|
| 306 | $msg = 'メール送信に失敗しました。[' . $msg . ']'; |
|---|
| 307 | trigger_error($msg, E_USER_WARNING); |
|---|
| 308 | GC_Utils_Ex::gfDebugLog($header); |
|---|
| 309 | return false; |
|---|
| 310 | } |
|---|
| 311 | return true; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /** |
|---|
| 315 | * HTMLメール送信を実行する. |
|---|
| 316 | * |
|---|
| 317 | * @return void |
|---|
| 318 | */ |
|---|
| 319 | function sendHtmlMail() |
|---|
| 320 | { |
|---|
| 321 | return $this->sendMail(true); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | /** |
|---|
| 325 | * メーラーバックエンドに応じたパラメーターを返す. |
|---|
| 326 | * |
|---|
| 327 | * @param string $backend Pear::Mail のバックエンド |
|---|
| 328 | * @return array メーラーバックエンドに応じたパラメーターの配列 |
|---|
| 329 | */ |
|---|
| 330 | function getBackendParams($backend) |
|---|
| 331 | { |
|---|
| 332 | switch ($backend) { |
|---|
| 333 | case 'mail': |
|---|
| 334 | $arrParams = array(); |
|---|
| 335 | break; |
|---|
| 336 | |
|---|
| 337 | case 'sendmail': |
|---|
| 338 | $arrParams = array( |
|---|
| 339 | 'sendmail_path' => '/usr/bin/sendmail', |
|---|
| 340 | 'sendmail_args' => '-i', |
|---|
| 341 | ); |
|---|
| 342 | break; |
|---|
| 343 | |
|---|
| 344 | case 'smtp': |
|---|
| 345 | $arrParams = array( |
|---|
| 346 | 'host' => $this->host, |
|---|
| 347 | 'port' => $this->port, |
|---|
| 348 | ); |
|---|
| 349 | if (defined('SMTP_USER') |
|---|
| 350 | && defined('SMTP_PASSWORD') |
|---|
| 351 | && !SC_Utils_Ex::isBlank(SMTP_USER) |
|---|
| 352 | && !SC_Utils_Ex::isBlank(SMTP_PASSWORD)) { |
|---|
| 353 | $arrParams['auth'] = true; |
|---|
| 354 | $arrParams['username'] = SMTP_USER; |
|---|
| 355 | $arrParams['password'] = SMTP_PASSWORD; |
|---|
| 356 | } |
|---|
| 357 | break; |
|---|
| 358 | |
|---|
| 359 | default: |
|---|
| 360 | trigger_error('不明なバックエンド。[$backend = ' . var_export($backend, true) . ']', E_USER_ERROR); |
|---|
| 361 | exit; |
|---|
| 362 | } |
|---|
| 363 | return $arrParams; |
|---|
| 364 | } |
|---|
| 365 | } |
|---|