/**
  * Returns an singleton instance of this class
  */
 public static function getInstance($mandrillApiKey)
 {
     if (self::$instance == null) {
         self::$instance = new EmailSenderManager($mandrillApiKey);
     }
     return self::$instance;
 }
 public function service()
 {
     $mandrillEmailSenderManager = new MandrillEmailSenderManager($this->getCmsVar("mandrill_api_key_news"));
     $email_body_html = $_REQUEST['email_body_html'];
     if (empty($email_body_html)) {
         $this->error(array('errText' => 'email body is empty!'));
     }
     $fromEmail = $this->getCmsVar('pcstore_news_email');
     $fromName = 'Pcstore.am Newsletter!';
     $includeUsers = $_REQUEST['include_all_active_users'];
     $subject = 'Newsletter from PcStore.am!!!';
     $test = $_REQUEST['test'];
     if ($test == 1) {
         $testEmail = $_REQUEST['test_email'];
         $res = $mandrillEmailSenderManager->sendHtmlEmail($testEmail, $subject, $email_body_html, $fromEmail, $fromName);
         $this->ok(array('count' => 1));
     }
     $uninterestingEmailsManager = UninterestingEmailsManager::getInstance($this->config, $this->args);
     $newsletterSubscribersManager = NewsletterSubscribersManager::getInstance($this->config, $this->args);
     $emailsArray = $newsletterSubscribersManager->getAllSubscribers();
     $filteredEmailsArray = $uninterestingEmailsManager->removeUninterestingEmailsFromList($emailsArray);
     if ($includeUsers == 1) {
         $userManager = UserManager::getInstance($this->config, $this->args);
         $allUsers = $userManager->selectAll();
         foreach ($allUsers as $userDto) {
             if ($userDto->getAvtive() == 0) {
                 continue;
             }
             $userRealEmailAddress = strtolower($userManager->getRealEmailAddressByUserDto($userDto));
             if (filter_var($userRealEmailAddress, FILTER_VALIDATE_EMAIL)) {
                 $filteredEmailsArray[] = $userRealEmailAddress;
             }
         }
     }
     //for FB, Google and TWITTER users emails may be duplicated!!!! so used array_unique
     $recipients = array_unique($filteredEmailsArray);
     if (count($recipients) === 0) {
         $this->error(array('errText' => 'There is no any recipient!'));
     }
     $email_body_html .= '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>';
     $email_body_html .= '<p style="font-size:10px"><a href="*|UNSUB:http://pcstore.am/unsubscriber|*">Click here to unsubscribe.</a></p>';
     $res = $mandrillEmailSenderManager->sendHtmlEmail($recipients, $subject, $email_body_html, $fromEmail, $fromName);
     $this->ok(array('count' => count($recipients)));
 }
 private function sendEmailToDealersEmails($companyExProfiledto, $byMandrill = false)
 {
     $dealerEmailsText = $companyExProfiledto->getDealerEmails();
     $dealerEmailsArray = explode(';', $dealerEmailsText);
     $subject = $companyExProfiledto->getPriceEmailSubject();
     $body = stripslashes($companyExProfiledto->getPriceEmailBody());
     $fromEmail = $companyExProfiledto->getFromEmail();
     $companyName = $this->getCustomer()->getName();
     $isServiceCompany = $this->getUserLevel() == UserGroups::$SERVICE_COMPANY;
     $priceFiles = array();
     if ($isServiceCompany) {
         $companiesPriceListManager = ServiceCompaniesPriceListManager::getInstance($this->config, $this->args);
         $companyId = $companyExProfiledto->getServiceCompanyId();
         $companyLastPrices = $companiesPriceListManager->getCompanyLastPrices($companyId);
         if (!empty($companyLastPrices)) {
             foreach ($companyLastPrices as $key => $clp) {
                 $priceFiles[] = DATA_DIR . "/service_companies_prices/" . $companyId . '/' . $clp->getFileName() . '.' . $clp->getFileExt();
             }
         }
         $tmpSubdirectoryName = 'service_companies';
     } else {
         $companiesPriceListManager = CompaniesPriceListManager::getInstance($this->config, $this->args);
         $companyId = $companyExProfiledto->getCompanyId();
         $companyLastPrices = $companiesPriceListManager->getCompanyLastPrices($companyId);
         if (!empty($companyLastPrices)) {
             foreach ($companyLastPrices as $key => $clp) {
                 $priceFiles[] = DATA_DIR . "/companies_prices/" . $companyId . '/' . $clp->getFileName() . '.' . $clp->getFileExt();
             }
         }
         $tmpSubdirectoryName = 'companies';
     }
     $allEmailFileAttachments = array();
     foreach ($priceFiles as $key => $priceFile) {
         $pfnParts = explode('.', $priceFile);
         $priceFileExtention = end($pfnParts);
         if (file_exists($priceFile)) {
             $postfix = "";
             if ($key > 0) {
                 $postfix = '_' . ($key + 1);
             }
             $allEmailFileAttachments[$companyName . ' Pricelist' . $postfix . '.' . $priceFileExtention] = $priceFile;
         }
     }
     $additionalAttachedFiles = array();
     $dir = HTDOCS_TMP_DIR_ATTACHMENTS . '/' . $tmpSubdirectoryName . '/' . $companyId . '/';
     if (is_dir($dir)) {
         $additionalAttachedFiles = scandir($dir);
     }
     foreach ($additionalAttachedFiles as $file) {
         $fileFullPath = $dir . $file;
         if (is_file($fileFullPath)) {
             $path_parts = pathinfo($fileFullPath);
             $basename = $path_parts['basename'];
             $fileDisplayName = substr($basename, strpos($basename, '_') + 1);
             $allEmailFileAttachments[$fileDisplayName] = $fileFullPath;
         }
     }
     if ($byMandrill == true) {
         $mandrillEmailSenderManager = new MandrillEmailSenderManager($this->getCmsVar("mandrill_api_key"));
         $body .= '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>';
         $body .= '<p style="font-size:10px"><a href="*|UNSUB:http://pcstore.am/unsub/' . ($isServiceCompany ? 'sc' : 's') . '/' . $companyId . '|*">Click here to unsubscribe.</a></p>';
         $res = $mandrillEmailSenderManager->sendHtmlEmail($dealerEmailsArray, $subject, $body, $fromEmail, $companyName, $allEmailFileAttachments, ($isServiceCompany ? 'service_company' : 'company') . '_' . $companyId);
         $sentSuccess = is_array($res);
     } else {
         $emailSenderManager = new EmailSenderManager('gmail');
         $res = $emailSenderManager->sendBulkEmailWithAttachmentsUsingPcstoreEmails($dealerEmailsArray, $subject, $body, array(), $allEmailFileAttachments, $fromEmail, $companyName);
         $sentSuccess = $res === true;
     }
     if ($sentSuccess !== true) {
         return $res;
     } else {
         $companyPriceEmailHistoryManager = CompanyPriceEmailHistoryManager::getInstance($this->config, $this->args);
         $companyPriceEmailHistoryManager->addRow($companyId, $isServiceCompany ? "service_company" : "company", $fromEmail, $dealerEmailsArray, $body, $subject, array_keys($allEmailFileAttachments));
     }
     return true;
 }