Msg.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }else{
  55. $from_num=str_replace("Message from ", "", $detail->subject);
  56. $txt=$detail->textPlain;
  57. }
  58. $temp = [
  59. 'subject' => $detail->subject,
  60. 'content' => $txt,
  61. 'from' => $detail->fromAddress,
  62. 'fromName' => $from_num,
  63. 'replyTo' => $detail->replyTo,
  64. 'date' =>$detail->date,
  65. 'cc' => $detail->cc,
  66. 'bcc' => $detail->bcc,
  67. ];
  68. $cache->set($key, $temp);
  69. }
  70. $mail_info[]=$temp;
  71. }
  72. $i++;
  73. }
  74. $this->result=$mail_info;
  75. $this->num=$num;
  76. $this->render();
  77. }
  78. }