Esempio n. 1
1
 /**
  * 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();
                         }
                     }
                 }
             }
         }
     }
 }
Esempio n. 2
1
}
$db = mysql_select_db($cfg["db_name"], $mysql_pconnect);
if (!$db) {
    echo "DB Select Failed";
    exit;
}
//Clear Database
/*mysql_query( "TRUNCATE TABLE `emailtodb_attach`" );
mysql_query( "TRUNCATE TABLE `emailtodb_email`" );*/
// IMAP must be enabled in Google Mail Settings
//http://lcl.apewp.org/php-imap-master/example/index.php
define('GMAIL_IMAP_PATH', '{imap.gmail.com:993/imap/ssl}INBOX');
define('GMAIL_EMAIL', '*****@*****.**');
define('GMAIL_PASSWORD', '*********');
define('ATTACHMENTS_DIR', __DIR__ . '/attachments');
$mailbox = new ImapMailbox(GMAIL_IMAP_PATH, GMAIL_EMAIL, GMAIL_PASSWORD, ATTACHMENTS_DIR, 'utf-8');
$mails = array();
$baseUri = 'http://lcl.sandbox.com/php-imap-master/example/attachments/';
// Get some mail
$mailsIds = $mailbox->searchMailBox('ALL');
if (!$mailsIds) {
    die('Mailbox is empty');
}
//get only from first email in array
//$mailId = reset($mailsIds);
//get only from latest email in array
$mailId = end($mailsIds);
//Get Header Only, See Option in function
//$header = $mailbox->getHeader( $mailId );
//var_dump( $header );
//Get all mail parts
 public function pullSaveTranslations()
 {
     $imap = Configure::read('EmailReceive')['imap'];
     $mailbox = new PhpImap\Mailbox($imap['path'], $imap['login'], $imap['password']);
     $mailsIds = $mailbox->searchMailBox('ALL');
     if (!$mailsIds) {
         die('Mailbox is empty');
     }
     foreach ($mailsIds as $mailId) {
         $mail = $mailbox->getMail($mailId);
         var_dump($mail);
     }
     $this->autoRender = false;
 }
 /**
  * @param string $criteria
  * @param int    $numberOfRetries
  * @param int    $waitInterval
  *
  * @return array
  **/
 protected function retry($criteria, $numberOfRetries, $waitInterval)
 {
     $mailIds = [];
     while ($numberOfRetries > 0) {
         sleep($waitInterval);
         $mailIds = $this->mailbox->searchMailBox($criteria);
         if (!empty($mailIds)) {
             break;
         }
         $numberOfRetries--;
         codecept_debug("Failed to find the email, retrying ... ({$numberOfRetries}) trie(s) left");
     }
     return $mailIds;
 }
 public function readmails1()
 {
     $mailbox = new ImapMailbox('{imap-mail.outlook.com:993/imap/ssl/novalidate-cert}INBOX', '*****@*****.**', '786Saifu@', __DIR__);
     //dd($mailbox);
     $msgnos = $mailbox->searchMailBox('ALL');
     $mailId = $msgnos[73];
     //foreach ($msgnos as $mailId) {
     // get overview of mails
     $overview = $mailbox->get_overview($mailId);
     $mail = $mailbox->getMail($mailId);
     dd($mail);
     $html = $mail->textHtml;
     $body = self::trimTableTag($html);
     // }
 }
Esempio n. 6
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');
             }
         }
     }
 }