sendmail.inc.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: sendmail.inc.php 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. if($mail_setting['mailsilent']) {
  9. error_reporting(0);
  10. }
  11. $maildelimiter = $mail_setting['maildelimiter'] == 1 ? "\r\n" : ($mail_setting['maildelimiter'] == 2 ? "\r" : "\n");
  12. $mailusername = isset($mail_setting['mailusername']) ? $mail_setting['mailusername'] : 1;
  13. $appname = $this->base->cache['apps'][$mail['appid']]['name'];
  14. $mail['subject'] = '=?'.$mail['charset'].'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', '['.$appname.'] '.$mail['subject']))).'?=';
  15. $mail['message'] = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $mail['message'])))))));
  16. $email_from = $mail['frommail'] == '' ? '=?'.$mail['charset'].'?B?'.base64_encode($appname)."?= <$mail_setting[maildefault]>" : (preg_match('/^(.+?) \<(.+?)\>$/',$email_from, $from) ? '=?'.$mail['charset'].'?B?'.base64_encode($from[1])."?= <$from[2]>" : $mail['frommail']);
  17. foreach(explode(',', $mail['email_to']) as $touser) {
  18. $tousers[] = preg_match('/^(.+?) \<(.+?)\>$/',$touser, $to) ? ($mailusername ? '=?'.$mail['charset'].'?B?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
  19. }
  20. $mail['email_to'] = implode(',', $tousers);
  21. $headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: Discuz! $version{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/".($mail['htmlon'] ? 'html' : 'plain')."; charset=$mail[charset]{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
  22. $mail_setting['mailport'] = $mail_setting['mailport'] ? $mail_setting['mailport'] : 25;
  23. if($mail_setting['mailsend'] == 1 && function_exists('mail')) {
  24. return @mail($mail['email_to'], $mail['subject'], $mail['message'], $headers);
  25. } elseif($mail_setting['mailsend'] == 2) {
  26. if(!$fp = fsockopen($mail_setting['mailserver'], $mail_setting['mailport'], $errno, $errstr, 30)) {
  27. return false;
  28. }
  29. stream_set_blocking($fp, true);
  30. $lastmessage = fgets($fp, 512);
  31. if(substr($lastmessage, 0, 3) != '220') {
  32. return false;
  33. }
  34. fputs($fp, ($mail_setting['mailauth'] ? 'EHLO' : 'HELO')." discuz\r\n");
  35. $lastmessage = fgets($fp, 512);
  36. if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
  37. return false;
  38. }
  39. while(1) {
  40. if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
  41. break;
  42. }
  43. $lastmessage = fgets($fp, 512);
  44. }
  45. if($mail_setting['mailauth']) {
  46. fputs($fp, "AUTH LOGIN\r\n");
  47. $lastmessage = fgets($fp, 512);
  48. if(substr($lastmessage, 0, 3) != 334) {
  49. return false;
  50. }
  51. fputs($fp, base64_encode($mail_setting['mailauth_username'])."\r\n");
  52. $lastmessage = fgets($fp, 512);
  53. if(substr($lastmessage, 0, 3) != 334) {
  54. return false;
  55. }
  56. fputs($fp, base64_encode($mail_setting['mailauth_password'])."\r\n");
  57. $lastmessage = fgets($fp, 512);
  58. if(substr($lastmessage, 0, 3) != 235) {
  59. return false;
  60. }
  61. $email_from = $mail_setting['mailfrom'];
  62. }
  63. fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
  64. $lastmessage = fgets($fp, 512);
  65. if(substr($lastmessage, 0, 3) != 250) {
  66. fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
  67. $lastmessage = fgets($fp, 512);
  68. if(substr($lastmessage, 0, 3) != 250) {
  69. return false;
  70. }
  71. }
  72. $email_tos = array();
  73. foreach(explode(',', $mail['email_to']) as $touser) {
  74. $touser = trim($touser);
  75. if($touser) {
  76. fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
  77. $lastmessage = fgets($fp, 512);
  78. if(substr($lastmessage, 0, 3) != 250) {
  79. fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
  80. $lastmessage = fgets($fp, 512);
  81. return false;
  82. }
  83. }
  84. }
  85. fputs($fp, "DATA\r\n");
  86. $lastmessage = fgets($fp, 512);
  87. if(substr($lastmessage, 0, 3) != 354) {
  88. return false;
  89. }
  90. $headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($mail['message'].microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}";
  91. fputs($fp, "Date: ".gmdate('r')."\r\n");
  92. fputs($fp, "To: ".$mail['email_to']."\r\n");
  93. fputs($fp, "Subject: ".$mail['subject']."\r\n");
  94. fputs($fp, $headers."\r\n");
  95. fputs($fp, "\r\n\r\n");
  96. fputs($fp, "$mail[message]\r\n.\r\n");
  97. $lastmessage = fgets($fp, 512);
  98. if(substr($lastmessage, 0, 3) != 250) {
  99. return false;
  100. }
  101. fputs($fp, "QUIT\r\n");
  102. return true;
  103. } elseif($mail_setting['mailsend'] == 3) {
  104. ini_set('SMTP', $mail_setting['mailserver']);
  105. ini_set('smtp_port', $mail_setting['mailport']);
  106. ini_set('sendmail_from', $email_from);
  107. return @mail($mail['email_to'], $mail['subject'], $mail['message'], $headers);
  108. }
  109. ?>