Msg.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Heanup\App\Api\Controller;
  3. use Ark\Filecache\FileCache;
  4. use Heanup\App\Api\RestfulController;
  5. use PhpImap\Exceptions\InvalidParameterException;
  6. use PhpImap\Mailbox;
  7. class Msg extends RestfulController
  8. {
  9. /**
  10. * Get Method|获取数据
  11. * @return void
  12. * @throws InvalidParameterException
  13. */
  14. protected function getData()
  15. {
  16. $mailbox = new Mailbox(
  17. '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
  18. "chen6485882@gmail.com", // Username for the before configured mailbox
  19. "chendeben6485882", // Password for the before configured username
  20. __DIR__, // Directory, where attachments will be saved (optional)
  21. 'UTF-8' // Server encoding (optional)
  22. );
  23. $mail_ids = $mailbox->searchMailbox('FROM txt.voice.google.com');
  24. // $mail_info = $mailbox->getMailsInfo($mail_ids);
  25. $mail_info=[];
  26. $mail_ids=array_reverse($mail_ids);
  27. $i=0;
  28. $cache = new FileCache([
  29. 'root' => APP_DIR.'/cache', // Cache root
  30. 'ttl' => 0, // Time to live
  31. 'compress' => false, // Compress data with gzcompress or not
  32. 'serialize' => 'json', // How to serialize data: json, php, raw
  33. ]);
  34. foreach ($mail_ids as $mail_id) {
  35. if ($i>=3){
  36. break;
  37. }
  38. $i++;
  39. $detail=$cache->get($mail_id);
  40. if (!$detail){
  41. $detail = $mailbox->getMail($mail_id);
  42. $cache->set($mail_id, $detail);
  43. }
  44. $txt=explode("\r\n", $detail->textPlain);
  45. $temp=[
  46. 'subject' => $detail->subject,
  47. 'content' => $txt[1],
  48. 'from' => $detail->fromAddress,
  49. 'fromName' => $detail->fromName,
  50. 'replyTo' => $detail->replyTo,
  51. 'date' => $detail->date,
  52. 'cc' => $detail->cc,
  53. 'bcc' => $detail->bcc,
  54. ];
  55. $mail_info[]=$temp;
  56. }
  57. $this->result=$mail_info;
  58. $this->render();
  59. }
  60. /**
  61. * Post Method|新增数据
  62. * @return void
  63. */
  64. protected function postData()
  65. {
  66. }
  67. /**
  68. * Put Method|更新数据
  69. * @return void
  70. */
  71. protected function putData()
  72. {
  73. }
  74. /**
  75. * Delete Method|删除数据
  76. * @return void
  77. */
  78. protected function deleteData()
  79. {
  80. }
  81. }