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