PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/mall/upload/system/library/mail.php

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