email.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. class smtp
  3. {
  4. public $smtp_port;
  5. public $time_out;
  6. public $host_name;
  7. public $log_file;
  8. public $relay_host;
  9. public $debug;
  10. public $auth;
  11. public $user;
  12. public $pass;
  13. public $sock;
  14. public function smtp($relay_host = '', $smtp_port = 25, $auth = false, $user, $pass)
  15. {
  16. $this->debug = false;
  17. $this->smtp_port = $smtp_port;
  18. $this->relay_host = $relay_host;
  19. $this->time_out = 30;
  20. $this->auth = $auth;
  21. $this->user = $user;
  22. $this->pass = $pass;
  23. $this->host_name = 'localhost';
  24. $this->log_file = '';
  25. $this->sock = false;
  26. }
  27. public function sendmail($to, $from, $subject = '', $body = '', $mailtype, $cc = '', $bcc = '', $additional_headers = '')
  28. {
  29. $mail_from = $this->get_address($this->strip_comment($from));
  30. $body = @ereg_replace('(^|(
  31. ))(\\.)', '\\1.\\3', $body);
  32. $header .= 'MIME-Version:1.0
  33. ';
  34. if ($mailtype == 'HTML') {
  35. $header .= 'Content-Type:text/html
  36. ';
  37. }
  38. $header .= 'To: ' . $to . '
  39. ';
  40. if ($cc != '') {
  41. $header .= 'Cc: ' . $cc . '
  42. ';
  43. }
  44. $header .= 'From: ' . $from . '<' . $from . '>
  45. ';
  46. $header .= 'Subject: ' . $subject . '
  47. ';
  48. $header .= $additional_headers;
  49. $header .= 'Date: ' . date('r') . '
  50. ';
  51. $header .= 'X-Mailer:By Redhat (PHP/' . phpversion() . ')
  52. ';
  53. list($msec, $sec) = explode(' ', microtime());
  54. $header .= 'Message-ID: <' . date('YmdHis', $sec) . '.' . $msec * 1000000 . '.' . $mail_from . '>
  55. ';
  56. $TO = explode(',', $this->strip_comment($to));
  57. if ($cc != '') {
  58. $TO = array_merge($TO, explode(',', $this->strip_comment($cc)));
  59. }
  60. if ($bcc != '') {
  61. $TO = array_merge($TO, explode(',', $this->strip_comment($bcc)));
  62. }
  63. $sent = true;
  64. foreach ($TO as $rcpt_to) {
  65. $rcpt_to = $this->get_address($rcpt_to);
  66. if (!$this->smtp_sockopen($rcpt_to)) {
  67. $this->log_write('Error: Cannot send email to ' . $rcpt_to . '
  68. ');
  69. $sent = false;
  70. continue;
  71. }
  72. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  73. $this->log_write('E-mail has been sent to <' . $rcpt_to . '>
  74. ');
  75. }
  76. else {
  77. $this->log_write('Error: Cannot send email to <' . $rcpt_to . '>
  78. ');
  79. $sent = false;
  80. }
  81. fclose($this->sock);
  82. $this->log_write('Disconnected from remote host
  83. ');
  84. }
  85. return $sent;
  86. }
  87. public function smtp_send($helo, $from, $to, $header, $body = '')
  88. {
  89. if (!$this->smtp_putcmd('HELO', $helo)) {
  90. return $this->smtp_error('sending HELO command');
  91. }
  92. if ($this->auth) {
  93. if (!$this->smtp_putcmd('AUTH LOGIN', base64_encode($this->user))) {
  94. return $this->smtp_error('sending HELO command');
  95. }
  96. if (!$this->smtp_putcmd('', base64_encode($this->pass))) {
  97. return $this->smtp_error('sending HELO command');
  98. }
  99. }
  100. if (!$this->smtp_putcmd('MAIL', 'FROM:<' . $from . '>')) {
  101. return $this->smtp_error('sending MAIL FROM command');
  102. }
  103. if (!$this->smtp_putcmd('RCPT', 'TO:<' . $to . '>')) {
  104. return $this->smtp_error('sending RCPT TO command');
  105. }
  106. if (!$this->smtp_putcmd('DATA')) {
  107. return $this->smtp_error('sending DATA command');
  108. }
  109. if (!$this->smtp_message($header, $body)) {
  110. return $this->smtp_error('sending message');
  111. }
  112. if (!$this->smtp_eom()) {
  113. return $this->smtp_error('sending <CR><LF>.<CR><LF> [EOM]');
  114. }
  115. if (!$this->smtp_putcmd('QUIT')) {
  116. return $this->smtp_error('sending QUIT command');
  117. }
  118. return true;
  119. }
  120. public function smtp_sockopen($address)
  121. {
  122. if ($this->relay_host == '') {
  123. return $this->smtp_sockopen_mx($address);
  124. }
  125. else {
  126. return $this->smtp_sockopen_relay();
  127. }
  128. }
  129. public function smtp_sockopen_relay()
  130. {
  131. $this->log_write('Trying to ' . $this->relay_host . ':' . $this->smtp_port . '
  132. ');
  133. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  134. if (!($this->sock && $this->smtp_ok())) {
  135. $this->log_write('Error: Cannot connenct to relay host ' . $this->relay_host . '
  136. ');
  137. $this->log_write('Error: ' . $errstr . ' (' . $errno . ')
  138. ');
  139. return false;
  140. }
  141. $this->log_write('Connected to relay host ' . $this->relay_host . '
  142. ');
  143. return true;
  144. }
  145. public function smtp_sockopen_mx($address)
  146. {
  147. $domain = @ereg_replace('^.+@([^@]+)$', '\\1', $address);
  148. if (!@getmxrr($domain, $MXHOSTS)) {
  149. $this->log_write('Error: Cannot resolve MX "' . $domain . '"
  150. ');
  151. return false;
  152. }
  153. foreach ($MXHOSTS as $host) {
  154. $this->log_write('Trying to ' . $host . ':' . $this->smtp_port . '
  155. ');
  156. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  157. if (!($this->sock && $this->smtp_ok())) {
  158. $this->log_write('Warning: Cannot connect to mx host ' . $host . '
  159. ');
  160. $this->log_write('Error: ' . $errstr . ' (' . $errno . ')
  161. ');
  162. continue;
  163. }
  164. $this->log_write('Connected to mx host ' . $host . '
  165. ');
  166. return true;
  167. }
  168. $this->log_write('Error: Cannot connect to any mx hosts (' . implode(', ', $MXHOSTS) . ')
  169. ');
  170. return false;
  171. }
  172. public function smtp_message($header, $body)
  173. {
  174. fputs($this->sock, $header . '
  175. ' . $body);
  176. $this->smtp_debug('> ' . str_replace('
  177. ', '
  178. ' . '> ', $header . '
  179. > ' . $body . '
  180. > '));
  181. return true;
  182. }
  183. public function smtp_eom()
  184. {
  185. fputs($this->sock, '
  186. .
  187. ');
  188. $this->smtp_debug('. [EOM]
  189. ');
  190. return $this->smtp_ok();
  191. }
  192. public function smtp_ok()
  193. {
  194. $response = str_replace('
  195. ', '', fgets($this->sock, 512));
  196. $this->smtp_debug($response . '
  197. ');
  198. if (!@ereg('^[23]', $response)) {
  199. fputs($this->sock, 'QUIT
  200. ');
  201. fgets($this->sock, 512);
  202. $this->log_write('Error: Remote host returned "' . $response . '"
  203. ');
  204. return false;
  205. }
  206. return true;
  207. }
  208. public function smtp_putcmd($cmd, $arg = '')
  209. {
  210. if ($arg != '') {
  211. if ($cmd == '') {
  212. $cmd = $arg;
  213. }
  214. else {
  215. $cmd = $cmd . ' ' . $arg;
  216. }
  217. }
  218. fputs($this->sock, $cmd . '
  219. ');
  220. $this->smtp_debug('> ' . $cmd . '
  221. ');
  222. return $this->smtp_ok();
  223. }
  224. public function smtp_error($string)
  225. {
  226. $this->log_write('Error: Error occurred while ' . $string . '.
  227. ');
  228. return false;
  229. }
  230. public function log_write($message)
  231. {
  232. $this->smtp_debug($message);
  233. if ($this->log_file == '') {
  234. return true;
  235. }
  236. $message = date('M d H:i:s ') . get_current_user() . '[' . getmypid() . ']: ' . $message;
  237. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, 'a'))) {
  238. $this->smtp_debug('Warning: Cannot open log file "' . $this->log_file . '"
  239. ');
  240. return false;
  241. }
  242. flock($fp, LOCK_EX);
  243. fputs($fp, $message);
  244. fclose($fp);
  245. return true;
  246. }
  247. public function strip_comment($address)
  248. {
  249. $comment = '\\([^()]*\\)';
  250. while (@ereg($comment, $address)) {
  251. $address = @ereg_replace($comment, '', $address);
  252. }
  253. return $address;
  254. }
  255. public function get_address($address)
  256. {
  257. $address = @ereg_replace('([
  258. ])+', '', $address);
  259. $address = @ereg_replace('^.*<(.+)>.*$', '\\1', $address);
  260. return $address;
  261. }
  262. public function smtp_debug($message)
  263. {
  264. if ($this->debug) {
  265. echo $message . '<br>';
  266. }
  267. }
  268. public function get_attach_type($image_tag)
  269. {
  270. $filedata = array();
  271. $img_file_con = fopen($image_tag, 'r');
  272. unset($image_data);
  273. while ($tem_buffer = AddSlashes(fread($img_file_con, filesize($image_tag)))) {
  274. $image_data .= $tem_buffer;
  275. }
  276. fclose($img_file_con);
  277. $filedata['context'] = $image_data;
  278. $filedata['filename'] = basename($image_tag);
  279. $extension = substr($image_tag, strrpos($image_tag, '.'), strlen($image_tag) - strrpos($image_tag, '.'));
  280. switch ($extension) {
  281. case '.gif':
  282. $filedata['type'] = 'image/gif';
  283. break;
  284. case '.gz':
  285. $filedata['type'] = 'application/x-gzip';
  286. break;
  287. case '.htm':
  288. $filedata['type'] = 'text/html';
  289. break;
  290. case '.html':
  291. $filedata['type'] = 'text/html';
  292. break;
  293. case '.jpg':
  294. $filedata['type'] = 'image/jpeg';
  295. break;
  296. case '.tar':
  297. $filedata['type'] = 'application/x-tar';
  298. break;
  299. case '.txt':
  300. $filedata['type'] = 'text/plain';
  301. break;
  302. case '.zip':
  303. $filedata['type'] = 'application/zip';
  304. break;
  305. default:
  306. $filedata['type'] = 'application/octet-stream';
  307. break;
  308. }
  309. return $filedata;
  310. }
  311. }
  312. !defined('IN_MYMPS') && exit('FORBIDDEN');
  313. ?>