Msg.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (!$temp) {
  50. $detail = $mailbox->getMail($mail_id);
  51. if ($from_google){
  52. $from_num=$detail->fromName;
  53. $txt = explode("\r\n", $detail->textPlain)[1];
  54. $date=$detail->date;
  55. }else{
  56. $from_num=str_replace("Message from ", "", $detail->subject);
  57. $txt=$detail->textPlain;
  58. $rwn=explode("\r\n", $detail->headersRaw);
  59. $date=explode(": ", $rwn[2])[1];
  60. }
  61. $temp = [
  62. 'subject' => $detail->subject,
  63. 'content' => $txt,
  64. 'from' => $detail->fromAddress,
  65. 'fromName' => $from_num,
  66. 'replyTo' => $detail->replyTo,
  67. 'date' =>date("Y-m-d H:i:s",strtotime($date)),
  68. 'cc' => $detail->cc,
  69. 'bcc' => $detail->bcc,
  70. ];
  71. $cache->set($key, $temp);
  72. }
  73. $mail_info[]=$temp;
  74. }
  75. $i++;
  76. }
  77. $this->result=$mail_info;
  78. $this->num=$num;
  79. $this->render();
  80. }
  81. }