Domain.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Heanup\App\Api\Controller\Vestacp;
  3. use Heanup\App\Api\Package\Database\Mail;
  4. use Heanup\App\Api\RestfulController;
  5. use Heanup\Library\Vestacp;
  6. use PhpImap\Exceptions\InvalidParameterException;
  7. use PhpImap\Mailbox;
  8. use ProfiCloS\VestaCP\ClientException;
  9. use ProfiCloS\VestaCP\ProcessException;
  10. class Domain extends RestfulController
  11. {
  12. /**
  13. * @var $this
  14. */
  15. public $_data;
  16. /**
  17. * @var $this
  18. */
  19. public $email;
  20. /**
  21. * @var array
  22. */
  23. public $mail_ids;
  24. /**
  25. * @var array
  26. */
  27. public $info;
  28. /**
  29. * Get Method|获取数据
  30. * @return void
  31. * @throws InvalidParameterException
  32. */
  33. protected function getData()
  34. {
  35. $type = $this->getRequest()->string("type", 'default');
  36. $this->_data = $_SESSION['data'];
  37. $login = $this->_data['email'] . "@" . $this->_data['domain'];
  38. $password = $this->_data['password'];
  39. $this->email = $this->getQuery()->string("email");
  40. // $this->showSuccess([$login,$password]);
  41. if ($this->email) {
  42. $login = $this->email;
  43. $account = substr($this->email, 0, strpos($this->email, "@"));
  44. $domain = substr($this->email, strpos($this->email, "@") + 1);
  45. $_mail = Mail::getOne(['email' => $account, 'domain' => $domain, 'status' => 1]);
  46. $password = $_mail['password'];
  47. $this->_data=$_mail;
  48. }
  49. if ($type == "get_mail") {
  50. $mailbox = new Mailbox(
  51. '{mail.97admin.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
  52. $login, // Username for the before configured mailbox
  53. $password, // Password for the before configured username
  54. __DIR__, // Directory, where attachments will be saved (optional)
  55. 'UTF-8' // Server encoding (optional)
  56. );
  57. $mail_ids = $mailbox->searchMailbox('ALL');
  58. $mail_info = $mailbox->getMailsInfo($mail_ids);
  59. $mail_info = array_reverse($mail_info);
  60. $this->showSuccess($mail_info);
  61. } elseif ($type == 'get_detail') {
  62. $mailbox = new Mailbox(
  63. '{mail.97admin.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
  64. $login, // Username for the before configured mailbox
  65. $password, // Password for the before configured username
  66. __DIR__, // Directory, where attachments will be saved (optional)
  67. 'UTF-8' // Server encoding (optional)
  68. );
  69. $mail_id = $this->getQuery()->intval("id");
  70. $detail = $mailbox->getMail($mail_id);
  71. $this->info = [
  72. 'subject' => $detail->subject,
  73. 'body' => $detail->textHtml,
  74. 'from' => $detail->fromAddress,
  75. 'fromName' => $detail->fromName,
  76. 'replyTo' => $detail->replyTo,
  77. 'date' => $detail->date,
  78. 'cc' => $detail->cc,
  79. 'bcc' => $detail->bcc,
  80. ];
  81. $this->getTemplate()->setTpl("Detail");
  82. $this->render();
  83. die();
  84. }
  85. // $this->showSuccess($this->_data);
  86. $this->getTemplate()->setTpl("Domain");
  87. $this->render();
  88. }
  89. /**
  90. * 新增一个临时邮箱
  91. * @return void
  92. */
  93. protected function postData()
  94. {
  95. $domain = $this->getRequest()->string("domain", 'a4sky.com');
  96. $email = $this->getRequest()->string("email");
  97. $password = md5(uniqid());
  98. $data = compact("domain", "email", "password");
  99. $data['expired_time'] = date("Y-m-d H:i:s", time() + 7200);
  100. $data['add_time'] = date("Y-m-d H:i:s");
  101. $exist = Mail::getOne(['email' => $email, 'domain' => $domain, 'status' => 1]);
  102. $this->getTemplate()->setTpl("Domain");
  103. $this->_data = $_SESSION['data'] = $data;
  104. if (!$exist) {
  105. try {
  106. $result = Vestacp::getInstance()->getModuleMail("admin")->addAccount($domain, $email, $password);
  107. if ($result) {
  108. Mail::insert($data);
  109. $this->render();
  110. }
  111. } catch (ClientException $e) {
  112. $this->showError(400, $e->getMessage());
  113. } catch (ProcessException $e) {
  114. $this->showError(400, $e->getMessage());
  115. }
  116. }
  117. $this->render();
  118. }
  119. /**
  120. * Put Method|更新数据
  121. * @return void
  122. */
  123. protected function putData()
  124. {
  125. }
  126. /**
  127. * Delete Method|删除数据
  128. * @return void
  129. */
  130. protected function deleteData()
  131. {
  132. }
  133. }