Msg.php 2.3 KB

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