zen.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * The zen file of mail module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yidong Wang <yidong@easycorp.ltd>
  8. * @package mail
  9. * @link https://www.zentao.net
  10. */
  11. class mailZen extends mail
  12. {
  13. /**
  14. * 获取编辑页面的邮箱配置。
  15. * Get mail config for edit.
  16. *
  17. * @access protected
  18. * @return object|false
  19. */
  20. protected function getConfigForEdit()
  21. {
  22. $mailConfig = '';
  23. if($this->session->mailConfig) $mailConfig = $this->session->mailConfig;
  24. if($this->config->mail->turnon)
  25. {
  26. $mailConfig = $this->config->mail->smtp;
  27. $mailConfig->fromAddress = $this->config->mail->fromAddress;
  28. $mailConfig->fromName = $this->config->mail->fromName;
  29. $mailConfig->charset = zget($mailConfig, 'charset', 'utf-8');
  30. }
  31. if(empty($mailConfig) || !is_object($mailConfig)) return false;
  32. $mailConfig->domain = isset($this->config->mail->domain) ? $this->config->mail->domain : common::getSysURL();
  33. return $mailConfig;
  34. }
  35. /**
  36. * Get mail config for save.
  37. *
  38. * @access protected
  39. * @return object
  40. */
  41. protected function getConfigForSave()
  42. {
  43. $mailConfig = new stdclass();
  44. $mailConfig->smtp = new stdclass();
  45. $mailConfig->turnon = $this->post->turnon;
  46. $mailConfig->mta = 'smtp';
  47. $mailConfig->async = $this->post->async;
  48. $mailConfig->fromAddress = trim($this->post->fromAddress);
  49. $mailConfig->fromName = trim($this->post->fromName);
  50. $mailConfig->domain = trim($this->post->domain);
  51. $mailConfig->smtp->host = trim($this->post->host);
  52. $mailConfig->smtp->port = trim($this->post->port);
  53. $mailConfig->smtp->auth = $this->post->auth;
  54. $mailConfig->smtp->username = trim($this->post->username);
  55. $mailConfig->smtp->password = $this->post->password;
  56. $mailConfig->smtp->secure = $this->post->secure;
  57. $mailConfig->smtp->debug = $this->post->debug;
  58. $mailConfig->smtp->charset = $this->post->charset;
  59. return $mailConfig;
  60. }
  61. /**
  62. * Get has mail user pairs.
  63. *
  64. * @access protected
  65. * @return array
  66. */
  67. protected function getHasMailUserPairs()
  68. {
  69. $users = $this->dao->select('*')->from(TABLE_USER)->where('email')->ne('')->andWhere('deleted')->eq(0)->orderBy('account')->fetchAll();
  70. $userPairs = array();
  71. foreach($users as $user) $userPairs[$user->account] = $user->realname . ' ' . $user->email;
  72. return $userPairs;
  73. }
  74. /**
  75. * Send a queue.
  76. *
  77. * @param object $queue
  78. * @access protected
  79. * @return array|false
  80. * @param bool $includeMe
  81. */
  82. protected function sendQueue($queue, $includeMe = false)
  83. {
  84. $now = helper::now();
  85. $log = '';
  86. $mailStatus = 'wait';
  87. if(!isset($queue->merge) or $queue->merge == false) $mailStatus = $this->dao->select('*')->from(TABLE_NOTIFY)->where('id')->eq($queue->id)->fetch('status');
  88. if(empty($mailStatus) or $mailStatus != 'wait') return false;
  89. $this->dao->update(TABLE_NOTIFY)->set('status')->eq('sending')->where('id')->in($queue->id)->exec();
  90. $this->mail->send($queue->toList, $queue->subject, $queue->data, $queue->ccList, $includeMe);
  91. $data = new stdclass();
  92. $data->sendTime = $now;
  93. $data->status = 'sended';
  94. if($this->mail->isError())
  95. {
  96. $data->status = 'fail';
  97. $data->failReason = implode("\n", $this->mail->getError());
  98. }
  99. $this->dao->update(TABLE_NOTIFY)->data($data)->where('id')->in($queue->id)->exec();
  100. $log .= "Send #$queue->id result is {$data->status}\n";
  101. if($data->status == 'fail') $log .= "reason is $data->failReason\n";
  102. return array('result' => $data->status == 'fail' ? 'fail' : 'success', 'message' => $log);
  103. }
  104. /**
  105. * Delete sent queue.
  106. *
  107. * @access protected
  108. * @return void
  109. */
  110. protected function deleteSentQueue()
  111. {
  112. $lastMail = $this->dao->select('id,status')->from(TABLE_NOTIFY)->where('objectType')->eq('mail')->orderBy('id_desc')->limit(1)->fetch();
  113. if(!empty($lastMail) and $lastMail->id > 1000000)
  114. {
  115. $unSendNum = $this->dao->select('count(id) as count')->from(TABLE_NOTIFY)->where('status')->eq('wait')->fetch('count');
  116. if($unSendNum == 0) $this->dao->exec('TRUNCATE table ' . TABLE_NOTIFY);
  117. }
  118. /* Delete two days ago queues. */
  119. $this->dao->delete()->from(TABLE_NOTIFY)->where('status')->eq('sended')->andWhere('sendTime')->le(date('Y-m-d H:i:s', time() - 2 * 24 * 3600))->exec();
  120. }
  121. }