EmailLogic.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. class EmailLogic {
  3. private $email_task;
  4. function __construct() {
  5. Doo::loadModel ( 'm/email_task' );
  6. $this->email_task = new EmailTask ();
  7. }
  8. /**
  9. * 获取邮件群发任务
  10. */
  11. function get_email_task() {
  12. $result = $this->email_task->get_email_task ();
  13. return $result;
  14. }
  15. /**
  16. * 添加邮件群发任务
  17. * @param unknown_type $data
  18. */
  19. function add_email_task($data = array()) {
  20. Doo::loadModel ( 'users' );
  21. $user = new Users ();
  22. $t = $user->getCountz ();
  23. $this->email_task->email_title = $data ['email_title'];
  24. $this->email_task->email_content = $data ['email_content'];
  25. $this->email_task->status = 1;
  26. $this->email_task->send_count = $user->getCountz ();
  27. $this->email_task->time = time ();
  28. $id = $this->db ()->insert ( $this->email_task );
  29. return $id;
  30. }
  31. /**
  32. * 获取邮件任务
  33. * @param unknown_type $tid
  34. */
  35. function get_email_task_by_tid($tid = 0) {
  36. $result = $this->email_task->get_email_task_by_tid ( $tid );
  37. Doo::loadModel ( 'users' );
  38. $user = new Users ();
  39. $limit = 30;
  40. if ($result ['is_send_count'] >= $result ['send_count'])
  41. return array ();
  42. else {
  43. if ($result ['send_count'] < $limit)
  44. $limit = $result ['send_count'];
  45. $in = $result ['send_count'] - $result ['is_send_count'];
  46. if ($in < $limit)
  47. $limit = $in;
  48. $result ['user_list'] = $user->getAll ( $result ['is_send_count'] . "," . $limit );
  49. return $result;
  50. }
  51. }
  52. /**
  53. * 获取发送失败邮件
  54. * @param unknown_type $tid
  55. */
  56. function get_faild_email_task_by_tid($tid = 0) {
  57. $result = $this->email_task->get_email_task_by_tid ( $tid );
  58. return $result;
  59. }
  60. /**
  61. * 发送邮件
  62. * @param unknown_type $email
  63. */
  64. function send_email($email = "", $title = "", $content = "", $host = "smtp.exmail.qq.com", $port = "", $username = "websupport@smartcost.com.cn", $password = "smartcost2010", $form = "websupport@smartcost.com.cn") {
  65. require_once './protected/class/class.pop3.php';
  66. require_once './protected/class/class.smtp.php';
  67. require_once './protected/class/class.phpmailer.php';
  68. $mail = new PHPMailer ();
  69. $body = stripcslashes ( $content );
  70. $mail->IsSMTP (); // telling the class to use SMTP
  71. $mail->SMTPAuth = true; // enable SMTP authentication
  72. //$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
  73. //$mail->Host = "smtp.exmail.qq.com"; // sets GMAIL as the SMTP server
  74. $mail->Host = $host;
  75. if (! empty ( $port ))
  76. $mail->Port = $port; // set the SMTP port for the GMAIL server
  77. //$mail->CharSet = "GBK";
  78. $mail->Username = $username; // GMAIL username
  79. $mail->Password = $password; // GMAIL password
  80. $mail->From = $username;
  81. $mail->FromName = "=?UTF-8?B?" . base64_encode ( '纵横知道·问答' ) . "?=";
  82. $mail->Subject = $title;
  83. $mail->MsgHTML ( $body );
  84. $mail->AddAddress ( $email );
  85. if (! $mail->Send ()) {
  86. //echo $mail->ErrorInfo;
  87. return false;
  88. } else {
  89. return true;
  90. }
  91. }
  92. /**
  93. * 删除邮件任务
  94. * @param unknown_type $tid
  95. */
  96. function delete_email_task_by_tid($tid = 0) {
  97. Doo::loadModel ( 'm/email_task' );
  98. $task = new EmailTask ();
  99. $task->tid = $tid;
  100. $this->db ()->delete ( $task );
  101. }
  102. /**
  103. * 邮件发送完成-更新状态
  104. * @param unknown_type $tid
  105. */
  106. function update_email_task_by_faild_email($tid) {
  107. Doo::loadModel ( 'm/email_task' );
  108. $task = new EmailTask ();
  109. $email = $task->get_email_task_by_tid ( $tid );
  110. $task->tid = $tid;
  111. $task->status = 3;
  112. if (empty ( $email ['faild_uid'] ))
  113. $task->status = 4;
  114. $this->db ()->update ( $task );
  115. }
  116. /**
  117. * 更新群发任务进度
  118. * @param unknown_type $tid
  119. * @param unknown_type $is_send_count
  120. */
  121. function update_email_task_by_tid($tid = 0, $is_send_count = 0, $faild_email = "") {
  122. $task = $this->email_task->get_email_task_by_tid ( $tid );
  123. $this->email_task->tid = $tid;
  124. $this->email_task->is_send_count = $is_send_count;
  125. $this->email_task->status = 2;
  126. $this->email_task->faild_uid = $faild_email . $task ['faild_uid'];
  127. $this->db ()->update ( $this->email_task );
  128. }
  129. /**
  130. * 更新邮件失败状态
  131. * @param unknown_type $tid
  132. * @param unknown_type $faild_email
  133. */
  134. function update_email_task_status_by_tid($tid = 0, $status = 4) {
  135. Doo::loadModel ( 'm/email_task' );
  136. $task = new EmailTask ();
  137. $task->tid = $tid;
  138. $task->status = $status;
  139. $this->db ()->update ( $task );
  140. }
  141. /**
  142. * 更新失败邮件
  143. * @param unknown_type $tid
  144. * @param unknown_type $faild_email
  145. */
  146. function update_faild_email_task_by_tid($tid = 0, $faild_email = "") {
  147. Doo::loadModel ( 'm/email_task' );
  148. $task = new EmailTask ();
  149. //$task_faild = $this->email_task->get_email_task_by_tid ( $tid );
  150. $task->tid = $tid;
  151. if (empty ( $faild_email ))
  152. $task->status = 4;
  153. $task->faild_uid = $faild_email;
  154. $this->db ()->update ( $task );
  155. }
  156. /**
  157. * 更新失败邮件
  158. * @param unknown_type $tid
  159. * @param unknown_type $faild_email
  160. */
  161. function de_faild_email_task_by_tid($tid = 0, $faild_email = "") {
  162. Doo::loadModel ( 'm/email_task' );
  163. $task = new EmailTask ();
  164. //$task_faild = $this->email_task->get_email_task_by_tid ( $tid );
  165. $task->tid = $tid;
  166. $task->de_email = $faild_email;
  167. $this->db ()->update ( $task );
  168. }
  169. /**
  170. * 获得分页数据
  171. * @param unknown_type $table
  172. * @param unknown_type $condition
  173. * @param unknown_type $on_page
  174. * @param unknown_type $page_size
  175. */
  176. function get_page($table = "", $condition = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  177. $page_c = "";
  178. $page ['previous'] = get_previous ( $on_page );
  179. $page ['on_page'] = $on_page;
  180. $total_count = $this->get_table_count ( $table, $condition );
  181. $total = intval ( $total_count / $page_size );
  182. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  183. $page ['total_data'] = $total_count;
  184. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  185. $i = 1;
  186. $page_max = 1;
  187. if ($on_page > 10) {
  188. $page_max = intval ( $on_page / 10 ) + 1;
  189. $i = intval ( $on_page / 10 ) * 10 - 1;
  190. }
  191. for(; $i <= $page ['total_page']; $i ++) {
  192. if ($i == $on_page) {
  193. if ($other == "page")
  194. $page_c .= '<a href="javascript:void(0);" class="current">' . $i . '</a>';
  195. else
  196. $page_c .= '&nbsp;<span class="current">' . $i . '</span>&nbsp;';
  197. } else if ($other == "page")
  198. $page_c .= '<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>';
  199. else
  200. $page_c .= '&nbsp;<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>&nbsp;';
  201. if ($i == (10 * $page_max))
  202. break;
  203. }
  204. $page ['page'] = $page_c;
  205. $page ['lower'] = (-- $on_page) * $page_size;
  206. return $page;
  207. }
  208. /**
  209. * 获取总页数
  210. * @param unknown_type $table
  211. * @param unknown_type $condition
  212. */
  213. public function get_table_count($table = "", $condition = "") {
  214. $sql = "select count(*) as count from " . $table . " where 1 " . $condition;
  215. $query = Doo::db ()->query ( $sql );
  216. $result = $query->fetch ();
  217. return $result ['count'];
  218. }
  219. /**
  220. * 获取数据
  221. * @param unknown_type $data 一般选择条件
  222. * @param unknown_type $limit
  223. * @param unknown_type $condition
  224. * @param unknown_type $table
  225. */
  226. function get_list($data = array(), $condition = "", $limit = "", $table = "") {
  227. foreach ( $data as $key => $value ) {
  228. if (is_numeric ( $value ))
  229. $condition .= " and " . $key . " = " . $value;
  230. else
  231. $condition .= " and " . $key . " like '%" . $value . "%' ";
  232. }
  233. $condition = ' where 1 ' . $condition . $limit;
  234. $sql = "select * from " . $table . $condition;
  235. $query = Doo::db ()->query ( $sql );
  236. $result = $query->fetchAll ();
  237. return $result;
  238. }
  239. function db() {
  240. return Doo::db ();
  241. }
  242. }
  243. ?>