/**
  * Reademails
  * @return type
  */
 public function readmails(Emails $emails, Email $settings_email, System $system)
 {
     $path_url = $system->first()->url;
     if ($settings_email->first()->email_fetching == 1) {
         if ($settings_email->first()->all_emails == 1) {
             $helptopic = $this->TicketController->default_helptopic();
             $sla = $this->TicketController->default_sla();
             $email = $emails->get();
             foreach ($email as $e_mail) {
                 $dept = $e_mail->department;
                 $host = $e_mail->fetching_host;
                 $port = $e_mail->fetching_port;
                 $protocol = $e_mail->mailbox_protocol;
                 $imap_config = '{' . $host . ':' . $port . $protocol . '}INBOX';
                 $password = Crypt::decrypt($e_mail->password);
                 $mailbox = new ImapMailbox($imap_config, $e_mail->user_name, $password, __DIR__);
                 $mails = array();
                 $mailsIds = $mailbox->searchMailBox('SINCE ' . date('d-M-Y', strtotime("-1 day")));
                 if (!$mailsIds) {
                     die('Mailbox is empty');
                 }
                 // dd($mailsIds);
                 foreach ($mailsIds as $mailId) {
                     $overview = $mailbox->get_overview($mailId);
                     $var = $overview[0]->seen ? 'read' : 'unread';
                     if ($var == 'unread') {
                         $mail = $mailbox->getMail($mailId);
                         if ($settings_email->email_collaborator == 1) {
                             $collaborator = $mail->cc;
                         } else {
                             $collaborator = null;
                         }
                         $body = $mail->textHtml;
                         // dd($mailId);
                         if ($body == null) {
                             $body = $mailbox->backup_getmail($mailId);
                             $body = str_replace('\\r\\n', '<br/>', $body);
                             // var_dump($body);
                         }
                         // dd($body);
                         $date = $mail->date;
                         $datetime = $overview[0]->date;
                         $date_time = explode(" ", $datetime);
                         $date = $date_time[1] . "-" . $date_time[2] . "-" . $date_time[3] . " " . $date_time[4];
                         $date = date('Y-m-d H:i:s', strtotime($date));
                         if (isset($mail->subject)) {
                             $subject = $mail->subject;
                         } else {
                             $subject = "No Subject";
                         }
                         $fromname = $mail->fromName;
                         $fromaddress = $mail->fromAddress;
                         $source = "2";
                         $phone = "";
                         $priority = '1';
                         $assign = "";
                         $form_data = null;
                         if ($this->TicketController->create_user($fromaddress, $fromname, $subject, $body, $phone, $helptopic, $sla, $priority, $source, $collaborator, $dept, $assign, $form_data) == true) {
                             $thread_id = Ticket_Thread::whereRaw('id = (select max(`id`) from ticket_thread)')->first();
                             $thread_id = $thread_id->id;
                             foreach ($mail->getAttachments() as $attachment) {
                                 $support = "support";
                                 // echo $_SERVER['DOCUMENT_ROOT'];
                                 $dir_img_paths = __DIR__;
                                 $dir_img_path = explode('/code', $dir_img_paths);
                                 $filepath = explode('../../../../../../public/', $attachment->filePath);
                                 // dd($filepath);
                                 // $path = $dir_img_path[0]."/public/".$filepath[1];
                                 $path = public_path() . '/' . $filepath[1];
                                 // dd($path);
                                 $filesize = filesize($path);
                                 $file_data = file_get_contents($path);
                                 $ext = pathinfo($attachment->filePath, PATHINFO_EXTENSION);
                                 $imageid = $attachment->id;
                                 $string = str_replace('-', '', $attachment->name);
                                 $filename = explode('src', $attachment->filePath);
                                 $filename = str_replace('\\', '', $filename);
                                 $body = str_replace("cid:" . $imageid, $filepath[1], $body);
                                 $pos = strpos($body, $filepath[1]);
                                 if ($pos == false) {
                                     if ($settings_email->first()->attachment == 1) {
                                         $upload = new Ticket_attachments();
                                         $upload->file = $file_data;
                                         $upload->thread_id = $thread_id;
                                         $upload->name = $filepath[1];
                                         $upload->type = $ext;
                                         $upload->size = $filesize;
                                         $upload->poster = "ATTACHMENT";
                                         $upload->save();
                                     }
                                 } else {
                                     $upload = new Ticket_attachments();
                                     $upload->file = $file_data;
                                     $upload->thread_id = $thread_id;
                                     $upload->name = $filepath[1];
                                     $upload->type = $ext;
                                     $upload->size = $filesize;
                                     $upload->poster = "INLINE";
                                     $upload->save();
                                 }
                                 unlink($path);
                             }
                             $body = Encoding::fixUTF8($body);
                             $thread = Ticket_Thread::where('id', '=', $thread_id)->first();
                             $thread->body = $this->separate_reply($body);
                             $thread->save();
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * post port
  *
  * @return string
  */
 public static function port()
 {
     $port = Emails::where('id', '=', '1')->first();
     $portvalue = $port->option_value;
     return $portvalue;
 }
 /**
  * Remove the specified resource from storage.
  * @param type int $id
  * @param type Emails $email
  * @return type Response
  */
 public function destroy($id, Emails $email)
 {
     try {
         $emails = $email->whereId($id)->first();
         if ($emails->delete() == true) {
             return redirect('emails')->with('success', 'Email Deleted sucessfully');
         } else {
             return redirect('emails')->with('fails', 'Email can not  Delete ');
         }
     } catch (Exception $e) {
         return redirect('emails')->with('fails', 'Email can not  Delete ');
     }
 }
 /**
  * get the form for Email setting page
  * @param type Email $email
  * @param type Template $template
  * @param type Emails $email1
  * @return type Response
  */
 public function getemail(Email $email, Template $template, Emails $email1)
 {
     try {
         /* fetch the values of email from Email table */
         $emails = $email->whereId('1')->first();
         /* Fetch the values from Template table */
         $templates = $template->get();
         /* Fetch the values from Emails table */
         $emails1 = $email1->get();
         /* Direct to Email Settings Page */
         return view('themes.default1.admin.helpdesk.settings.email', compact('emails', 'templates', 'emails1'));
     } catch (Exception $e) {
         return view('404');
     }
 }
 /**
  * Show the form for editing the specified resource.
  * @param type int $id
  * @param type User $user
  * @param type Group_assign_department $group_assign_department
  * @param type Template $template
  * @param type Teams $team
  * @param type Department $department
  * @param type Sla_plan $sla
  * @param type Emails $email
  * @param type Groups $group
  * @return type Response
  */
 public function edit($id, User $user, Group_assign_department $group_assign_department, Template $template, Teams $team, Department $department, Sla_plan $sla, Emails $email, Groups $group)
 {
     try {
         $slas = $sla->get();
         $user = $user->where('role', 'agent')->get();
         $emails = $email->get();
         $templates = $template->get();
         $departments = $department->whereId($id)->first();
         $groups = $group->lists('id', 'name');
         $assign = $group_assign_department->where('department_id', $id)->lists('group_id');
         return view('themes.default1.admin.helpdesk.agent.departments.edit', compact('assign', 'team', 'templates', 'departments', 'slas', 'user', 'emails', 'groups'));
     } catch (Exception $e) {
         return view('404');
     }
 }
 /**
  * Form for Email connection checking.
  * @param type Emails $email
  * @return type Response
  */
 public function formDiagno(Emails $email)
 {
     try {
         $emails = $email->get();
         return view('themes.default1.admin.helpdesk.emails.template.formDiagno', compact('emails'));
     } catch (Exception $e) {
         return view('404');
     }
 }