public function sendContent($from, $to, $subject, $type, $content)
 {
     if (is_string($to)) {
         $to = [$to];
     }
     $recipients = Mailjet::parse_recipient_type($to);
     // Build the HTTP POST body text
     if ($type == 'html') {
         $body = http_build_query(array('from' => $from, 'to' => implode(', ', $recipients['to']), 'cc' => implode(', ', $recipients['cc']), 'bcc' => implode(', ', $recipients['bcc']), 'subject' => $subject, 'html' => $content));
     } else {
         if ($type == 'text') {
             $body = http_build_query(array('from' => $from, 'to' => implode(', ', $recipients['to']), 'cc' => implode(', ', $recipients['cc']), 'bcc' => implode(', ', $recipients['bcc']), 'subject' => $subject, 'text' => $content));
         } else {
             throw new Exception('Wrong email type');
         }
     }
     utils::log($body);
     $options = array('scheme' => 'http', 'host' => 'api.mailjet.com', 'path' => '/v3/send/');
     $endpoint = Mailjet::unparse_url($options);
     $headers = array('Authorization' => 'Basic ' . $this->_authentificate, 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => strlen($body));
     // API request
     Unirest\Request::verifyPeer(false);
     $response = Unirest\Request::post($endpoint, $headers, $body);
     utils::log('STATUS: ' . $response->code);
     utils::log('HEADERS: ' . json_encode($response->headers));
     utils::log('BODY: ' . $response->raw_body);
     return $response->code == 200;
 }
 public function translate()
 {
     //utils::log ('translate launched!') ;
     try {
         $path = $this->lmv->dataDir("/{$this->identifier}.dependencies.json", true);
         if ($path === false) {
             throw new Exception('No dependency file found');
         }
         $content = file_get_contents($path);
         $connections = json_decode($content);
         if ($content === false || !is_object($connections) || is_null($connections[0])) {
             utils::log('ERROR: project dependencies corrupted :(');
             // Rebuild one master only
             $connections = (object) array('uniqueIdentifier' => $this->identifier, 'children' => []);
         }
         $items = [$this->identifier];
         $items = array_merge($items, $this->traverseConnections($connections->children));
         // This is to help the upload progress bar to be more precise
         if (file_put_contents($path, json_encode($items)) === false) {
             utils::log('ERROR: project dependencies not saved :(');
             throw new Exception('ERROR: project dependencies not saved :(');
         }
         // Bucket
         $policy = 'transient';
         $r1 = $this->lmv->createBucketIfNotExist($policy);
         if ($r1 == null) {
             // No need to continue if the bucket was not created
             utils::log('Failed to create bucket!');
             throw new Exception('Failed to create bucket!');
         }
         utils::log('Bucket created (or did exist already)!');
         // Uploading file(s)
         utils::log('Uploading(s)');
         foreach ($items as $item) {
             utils::log(" uploading {$item}");
             $r2 = $this->lmv->uploadFile($item);
             if ($r2 == null) {
                 utils::log(" Failed to upload {$item}");
                 throw new Exception("Failed to upload {$item}");
             }
             utils::log("{$item} upload completed!");
         }
         utils::log('All files uploaded');
         // Dependencies
         $r3 = $this->lmv->setDependencies(count($items) == 1 ? null : $connections);
         if ($r3 == null) {
             utils::log('Failed to set connections');
             throw new Exception('Failed to set connections');
         }
         utils::log('References set, launching translation');
         // Register
         $r4 = $this->lmv->register($connections);
         if ($r3 == null) {
             utils::log('URN registration for translation failed');
             throw new Exception('URN registration for translation failed');
         }
         utils::log('URN registered for translation');
         // We are done for now!
         // Just remember locally we did submit the project for translation
         $urn = $this->lmv->getURN($this->identifier);
         $urn = base64_encode($urn);
         $data = (object) array('guid' => $urn, 'progress' => '0% complete', 'startedAt' => gmdate(DATE_RFC2822), 'status' => 'requested', 'success' => '0%', 'urn' => $urn);
         $path = $this->lmv->dataDir("/{$this->identifier}.resultdb.json");
         if (file_put_contents($path, json_encode($data)) === false) {
             utils::log("Could not save file to disk - {$path}");
         }
         $filesystem = new Filesystem();
         foreach ($items as $item) {
             $filesystem->remove($this->lmv->dataDir("/{$item}.json"));
         }
         $filesystem->remove($this->lmv->dataDir("/{$this->identifier}.dependencies.json"));
         $filesystem->remove($this->lmv->dataDir("/{$this->identifier}.connections.json"));
         return true;
     } catch (Exception $ex) {
         //- We are done! email me if any error
         utils::log('Something wrong happened during upload');
         $loader = new Twig_Loader_Filesystem(array($this->lmv->viewsDir('', true)));
         $environment = new Twig_Environment($loader, array('debug' => false, 'charset' => 'utf-8', 'strict_variables' => true));
         $content = $environment->render('email-xlt-failed.twig', array('ID' => $this->identifier));
         $config = lmv::config();
         $mjet = new Mailjet($config['MAILJET1'], $config['MAILJET2']);
         $mjet->sendContent('ADN Sparks <*****@*****.**>', '*****@*****.**', 'Autodesk View & Data API Extractor app failed to translate a project', 'html', $content);
         $filesystem = new Filesystem();
         $filesystem->rename($this->lmv->dataDir("/{$this->identifier}.resultdb.json", true), $this->lmv->dataDir("/{$this->identifier}.resultdb.failed"), true);
         return false;
     }
 }
 protected function NotifyError()
 {
     $loader = new Twig_Loader_Filesystem(array($this->lmv->viewsDir('', true)));
     $environment = new Twig_Environment($loader, array('debug' => false, 'charset' => 'utf-8', 'strict_variables' => true));
     $content = $environment->render('email-extract-failed.twig', array('ID' => $this->identifier));
     $config = lmv::config();
     $mjet = new Mailjet($config['MAILJET1'], $config['MAILJET2']);
     $mjet->sendContent('ADN Sparks <*****@*****.**>', $config['mailTo'], 'Autodesk View & Data API Extraction failed', 'html', $content);
     //'replyTo': '*****@*****.**',
     //'forceEmbeddedImages': true
 }