· 6 years ago · Sep 13, 2019, 06:28 PM
1<?php
2
3use Illuminate\Console\Command;
4use Symfony\Component\Console\Input\InputOption;
5use Symfony\Component\Console\Input\InputArgument;
6
7class MailCheck extends Command {
8
9 /**
10 * The console command name.
11 *
12 * @var string
13 */
14 protected $name = 'mail:check';
15
16 /**
17 * The console command description.
18 *
19 * @var string
20 */
21 protected $description = 'Check IMAP mails and sync.';
22
23 /**
24 * Create a new command instance.
25 *
26 * @return void
27 */
28 public function __construct()
29 {
30 parent::__construct();
31 }
32
33 /**
34 * Execute the console command.
35 *
36 * @return mixed
37 */
38 public function fire()
39 {
40 $mailbox = new PhpImap\Mailbox('{imap.gmail.com:993/imap/ssl}INBOX', '*****@gmail.com', '***********', public_path()."/upload/mail-attachments/");
41
42 $mailIds = $mailbox->searchMailBox('ALL');
43 if(!$mailIds) {
44 $this->info("No mail!");
45 } else {
46 foreach($mailIds as $mailId) {
47 if(MailArchive::where("mid", $mailId)->count() == 0) {
48 $getMail = $mailbox->getMail($mailId);
49
50 $tos = [];
51 foreach($getMail->to as $toMail => $toName) {
52 $tos[] = $toName."(".$toMail.")";
53 }
54 $ccs = [];
55 foreach($getMail->cc as $ccMail => $ccName) {
56 $ccs[] = $ccName."(".$ccMail.")";
57 }
58 $replys = [];
59 foreach($getMail->replyTo as $replyMail => $replyName) {
60 $replys[] = $replyName."(".$replyMail.")";
61 }
62
63 $mail = new MailArchive;
64 $mail->mid = $getMail->id;
65 $mail->date = $getMail->date;
66 $mail->subject = $getMail->subject;
67 $mail->fromName = $getMail->fromName;
68 $mail->fromAddress = $getMail->fromAddress;
69 $mail->to = implode(",", $tos);
70 $mail->cc = implode(",", $ccs);
71 $mail->reply = implode(",", $replys);
72 $mail->plain = $getMail->textPlain;
73 $mail->html = $getMail->textHtml;
74 $mail->save();
75
76 foreach($getMail->getAttachments() as $getAttachment) {
77 $attachment = new MailAttachment();
78 $attachment->aid = $getAttachment->id;
79 $attachment->name = $getAttachment->name;
80 $attachment->path = $getAttachment->filePath;
81 $attachment->mail_id = $mail->id;
82 $attachment->save();
83 }
84
85 $this->info("New Mail:".$mail->subject);
86 }
87 }
88 }
89 }
90
91 /**
92 * Get the console command arguments.
93 *
94 * @return array
95 */
96 protected function getArguments()
97 {
98 return array(
99 );
100 }
101
102 /**
103 * Get the console command options.
104 *
105 * @return array
106 */
107 protected function getOptions()
108 {
109 return array(
110 );
111 }
112
113}