mail.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: mail.php 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_MAIL_REPEAT', 5);
  9. class mailmodel {
  10. var $db;
  11. var $base;
  12. var $apps;
  13. function __construct(&$base) {
  14. $this->mailmodel($base);
  15. }
  16. function mailmodel(&$base) {
  17. $this->base = $base;
  18. $this->db = $base->db;
  19. $this->apps = &$this->base->cache['apps'];
  20. }
  21. function get_total_num() {
  22. $data = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."mailqueue");
  23. return $data;
  24. }
  25. function get_list($page, $ppp, $totalnum) {
  26. $start = $this->base->page_get_start($page, $ppp, $totalnum);
  27. $data = $this->db->fetch_all("SELECT m.*, u.username, u.email FROM ".UC_DBTABLEPRE."mailqueue m LEFT JOIN ".UC_DBTABLEPRE."members u ON m.touid=u.uid ORDER BY dateline DESC LIMIT $start, $ppp");
  28. foreach((array)$data as $k => $v) {
  29. $data[$k]['subject'] = htmlspecialchars($v['subject']);
  30. $data[$k]['tomail'] = empty($v['tomail']) ? $v['email'] : $v['tomail'];
  31. $data[$k]['dateline'] = $v['dateline'] ? $this->base->date($data[$k]['dateline']) : '';
  32. $data[$k]['appname'] = $this->base->cache['apps'][$v['appid']]['name'];
  33. }
  34. return $data;
  35. }
  36. function delete_mail($ids) {
  37. $ids = $this->base->implode($ids);
  38. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mailqueue WHERE mailid IN ($ids)");
  39. return $this->db->affected_rows();
  40. }
  41. function add($mail) {
  42. if($mail['level']) {
  43. $sql = "INSERT INTO ".UC_DBTABLEPRE."mailqueue (touid, tomail, subject, message, frommail, charset, htmlon, level, dateline, failures, appid) VALUES ";
  44. $values_arr = array();
  45. foreach($mail['uids'] as $uid) {
  46. if(empty($uid)) continue;
  47. $values_arr[] = "('$uid', '', '$mail[subject]', '$mail[message]', '$mail[frommail]', '$mail[charset]', '$mail[htmlon]', '$mail[level]', '$mail[dateline]', '0', '$mail[appid]')";
  48. }
  49. foreach($mail['emails'] as $email) {
  50. if(empty($email)) continue;
  51. $values_arr[] = "('', '$email', '$mail[subject]', '$mail[message]', '$mail[frommail]', '$mail[charset]', '$mail[htmlon]', '$mail[level]', '$mail[dateline]', '0', '$mail[appid]')";
  52. }
  53. $sql .= implode(',', $values_arr);
  54. $this->db->query($sql);
  55. $insert_id = $this->db->insert_id();
  56. $insert_id && $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='mailexists', value='1'");
  57. return $insert_id;
  58. } else {
  59. $mail['email_to'] = array();
  60. $uids = 0;
  61. foreach($mail['uids'] as $uid) {
  62. if(empty($uid)) continue;
  63. $uids .= ','.$uid;
  64. }
  65. $users = $this->db->fetch_all("SELECT uid, username, email FROM ".UC_DBTABLEPRE."members WHERE uid IN ($uids)");
  66. foreach($users as $v) {
  67. $mail['email_to'][] = $v['username'].'<'.$v['email'].'>';
  68. }
  69. foreach($mail['emails'] as $email) {
  70. if(empty($email)) continue;
  71. $mail['email_to'][] = $email;
  72. }
  73. $mail['message'] = str_replace('\"', '"', $mail['message']);
  74. $mail['email_to'] = implode(',', $mail['email_to']);
  75. return $this->send_one_mail($mail);
  76. }
  77. }
  78. function send() {
  79. register_shutdown_function(array($this, '_send'));
  80. }
  81. function _send() {
  82. $mail = $this->_get_mail();
  83. if(empty($mail)) {
  84. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='mailexists', value='0'");
  85. return NULL;
  86. } else {
  87. $mail['email_to'] = $mail['tomail'] ? $mail['tomail'] : $mail['username'].'<'.$mail['email'].'>';
  88. if($this->send_one_mail($mail)) {
  89. $this->_delete_one_mail($mail['mailid']);
  90. return true;
  91. } else {
  92. $this->_update_failures($mail['mailid']);
  93. return false;
  94. }
  95. }
  96. }
  97. function send_by_id($mailid) {
  98. if ($this->send_one_mail($this->_get_mail_by_id($mailid))) {
  99. $this->_delete_one_mail($mailid);
  100. return true;
  101. }
  102. }
  103. function send_one_mail($mail) {
  104. if(empty($mail)) return;
  105. $mail['email_to'] = $mail['email_to'] ? $mail['email_to'] : $mail['username'].'<'.$mail['email'].'>';
  106. $mail_setting = $this->base->settings;
  107. return include UC_ROOT.'lib/sendmail.inc.php';
  108. }
  109. function _get_mail() {
  110. $data = $this->db->fetch_first("SELECT m.*, u.username, u.email FROM ".UC_DBTABLEPRE."mailqueue m LEFT JOIN ".UC_DBTABLEPRE."members u ON m.touid=u.uid WHERE failures<'".UC_MAIL_REPEAT."' ORDER BY level DESC, mailid ASC LIMIT 1");
  111. return $data;
  112. }
  113. function _get_mail_by_id($mailid) {
  114. $data = $this->db->fetch_first("SELECT m.*, u.username, u.email FROM ".UC_DBTABLEPRE."mailqueue m LEFT JOIN ".UC_DBTABLEPRE."members u ON m.touid=u.uid WHERE mailid='$mailid'");
  115. return $data;
  116. }
  117. function _delete_one_mail($mailid) {
  118. $mailid = intval($mailid);
  119. return $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mailqueue WHERE mailid='$mailid'");
  120. }
  121. function _update_failures($mailid) {
  122. $mailid = intval($mailid);
  123. return $this->db->query("UPDATE ".UC_DBTABLEPRE."mailqueue SET failures=failures+1 WHERE mailid='$mailid'");
  124. }
  125. }
  126. ?>