1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Email extends \Kotchasan\KBase
21: {
22: 23: 24:
25: protected $error;
26:
27: 28: 29: 30: 31: 32:
33: public function error()
34: {
35: return empty($this->error) ? false : true;
36: }
37:
38: 39: 40: 41: 42: 43:
44: public function getErrorMessage()
45: {
46: return empty($this->error) ? '' : implode("\n", $this->error);
47: }
48:
49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
61: public static function send($mailto, $replyto, $subject, $msg, $cc = '', $bcc = '')
62: {
63:
64: $obj = new static();
65: $obj->error = array();
66:
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('<?', '?>'), $msg);
81: if (empty(self::$cfg->email_use_phpMailer)) {
82:
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:
99: include_once VENDOR_DIR.'PHPMailer/class.phpmailer.php';
100:
101: $mail = new \PHPMailer();
102: if (self::$cfg->email_use_phpMailer == 1) {
103:
104: $mail->isSMTP();
105: } else {
106:
107: $mail->isMail();
108: }
109:
110: $mail->CharSet = $charset;
111:
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:
137: $mail->Subject = $subject;
138:
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: