PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/system/library/mail.php

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