/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, $ticket)
 {
     $this->validate($request, ['response' => 'required']);
     $response = TicketResponse::create(['user_id' => $this->my_id, 'response' => $request->response, 'source' => 'web', 'ticket_id' => $ticket]);
     if (isset($request->attachments)) {
         foreach ($request->attachments as $attachment) {
             // Check that the directory exists
             $uploadPath = storage_path() . '/attachments/' . $ticket;
             $fs = new Filesystem();
             if (!$fs->isDirectory($uploadPath)) {
                 // Create the directory
                 $fs->makeDirectory($uploadPath);
             }
             $attachment->move($uploadPath, $attachment->getClientOriginalName());
             $_attachment = Attachment::create(['user_id' => $this->my_id, 'name' => $attachment->getClientOriginalName(), 'ticket_response_id' => $response->id]);
         }
     }
     $_ticket = Ticket::where('id', $ticket)->first();
     Ticket::where('id', $ticket)->update(['status_id' => $request->status]);
     $this->dispatch(new EmailUpdatedTicket($_ticket, $response));
     return redirect('/ticket-response/' . $ticket);
 }
Esempio n. 2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $config = Config::get('mail');
     switch ($config['encryption']) {
         case 'ssl':
             $connection_string = '{' . $config['host'] . ':993/imap/ssl}INBOX';
             break;
         case 'tls':
             $connection_string = '{' . $config['host'] . ':143/imap/tls}INBOX';
             break;
         case null:
         default:
             $connection_string = '{' . $config['host'] . ':143/imap}INBOX';
             break;
     }
     $this->info($connection_string);
     $tmp_attachment_path = storage_path() . '/attachments/tmp';
     $mailbox = new Mailbox($connection_string, $config['username'], $config['password'], $tmp_attachment_path);
     if (!$mailbox) {
         $this->error('Unable to connect to the IMAP server');
     }
     $mailIds = $mailbox->searchMailbox('UNSEEN');
     $this->info('Found ' . count($mailIds) . ' new emails to check');
     if (!empty($mailIds)) {
         foreach ($mailIds as $mailId) {
             $msg = $mailbox->getMail($mailId);
             $subject = str_replace(array('Re: ', 'RE: '), '', $msg->subject);
             $email = $msg->fromAddress;
             if ($msg->textHtml == '') {
                 $response = $msg->textPlain;
                 $response = str_replace("\n\r", '<br>', $response);
                 $response = str_replace("\n", '<br>', $response);
                 $response = '<p>' . $response . '</p>';
             } else {
                 $response = $msg->textHtml;
             }
             $_subjectParts = explode(' - ', $subject);
             if (count($_subjectParts) > 1) {
                 $_subjectParts = explode('[', $_subjectParts[1]);
                 $this->info('Email details:');
                 $this->info('Subject: ' . $subject);
                 $this->info('Email : ' . $email);
                 $this->info('TrackID : ' . $_subjectParts[0]);
                 // Find the ticket
                 $ticket = Ticket::where('track_id', $_subjectParts[0])->first();
                 // Find the user
                 $user = User::where('email', $email)->first();
                 if (count($ticket) > 0 || count($user) > 0) {
                     $this->info('Email associated with ticket ' . $ticket->id);
                     $this->info('Email associated with user ' . $user->first_name . ' ' . $user->last_name);
                     $uploadPath = storage_path() . '/attachments/' . $ticket->id;
                     $response = TicketResponse::create(['user_id' => $user->id, 'response' => $response, 'source' => 'email', 'ticket_id' => $ticket->id]);
                     $attachments = $msg->getAttachments();
                     if (count($attachments) > 0) {
                         $fs = new Filesystem();
                         if (!$fs->isDirectory($uploadPath)) {
                             $fs->makeDirectory($uploadPath);
                         }
                     }
                     foreach ($attachments as $attachment) {
                         $tmpPath = $attachment->filePath;
                         $fileName = $attachment->name;
                         $this->info('Trying to move file from ' . $tmpPath . ' to ' . $uploadPath . '/' . $fileName);
                         File::move($tmpPath, $uploadPath . '/' . $fileName);
                         Attachment::create(['user_id' => $user->id, 'name' => $fileName, 'ticket_response_id' => $response->id]);
                     }
                     $this->dispatch(new EmailUpdatedTicket($ticket, $response));
                 } else {
                     $this->info('Ticket or User not found');
                 }
             } else {
                 $this->info('Not an email for us');
             }
         }
     }
 }