PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/library/mail.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 414 lines | 319 code | 94 blank | 1 comment | 104 complexity | c583cda61fe3650c349215e16a024f22 MD5 | raw file
  1. <?php
  2. final class Mail {
  3. protected $to;
  4. protected $from;
  5. protected $sender;
  6. protected $subject;
  7. protected $text;
  8. protected $html;
  9. protected $attachments = array();
  10. public $protocol = 'mail';
  11. public $hostname;
  12. public $username;
  13. public $password;
  14. public $port = 25;
  15. public $timeout = 5;
  16. public $newline = "\n";
  17. public $crlf = "\r\n";
  18. public $verp = false;
  19. public $parameter = '';
  20. public function setTo($to) {
  21. $this->to = $to;
  22. }
  23. public function setFrom($from) {
  24. $this->from = $from;
  25. }
  26. public function setSender($sender) {
  27. $this->sender = html_entity_decode($sender, ENT_QUOTES, 'UTF-8');
  28. }
  29. public function setSubject($subject) {
  30. $this->subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8');
  31. }
  32. public function setText($text) {
  33. $this->text = $text;
  34. }
  35. public function setHtml($html) {
  36. $this->html = $html;
  37. }
  38. public function addAttachment($file, $filename = '') {
  39. if (!$filename) {
  40. $filename = basename($file);
  41. }
  42. $this->attachments[] = array(
  43. 'filename' => $filename,
  44. 'file' => $file
  45. );
  46. }
  47. public function send() {
  48. if (!$this->to) {
  49. trigger_error('Error: E-Mail to required!');
  50. exit();
  51. }
  52. if (!$this->from) {
  53. trigger_error('Error: E-Mail from required!');
  54. exit();
  55. }
  56. if (!$this->sender) {
  57. trigger_error('Error: E-Mail sender required!');
  58. exit();
  59. }
  60. if (!$this->subject) {
  61. trigger_error('Error: E-Mail subject required!');
  62. exit();
  63. }
  64. if ((!$this->text) && (!$this->html)) {
  65. trigger_error('Error: E-Mail message required!');
  66. exit();
  67. }
  68. if (is_array($this->to)) {
  69. $to = implode(',', $this->to);
  70. } else {
  71. $to = $this->to;
  72. }
  73. $boundary = '----=_NextPart_' . md5(time());
  74. $header = '';
  75. $header .= 'MIME-Version: 1.0' . $this->newline;
  76. if ($this->protocol != 'mail') {
  77. $header .= 'To: ' . $to . $this->newline;
  78. $header .= 'Subject: ' . $this->subject . $this->newline;
  79. }
  80. $header .= 'Date: ' . date("D, d M Y H:i:s O") . $this->newline;
  81. $header .= 'From: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . '<' . $this->from . '>' . $this->newline;
  82. $header .= 'Reply-To: ' . $this->sender . '<' . $this->from . '>' . $this->newline;
  83. $header .= 'Return-Path: ' . $this->from . $this->newline;
  84. $header .= 'X-Mailer: PHP/' . phpversion() . $this->newline;
  85. $header .= 'Content-Type: multipart/related; boundary="' . $boundary . '"' . $this->newline;
  86. if (!$this->html) {
  87. $message = '--' . $boundary . $this->newline;
  88. $message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
  89. $message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
  90. $message .= $this->text . $this->newline;
  91. } else {
  92. $message = '--' . $boundary . $this->newline;
  93. $message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $this->newline . $this->newline;
  94. $message .= '--' . $boundary . '_alt' . $this->newline;
  95. $message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
  96. $message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
  97. if ($this->text) {
  98. $message .= $this->text . $this->newline;
  99. } else {
  100. $message .= 'This is a HTML email and your email client software does not support HTML email!' . $this->newline;
  101. }
  102. $message .= '--' . $boundary . '_alt' . $this->newline;
  103. $message .= 'Content-Type: text/html; charset="utf-8"' . $this->newline;
  104. $message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
  105. $message .= $this->html . $this->newline;
  106. $message .= '--' . $boundary . '_alt--' . $this->newline;
  107. }
  108. foreach ($this->attachments as $attachment) {
  109. if (file_exists($attachment['file'])) {
  110. $handle = fopen($attachment['file'], 'r');
  111. $content = fread($handle, filesize($attachment['file']));
  112. fclose($handle);
  113. $message .= '--' . $boundary . $this->newline;
  114. $message .= 'Content-Type: application/octetstream; name="' . basename($attachment['file']) . '"' . $this->newline;
  115. $message .= 'Content-Transfer-Encoding: base64' . $this->newline;
  116. $message .= 'Content-Disposition: attachment; filename="' . basename($attachment['filename']) . '"' . $this->newline;
  117. $message .= 'Content-ID: <' . basename($attachment['filename']) . '>' . $this->newline;
  118. $message .= 'X-Attachment-Id: ' . basename($attachment['filename']) . $this->newline . $this->newline;
  119. $message .= chunk_split(base64_encode($content));
  120. }
  121. }
  122. $message .= '--' . $boundary . '--' . $this->newline;
  123. if ($this->protocol == 'mail') {
  124. ini_set('sendmail_from', $this->from);
  125. if ($this->parameter) {
  126. mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header, $this->parameter);
  127. } else {
  128. mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header);
  129. }
  130. } elseif ($this->protocol == 'smtp') {
  131. $handle = fsockopen($this->hostname, $this->port, $errno, $errstr, $this->timeout);
  132. if (!$handle) {
  133. trigger_error('Error: ' . $errstr . ' (' . $errno . ')');
  134. exit();
  135. } else {
  136. if (substr(PHP_OS, 0, 3) != 'WIN') {
  137. socket_set_timeout($handle, $this->timeout, 0);
  138. }
  139. while ($line = fgets($handle, 515)) {
  140. if (substr($line, 3, 1) == ' ') {
  141. break;
  142. }
  143. }
  144. if (substr($this->hostname, 0, 3) == 'tls') {
  145. fputs($handle, 'STARTTLS' . $this->crlf);
  146. while ($line = fgets($handle, 515)) {
  147. $reply .= $line;
  148. if (substr($line, 3, 1) == ' ') {
  149. break;
  150. }
  151. }
  152. if (substr($reply, 0, 3) != 220) {
  153. trigger_error('Error: STARTTLS not accepted from server!');
  154. exit();
  155. }
  156. }
  157. if (!empty($this->username) && !empty($this->password)) {
  158. fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . $this->crlf);
  159. $reply = '';
  160. while ($line = fgets($handle, 515)) {
  161. $reply .= $line;
  162. if (substr($line, 3, 1) == ' ') {
  163. break;
  164. }
  165. }
  166. if (substr($reply, 0, 3) != 250) {
  167. trigger_error('Error: EHLO not accepted from server!');
  168. exit();
  169. }
  170. fputs($handle, 'AUTH LOGIN' . $this->crlf);
  171. $reply = '';
  172. while ($line = fgets($handle, 515)) {
  173. $reply .= $line;
  174. if (substr($line, 3, 1) == ' ') {
  175. break;
  176. }
  177. }
  178. if (substr($reply, 0, 3) != 334) {
  179. trigger_error('Error: AUTH LOGIN not accepted from server!');
  180. exit();
  181. }
  182. fputs($handle, base64_encode($this->username) . $this->crlf);
  183. $reply = '';
  184. while ($line = fgets($handle, 515)) {
  185. $reply .= $line;
  186. if (substr($line, 3, 1) == ' ') {
  187. break;
  188. }
  189. }
  190. if (substr($reply, 0, 3) != 334) {
  191. trigger_error('Error: Username not accepted from server!');
  192. exit();
  193. }
  194. fputs($handle, base64_encode($this->password) . $this->crlf);
  195. $reply = '';
  196. while ($line = fgets($handle, 515)) {
  197. $reply .= $line;
  198. if (substr($line, 3, 1) == ' ') {
  199. break;
  200. }
  201. }
  202. if (substr($reply, 0, 3) != 235) {
  203. trigger_error('Error: Password not accepted from server!');
  204. exit();
  205. }
  206. } else {
  207. fputs($handle, 'HELO ' . getenv('SERVER_NAME') . $this->crlf);
  208. $reply = '';
  209. while ($line = fgets($handle, 515)) {
  210. $reply .= $line;
  211. if (substr($line, 3, 1) == ' ') {
  212. break;
  213. }
  214. }
  215. if (substr($reply, 0, 3) != 250) {
  216. trigger_error('Error: HELO not accepted from server!');
  217. exit();
  218. }
  219. }
  220. if ($this->verp) {
  221. fputs($handle, 'MAIL FROM: <' . $this->from . '>XVERP' . $this->crlf);
  222. } else {
  223. fputs($handle, 'MAIL FROM: <' . $this->from . '>' . $this->crlf);
  224. }
  225. $reply = '';
  226. while ($line = fgets($handle, 515)) {
  227. $reply .= $line;
  228. if (substr($line, 3, 1) == ' ') {
  229. break;
  230. }
  231. }
  232. if (substr($reply, 0, 3) != 250) {
  233. trigger_error('Error: MAIL FROM not accepted from server!');
  234. exit();
  235. }
  236. if (!is_array($this->to)) {
  237. fputs($handle, 'RCPT TO: <' . $this->to . '>' . $this->crlf);
  238. $reply = '';
  239. while ($line = fgets($handle, 515)) {
  240. $reply .= $line;
  241. if (substr($line, 3, 1) == ' ') {
  242. break;
  243. }
  244. }
  245. if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
  246. trigger_error('Error: RCPT TO not accepted from server!');
  247. exit();
  248. }
  249. } else {
  250. foreach ($this->to as $recipient) {
  251. fputs($handle, 'RCPT TO: <' . $recipient . '>' . $this->crlf);
  252. $reply = '';
  253. while ($line = fgets($handle, 515)) {
  254. $reply .= $line;
  255. if (substr($line, 3, 1) == ' ') {
  256. break;
  257. }
  258. }
  259. if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
  260. trigger_error('Error: RCPT TO not accepted from server!');
  261. exit();
  262. }
  263. }
  264. }
  265. fputs($handle, 'DATA' . $this->crlf);
  266. $reply = '';
  267. while ($line = fgets($handle, 515)) {
  268. $reply .= $line;
  269. if (substr($line, 3, 1) == ' ') {
  270. break;
  271. }
  272. }
  273. if (substr($reply, 0, 3) != 354) {
  274. trigger_error('Error: DATA not accepted from server!');
  275. exit();
  276. }
  277. // According to rfc 821 we should not send more than 1000 including the CRLF
  278. $message = str_replace("\r\n", "\n", $header . $message);
  279. $message = str_replace("\r", "\n", $message);
  280. $lines = explode("\n", $message);
  281. foreach ($lines as $line) {
  282. $results = str_split($line, 998);
  283. foreach ($results as $result) {
  284. if (substr(PHP_OS, 0, 3) != 'WIN') {
  285. fputs($handle, $result . $this->crlf);
  286. } else {
  287. fputs($handle, str_replace("\n", "\r\n", $result) . $this->crlf);
  288. }
  289. }
  290. }
  291. fputs($handle, '.' . $this->crlf);
  292. $reply = '';
  293. while ($line = fgets($handle, 515)) {
  294. $reply .= $line;
  295. if (substr($line, 3, 1) == ' ') {
  296. break;
  297. }
  298. }
  299. if (substr($reply, 0, 3) != 250) {
  300. trigger_error('Error: DATA not accepted from server!');
  301. exit();
  302. }
  303. fputs($handle, 'QUIT' . $this->crlf);
  304. $reply = '';
  305. while ($line = fgets($handle, 515)) {
  306. $reply .= $line;
  307. if (substr($line, 3, 1) == ' ') {
  308. break;
  309. }
  310. }
  311. if (substr($reply, 0, 3) != 221) {
  312. trigger_error('Error: QUIT not accepted from server!');
  313. exit();
  314. }
  315. fclose($handle);
  316. }
  317. }
  318. }
  319. }
  320. ?>