Msg.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Heanup\App\Api\Controller;
  3. use Ark\Filecache\FileCache;
  4. use Heanup\App\Api\Controller;
  5. use Heanup\Library\Redis;
  6. use PhpImap\Mailbox;
  7. class Msg extends Controller
  8. {
  9. /**
  10. * Get Method|获取数据
  11. * @return void
  12. */
  13. protected function main()
  14. {
  15. $mailbox = new Mailbox(
  16. '{mail.97admin.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
  17. "gv@a4sky.com", // Username for the before configured mailbox
  18. "6485882", // Password for the before configured username
  19. __DIR__, // Directory, where attachments will be saved (optional)
  20. 'UTF-8' // Server encoding (optional)
  21. );
  22. $mail_ids = $mailbox->searchMailbox('ALL');
  23. // $mail_info = $mailbox->getMailsInfo($mail_ids);
  24. $mail_info=[];
  25. $mail_ids=array_reverse($mail_ids);
  26. $i=0;
  27. // $cache = new FileCache([
  28. // 'root' => APP_DIR.'/cache', // Cache root
  29. // 'ttl' => 0, // Time to live
  30. // 'compress' => false, // Compress data with gzcompress or not
  31. // 'serialize' => 'json', // How to serialize data: json, php, raw
  32. // ]);
  33. $cache=Redis::instance();
  34. foreach ($mail_ids as $mail_id) {
  35. if ($i>=10){
  36. // $mailbox->deleteMail($mail_id);
  37. break;
  38. }else{
  39. $key="mails:".$mail_id;
  40. $temp=$cache->get($key);
  41. if (!$temp) {
  42. $detail = $mailbox->getMail($mail_id);
  43. $txt = explode("\r\n", $detail->textPlain);
  44. $temp = [
  45. 'subject' => $detail->subject,
  46. 'content' => $txt[1],
  47. 'from' => $detail->fromAddress,
  48. 'fromName' => $detail->fromName,
  49. 'replyTo' => $detail->replyTo,
  50. 'date' => $detail->date,
  51. 'cc' => $detail->cc,
  52. 'bcc' => $detail->bcc,
  53. ];
  54. $cache->set($key, $temp);
  55. }
  56. $mail_info[]=$temp;
  57. }
  58. $i++;
  59. }
  60. $this->result=$mail_info;
  61. $this->render();
  62. }
  63. }