Msg.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Heanup\App\Api\Controller;
  3. use Ark\Filecache\FileCache;
  4. use Heanup\App\Api\Controller;
  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. foreach ($mail_ids as $mail_id) {
  33. if ($i>=10){
  34. // $mailbox->deleteMail($mail_id);
  35. break;
  36. }else{
  37. $temp=$cache->get($mail_id);
  38. if (!$temp) {
  39. $detail = $mailbox->getMail($mail_id);
  40. $txt = explode("\r\n", $detail->textPlain);
  41. $temp = [
  42. 'subject' => $detail->subject,
  43. 'content' => $txt[1],
  44. 'from' => $detail->fromAddress,
  45. 'fromName' => $detail->fromName,
  46. 'replyTo' => $detail->replyTo,
  47. 'date' => $detail->date,
  48. 'cc' => $detail->cc,
  49. 'bcc' => $detail->bcc,
  50. ];
  51. $cache->set($mail_id, $temp);
  52. }
  53. $mail_info[]=$temp;
  54. }
  55. $i++;
  56. }
  57. $this->result=$mail_info;
  58. $this->render();
  59. }
  60. }