Msg.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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('FROM txt.voice.google.com');
  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>=5){
  35. break;
  36. }else{
  37. $key="mails:".$mail_id;
  38. $temp=$cache->get($key);
  39. if (!$temp) {
  40. $detail = $mailbox->getMail($mail_id);
  41. $txt = explode("\r\n", $detail->textPlain);
  42. $temp = [
  43. 'subject' => $detail->subject,
  44. 'content' => $txt[1],
  45. 'from' => $detail->fromAddress,
  46. 'fromName' => $detail->fromName,
  47. 'replyTo' => $detail->replyTo,
  48. 'date' => $detail->date,
  49. 'cc' => $detail->cc,
  50. 'bcc' => $detail->bcc,
  51. ];
  52. $cache->set($key, $temp);
  53. }
  54. $mail_info[]=$temp;
  55. }
  56. $i++;
  57. }
  58. $this->result=$mail_info;
  59. $this->render();
  60. }
  61. }