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