Msg.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $num=$this->getQuery()->string("num","6288883915");
  22. $from_google=false;
  23. if ($num=="6288883915"){
  24. $mail_ids = $mailbox->searchMailbox('FROM txt.voice.google.com');
  25. $from_google=true;
  26. }else{
  27. if ($num=="6285000156"){
  28. $mail_ids = $mailbox->searchMailbox('FROM textnow.me');
  29. $from_google=false;
  30. }
  31. }
  32. // $mail_info = $mailbox->getMailsInfo($mail_ids);
  33. $mail_info=[];
  34. $mail_ids=array_reverse($mail_ids);
  35. $i=0;
  36. // $cache = new FileCache([
  37. // 'root' => APP_DIR.'/cache', // Cache root
  38. // 'ttl' => 0, // Time to live
  39. // 'compress' => false, // Compress data with gzcompress or not
  40. // 'serialize' => 'json', // How to serialize data: json, php, raw
  41. // ]);
  42. $cache=Redis::instance();
  43. foreach ($mail_ids as $mail_id) {
  44. if ($i>=5){
  45. break;
  46. }else{
  47. $key="mails:".$mail_id;
  48. $temp=$cache->get($key);
  49. unset($temp);
  50. if (!$temp) {
  51. $detail = $mailbox->getMail($mail_id);
  52. if ($from_google){
  53. $from_num=$detail->fromName;
  54. $txt = explode("\r\n", $detail->textPlain)[1];
  55. $date=$detail->date;
  56. }else{
  57. $from_num=str_replace("Message from ", "", $detail->subject);
  58. $txt=$detail->textPlain;
  59. $rwn=explode("\r\n", $detail->headersRaw);
  60. $date=explode(": ", $rwn[2])[1];
  61. }
  62. $temp = [
  63. 'subject' => $detail->subject,
  64. 'content' => $txt,
  65. 'from' => $detail->fromAddress,
  66. 'fromName' => $from_num,
  67. 'replyTo' => $detail->replyTo,
  68. 'date' =>date("Y-m-d H:i:s",strtotime($date)),
  69. 'cc' => $detail->cc,
  70. 'bcc' => $detail->bcc,
  71. ];
  72. $cache->set($key, $temp);
  73. }
  74. $mail_info[]=$temp;
  75. }
  76. $i++;
  77. }
  78. $this->result=$mail_info;
  79. $this->num=$num;
  80. $this->render();
  81. }
  82. }