Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Email.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * Email function.
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Email extends \Kotchasan\KBase
 21: {
 22:     /**
 23:      * @var mixed
 24:      */
 25:     protected $error;
 26: 
 27:     /**
 28:      * ตรวจสอบว่ามีข้อผิดพลาดในการส่งอีเมลหรือไม่
 29:      * คืนค่า true ถ้ามี error, false ถ้าส่งอีเมลสำเร็จ.
 30:      *
 31:      * @return bool
 32:      */
 33:     public function error()
 34:     {
 35:         return empty($this->error) ? false : true;
 36:     }
 37: 
 38:     /**
 39:      * คืนค่าข้อผิดพลาดการส่งอีเมล
 40:      * ถ้าไม่มีข้อผิดพลาดคืนค่าข้อความว่าง
 41:      *
 42:      * @return string
 43:      */
 44:     public function getErrorMessage()
 45:     {
 46:         return empty($this->error) ? '' : implode("\n", $this->error);
 47:     }
 48: 
 49:     /**
 50:      * ฟังก์ชั่นส่งเมล์แบบกำหนดรายละเอียดเอง
 51:      *
 52:      * @param string $mailto  ที่อยู่อีเมลผู้รับ User Name<email@domain.tld> หรือ email@domain.tld คั่นแต่ละรายการด้วย ,
 53:      * @param string $replyto ที่อยู่อีเมลสำหรับการตอบกลับจดหมาย ถ้าระบุเป็นค่าว่างจะใช้ที่อยู่อีเมลจาก noreply_email
 54:      * @param string $subject หัวข้อจดหมาย
 55:      * @param string $msg รายละเอียดของจดหมาย (รองรับ HTML)
 56:      * @param string $cc ที่อยู่อีเมล์สำหรับ ส่งแบบ CC คั่นแต่ละรายการด้วย ,
 57:      * @param string $bcc ที่อยู่อีเมล์สำหรับ ส่งแบบ BCC คั่นแต่ละรายการด้วย ,
 58:      *
 59:      * @return \static
 60:      */
 61:     public static function send($mailto, $replyto, $subject, $msg, $cc = '', $bcc = '')
 62:     {
 63:         // create class
 64:         $obj = new static();
 65:         $obj->error = array();
 66:         // send email
 67:         $charset = empty(self::$cfg->email_charset) ? 'utf-8' : strtolower(self::$cfg->email_charset);
 68:         if (empty($replyto)) {
 69:             $replyto = array(strip_tags(self::$cfg->web_title), self::$cfg->noreply_email);
 70:         } elseif (preg_match('/^(.*)<(.*?)>$/', $replyto, $match)) {
 71:             $replyto = array(strip_tags($match[1]), (empty($match[2]) ? $match[1] : $match[2]));
 72:         } else {
 73:             $replyto = array($replyto, $replyto);
 74:         }
 75:         if ($charset != 'utf-8') {
 76:             $subject = iconv('utf-8', $charset, $subject);
 77:             $msg = iconv('utf-8', $charset, $msg);
 78:             $replyto[0] = iconv('utf-8', $charset, $replyto[0]);
 79:         }
 80:         $msg = preg_replace(array('/<\?/', '/\?>/'), array('&lt;?', '?&gt;'), $msg);
 81:         if (empty(self::$cfg->email_use_phpMailer)) {
 82:             // ส่งอีเมลด้วยฟังก์ชั่นของ PHP
 83:             $emails = array($mailto);
 84:             if ($cc != '') {
 85:                 $emails[] = $cc;
 86:             }
 87:             if ($bcc != '') {
 88:                 $emails[] = $bcc;
 89:             }
 90:             $headers = "MIME-Version: 1.0\r\n";
 91:             $headers .= 'Content-type: text/html; charset='.strtoupper($charset)."\r\n";
 92:             $headers .= 'From: '.$replyto[0]."\r\n";
 93:             $headers .= "Reply-to: $replyto[1]\r\n";
 94:             if (!@mail(implode(',', $emails), $subject, $msg, $headers)) {
 95:                 $obj->error['Unable to send mail'] = Language::get('Unable to send mail');
 96:             }
 97:         } else {
 98:             // ส่งอีเมลด้วย PHPMailer
 99:             include_once VENDOR_DIR.'PHPMailer/class.phpmailer.php';
100:             // Create a new PHPMailer instance
101:             $mail = new \PHPMailer();
102:             if (self::$cfg->email_use_phpMailer == 1) {
103:                 // Send messages using SMTP
104:                 $mail->isSMTP();
105:             } else {
106:                 // Send messages using PHP's mail() function
107:                 $mail->isMail();
108:             }
109:             // charset
110:             $mail->CharSet = $charset;
111:             // use html
112:             $mail->IsHTML();
113:             $mail->SMTPAuth = empty(self::$cfg->email_SMTPAuth) ? false : true;
114:             if ($mail->SMTPAuth) {
115:                 $mail->Username = self::$cfg->email_Username;
116:                 $mail->Password = self::$cfg->email_Password;
117:                 $mail->SMTPSecure = self::$cfg->email_SMTPSecure;
118:             }
119:             if (!empty(self::$cfg->email_Host)) {
120:                 $mail->Host = self::$cfg->email_Host;
121:             }
122:             if (!empty(self::$cfg->email_Port)) {
123:                 $mail->Port = self::$cfg->email_Port;
124:             }
125:             $mail->SMTPOptions = array(
126:                 'ssl' => array(
127:                     'verify_peer' => false,
128:                     'verify_peer_name' => false,
129:                     'allow_self_signed' => true,
130:                 ),
131:             );
132:             $mail->AddReplyTo($replyto[1], $replyto[0]);
133:             if ($mail->ValidateAddress(self::$cfg->noreply_email)) {
134:                 $mail->SetFrom(self::$cfg->noreply_email, strip_tags(self::$cfg->web_title));
135:             }
136:             // subject
137:             $mail->Subject = $subject;
138:             // message
139:             $mail->MsgHTML(preg_replace('/(<br([\s\/]{0,})>)/', "$1\r\n", $msg));
140:             $mail->AltBody = strip_tags($msg);
141:             foreach (explode(',', $mailto) as $email) {
142:                 if (preg_match('/^(.*)<(.*)>$/', $email, $match)) {
143:                     if ($mail->validateAddress($match[2])) {
144:                         $mail->addAddress($match[2], strip_tags($match[1]));
145:                     }
146:                 } elseif ($mail->validateAddress($email)) {
147:                     $mail->addAddress($email);
148:                 }
149:                 if ($cc != '') {
150:                     foreach (explode(',', $cc) as $cc_email) {
151:                         if ($mail->validateAddress($cc_email)) {
152:                             $mail->addCC($cc_email);
153:                         }
154:                     }
155:                 }
156:                 if ($bcc != '') {
157:                     foreach (explode(',', $bcc) as $bcc_email) {
158:                         if ($mail->validateAddress($bcc_email)) {
159:                             $mail->addBCC($bcc_email);
160:                         }
161:                     }
162:                 }
163:                 $err = $mail->send();
164:                 if ($err === false) {
165:                     $obj->error[$mail->ErrorInfo] = strip_tags($mail->ErrorInfo);
166:                 }
167:                 $mail->clearAddresses();
168:             }
169:         }
170:         return $obj;
171:     }
172: }
173: 
Kotchasan API documentation generated by ApiGen