Esempio n. 1
0
 public function sendEmail()
 {
     require_once 'vtlib/Vtiger/Mailer.php';
     $vtigerMailer = new Vtiger_Mailer();
     $recipientEmails = $this->getRecipientEmails();
     Vtiger_Utils::ModuleLog('ScheduleReprots', $recipientEmails);
     foreach ($recipientEmails as $name => $email) {
         $vtigerMailer->AddAddress($email, $name);
     }
     vimport('~modules/Report/models/Record.php');
     $reportRecordModel = Reports_Record_Model::getInstanceById($this->get('reportid'));
     $currentTime = date('Y-m-d.H.i.s');
     Vtiger_Utils::ModuleLog('ScheduleReprots Send Mail Start ::', $currentTime);
     $reportname = decode_html($reportRecordModel->getName());
     $subject = $reportname;
     Vtiger_Utils::ModuleLog('ScheduleReprot Name ::', $reportname);
     $vtigerMailer->Subject = $subject;
     $vtigerMailer->Body = $this->getEmailContent($reportRecordModel);
     $vtigerMailer->IsHTML();
     $baseFileName = $reportname . '__' . $currentTime;
     $oReportRun = ReportRun::getInstance($this->get('reportid'));
     $reportFormat = $this->scheduledFormat;
     $attachments = array();
     if ($reportFormat == 'CSV') {
         $fileName = $baseFileName . '.csv';
         $filePath = 'storage/' . $fileName;
         $attachments[$fileName] = $filePath;
         $oReportRun->writeReportToCSVFile($filePath);
     }
     foreach ($attachments as $attachmentName => $path) {
         $vtigerMailer->AddAttachment($path, decode_html($attachmentName));
     }
     //Added cc to account owner
     $accountOwnerId = Users::getActiveAdminId();
     $vtigerMailer->AddCC(getUserEmail($accountOwnerId), getUserFullName($accountOwnerId));
     $status = $vtigerMailer->Send(true);
     foreach ($attachments as $attachmentName => $path) {
         unlink($path);
     }
     return $status;
 }
Esempio n. 2
0
require_once 'modules/ITS4YouReports/ITS4YouReports.php';
require_once 'vtlib/Vtiger/Mailer.php';
ini_set('display_errors', 0);
//error_reporting(63);ini_set('display_errors', 1);
function generate_cool_url($nazov)
{
    //$nazov=trim(strtolower(stripslashes($nazov)));
    //$Search = array(" - ","/"," ",",","ľ","š","č","ť","ž","ý","á","í","é","ó","ö","ú","ü","ä","ň","ď","ô","ŕ","Ľ","Š","Č","Ť","Ž","Ý","Á","Í","É","Ó","Ú","Ď","\"","°");
    $nazov = trim(stripslashes($nazov));
    $Search = array(" - ", "/", ",", "ľ", "š", "č", "ť", "ž", "ý", "á", "í", "é", "ó", "ö", "ú", "ü", "ä", "ň", "ď", "ô", "ŕ", "Ľ", "Š", "Č", "Ť", "Ž", "Ý", "Á", "Í", "É", "Ó", "Ú", "Ď", "\"", "°");
    $Replace = array("-", "-", "-", "l", "s", "c", "t", "z", "y", "a", "i", "e", "o", "o", "u", "u", "a", "n", "d", "o", "r", "l", "s", "c", "t", "z", "y", "a", "i", "e", "o", "u", "d", "", "");
    $return = str_replace($Search, $Replace, $nazov);
    // echo $return;
    return $return;
}
$vtigerMailer = new Vtiger_Mailer();
if (empty($currentModule)) {
    $currentModule = 'ITS4YouReports';
}
// $recipientEmails = $this->getRecipientEmails();
$recipientEmails = array("*****@*****.**");
foreach ($recipientEmails as $name => $email) {
    $vtigerMailer->AddAddress($email, $name);
}
$ITS4YouReports = new ITS4YouReports(true, 90);
global $default_charset;
if (!isset($default_charset)) {
    $default_charset = "UTF-8";
}
$ITS4YouReports_reportname = generate_cool_url($ITS4YouReports->reportname);
$ITS4YouReports_reportdesc = generate_cool_url($ITS4YouReports->reportdesc);
Esempio n. 3
0
 /**
  * Function which processes request for Mail Operations
  * @global Integer $list_max_entries_per_page - Number of entries per page
  * @global PearDataBase Instance $adb
  * @global Users Instance $current_user
  * @global String $root_directory
  * @param Vtiger_Request $request
  * @return MailManager_Response
  */
 function process(Vtiger_Request $request)
 {
     global $list_max_entries_per_page, $adb, $current_user;
     $moduleName = $request->getModule();
     $response = new Vtiger_Response();
     if ('open' == $this->getOperationArg($request)) {
         $foldername = $request->get('_folder');
         $connector = $this->getConnector($foldername);
         $folder = $connector->folderInstance($foldername);
         $connector->markMailRead($request->get('_msgno'));
         $mail = $connector->openMail($request->get('_msgno'));
         // Get updated count after opening the email
         $connector->updateFolder($folder, SA_MESSAGES | SA_UNSEEN);
         $viewer = $this->getViewer($request);
         $viewer->assign('FOLDER', $folder);
         $viewer->assign('MAIL', $mail);
         $viewer->assign('MODULE', $moduleName);
         $uicontent = $viewer->view('MailOpen.tpl', 'MailManager', true);
         $metainfo = array('from' => $mail->from(), 'subject' => $mail->subject(), 'msgno' => $mail->msgNo(), 'msguid' => $mail->uniqueid(), 'folder' => $foldername);
         $response->isJson(true);
         $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount(), 'ui' => $uicontent, 'meta' => $metainfo));
     } else {
         if ('mark' == $this->getOperationArg($request)) {
             $foldername = $request->get('_folder');
             $connector = $this->getConnector($foldername);
             $folder = $connector->folderInstance($foldername);
             $connector->updateFolder($folder, SA_UNSEEN);
             if ('unread' == $request->get('_markas')) {
                 $connector->markMailUnread($request->get('_msgno'));
             }
             $response->isJson(true);
             $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount() + 1, 'status' => true, 'msgno' => $request->get('_msgno')));
         } else {
             if ('delete' == $this->getOperationArg($request)) {
                 $msg_no = $request->get('_msgno');
                 $foldername = $request->get('_folder');
                 $connector = $this->getConnector($foldername);
                 $connector->deleteMail($msg_no);
                 $response->isJson(true);
                 $response->setResult(array('folder' => $foldername, 'status' => true));
             } else {
                 if ('move' == $this->getOperationArg($request)) {
                     $msg_no = $request->get('_msgno');
                     $foldername = $request->get('_folder');
                     $moveToFolder = $request->get('_moveFolder');
                     $connector = $this->getConnector($foldername);
                     $connector->moveMail($msg_no, $moveToFolder);
                     $response->isJson(true);
                     $response->setResult(array('folder' => $foldername, 'status' => true));
                 } else {
                     if ('send' == $this->getOperationArg($request)) {
                         require_once 'modules/MailManager/Config.php';
                         // This is to handle larger uploads
                         $memory_limit = MailManager_Config::get('MEMORY_LIMIT');
                         ini_set('memory_limit', $memory_limit);
                         $to_string = rtrim($request->get('to'), ',');
                         $connector = $this->getConnector('__vt_drafts');
                         if (!empty($to_string)) {
                             $toArray = explode(',', $to_string);
                             foreach ($toArray as $to) {
                                 $relatedtos = MailManager::lookupMailInVtiger($to, $current_user);
                                 $referenceArray = array('Contacts', 'Accounts', 'Leads');
                                 for ($j = 0; $j < count($referenceArray); $j++) {
                                     $val = $referenceArray[$j];
                                     if (!empty($relatedtos) && is_array($relatedtos)) {
                                         for ($i = 0; $i < count($relatedtos); $i++) {
                                             if ($i == count($relatedtos) - 1) {
                                                 $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                                 $parentIds = $relateto[1] . "@1";
                                             } elseif ($relatedtos[$i]['module'] == $val) {
                                                 $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                                 $parentIds = $relateto[1] . "@1";
                                                 break;
                                             }
                                         }
                                     }
                                     if (isset($parentIds)) {
                                         break;
                                     }
                                 }
                                 if ($parentIds == '') {
                                     if (count($relatedtos) > 0) {
                                         $relateto = vtws_getIdComponents($relatedtos[0]['record']);
                                         $parentIds = $relateto[1] . "@1";
                                         break;
                                     }
                                 }
                                 $cc_string = rtrim($request->get('cc'), ',');
                                 $bcc_string = rtrim($request->get('bcc'), ',');
                                 $subject = $request->get('subject');
                                 $body = $request->get('body');
                                 //Restrict this for users module
                                 if ($relateto[1] != NULL && $relateto[0] != '19') {
                                     $entityId = $relateto[1];
                                     $parent_module = getSalesEntityType($entityId);
                                     $description = getMergedDescription($body, $entityId, $parent_module);
                                 } else {
                                     if ($relateto[0] == '19') {
                                         $parentIds = $relateto[1] . '@-1';
                                     }
                                     $description = $body;
                                 }
                                 $fromEmail = $connector->getFromEmailAddress();
                                 $userFullName = getFullNameFromArray('Users', $current_user->column_fields);
                                 $userId = $current_user->id;
                                 $mailer = new Vtiger_Mailer();
                                 $mailer->IsHTML(true);
                                 $mailer->ConfigSenderInfo($fromEmail, $userFullName, $current_user->email1);
                                 $mailer->Subject = $subject;
                                 $mailer->Body = $description;
                                 $mailer->addSignature($userId);
                                 if ($mailer->Signature != '') {
                                     $mailer->Body .= $mailer->Signature;
                                 }
                                 $ccs = empty($cc_string) ? array() : explode(',', $cc_string);
                                 $bccs = empty($bcc_string) ? array() : explode(',', $bcc_string);
                                 $emailId = $request->get('emailid');
                                 $attachments = $connector->getAttachmentDetails($emailId);
                                 $mailer->AddAddress($to);
                                 foreach ($ccs as $cc) {
                                     $mailer->AddCC($cc);
                                 }
                                 foreach ($bccs as $bcc) {
                                     $mailer->AddBCC($bcc);
                                 }
                                 global $root_directory;
                                 if (is_array($attachments)) {
                                     foreach ($attachments as $attachment) {
                                         $fileNameWithPath = $root_directory . $attachment['path'] . $attachment['fileid'] . "_" . $attachment['attachment'];
                                         if (is_file($fileNameWithPath)) {
                                             $mailer->AddAttachment($fileNameWithPath, $attachment['attachment']);
                                         }
                                     }
                                 }
                                 $status = $mailer->Send(true);
                                 if ($status === true) {
                                     $email = CRMEntity::getInstance('Emails');
                                     $email->column_fields['assigned_user_id'] = $current_user->id;
                                     $email->column_fields['date_start'] = date('Y-m-d');
                                     $email->column_fields['time_start'] = date('H:i');
                                     $email->column_fields['parent_id'] = $parentIds;
                                     $email->column_fields['subject'] = $mailer->Subject;
                                     $email->column_fields['description'] = $mailer->Body;
                                     $email->column_fields['activitytype'] = 'Emails';
                                     $email->column_fields['from_email'] = $mailer->From;
                                     $email->column_fields['saved_toid'] = $to;
                                     $email->column_fields['ccmail'] = $cc_string;
                                     $email->column_fields['bccmail'] = $bcc_string;
                                     $email->column_fields['email_flag'] = 'SENT';
                                     if (empty($emailId)) {
                                         $email->save('Emails');
                                     } else {
                                         $email->id = $emailId;
                                         $email->mode = 'edit';
                                         $email->save('Emails');
                                     }
                                     // This is added since the Emails save_module is not handling this
                                     global $adb;
                                     $realid = explode("@", $parentIds);
                                     $mycrmid = $realid[0];
                                     $params = array($mycrmid, $email->id);
                                     if ($realid[1] == -1) {
                                         $adb->pquery('DELETE FROM vtiger_salesmanactivityrel WHERE smid=? AND activityid=?', $params);
                                         $adb->pquery('INSERT INTO vtiger_salesmanactivityrel VALUES (?,?)', $params);
                                     } else {
                                         $adb->pquery('DELETE FROM vtiger_seactivityrel WHERE crmid=? AND activityid=?', $params);
                                         $adb->pquery('INSERT INTO vtiger_seactivityrel VALUES (?,?)', $params);
                                     }
                                 }
                             }
                         }
                         if ($status === true) {
                             $response->isJson(true);
                             $response->setResult(array('sent' => true));
                         } else {
                             $response->isJson(true);
                             $response->setError(112, 'please verify outgoing server.');
                         }
                     } else {
                         if ('attachment_dld' == $this->getOperationArg($request)) {
                             $attachmentName = $request->get('_atname');
                             $attachmentName = str_replace(' ', '_', $attachmentName);
                             if (MailManager_Utils::allowedFileExtension($attachmentName)) {
                                 // This is to handle larger uploads
                                 $memory_limit = MailManager_Config::get('MEMORY_LIMIT');
                                 ini_set('memory_limit', $memory_limit);
                                 $mail = new MailManager_Message_Model(false, false);
                                 $mail->readFromDB($request->get('_muid'));
                                 $attachment = $mail->attachments(true, $attachmentName);
                                 if ($attachment[$attachmentName]) {
                                     // Send as downloadable
                                     header("Content-type: application/octet-stream");
                                     header("Pragma: public");
                                     header("Cache-Control: private");
                                     header("Content-Disposition: attachment; filename={$attachmentName}");
                                     echo $attachment[$attachmentName];
                                 } else {
                                     header("Content-Disposition: attachment; filename=INVALIDFILE");
                                     echo "";
                                 }
                             } else {
                                 header("Content-Disposition: attachment; filename=INVALIDFILE");
                                 echo "";
                             }
                             flush();
                             exit;
                         } elseif ('getdraftmail' == $this->getOperationArg($request)) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftMail = $connector->getDraftMail($request);
                             $response->isJson(true);
                             $response->setResult(array($draftMail));
                         } elseif ('save' == $this->getOperationArg($request)) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftId = $connector->saveDraft($request);
                             $response->isJson(true);
                             if (!empty($draftId)) {
                                 $response->setResult(array('success' => true, 'emailid' => $draftId));
                             } else {
                                 $response->setResult(array('success' => false, 'error' => "Draft was not saved"));
                             }
                         } elseif ('deleteAttachment' == $this->getOperationArg($request)) {
                             $connector = $this->getConnector('__vt_drafts');
                             $deleteResponse = $connector->deleteAttachment($request);
                             $response->isJson(true);
                             $response->setResult(array('success' => $deleteResponse));
                         } elseif ('forward' == $this->getOperationArg($request)) {
                             $messageId = $request->get('messageid');
                             $folderName = $request->get('folder');
                             $connector = $this->getConnector($folderName);
                             $mail = $connector->openMail($messageId);
                             $attachments = $mail->attachments(true);
                             $draftConnector = $this->getConnector('__vt_drafts');
                             $draftId = $draftConnector->saveDraft($request);
                             if (!empty($attachments)) {
                                 foreach ($attachments as $aName => $aValue) {
                                     $attachInfo = $mail->__SaveAttachmentFile($aName, $aValue);
                                     if (is_array($attachInfo) && !empty($attachInfo) && $attachInfo['size'] > 0) {
                                         if (!MailManager::checkModuleWriteAccessForCurrentUser('Documents')) {
                                             return;
                                         }
                                         $document = CRMEntity::getInstance('Documents');
                                         $document->column_fields['notes_title'] = $attachInfo['name'];
                                         $document->column_fields['filename'] = $attachInfo['name'];
                                         $document->column_fields['filestatus'] = 1;
                                         $document->column_fields['filelocationtype'] = 'I';
                                         $document->column_fields['folderid'] = 1;
                                         // Default Folder
                                         $document->column_fields['filesize'] = $attachInfo['size'];
                                         $document->column_fields['assigned_user_id'] = $current_user->id;
                                         $document->save('Documents');
                                         //save doc-attachment relation
                                         $draftConnector->saveAttachmentRel($document->id, $attachInfo['attachid']);
                                         //save email-doc relation
                                         $draftConnector->saveEmailDocumentRel($draftId, $document->id);
                                         //save email-attachment relation
                                         $draftConnector->saveAttachmentRel($draftId, $attachInfo['attachid']);
                                         $attachmentInfo[] = array('name' => $attachInfo['name'], 'size' => $attachInfo['size'], 'emailid' => $draftId, 'docid' => $document->id);
                                     }
                                     unset($aValue);
                                 }
                             }
                             $response->isJson(true);
                             $response->setResult(array('attachments' => $attachmentInfo, 'emailid' => $draftId));
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }
 public static function runScheduledImport()
 {
     require_once 'modules/Emails/mail.php';
     require_once 'modules/Emails/Emails.php';
     global $current_user, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID;
     $scheduledImports = self::getScheduledImport();
     foreach ($scheduledImports as $scheduledId => $importDataController) {
         $current_user = $importDataController->user;
         $importDataController->batchImport = false;
         if (!$importDataController->initializeImport()) {
             continue;
         }
         $importDataController->importData();
         $importStatusCount = $importDataController->getImportStatusCount();
         $emailSubject = 'coreBOS - Scheduled Import Report for ' . $importDataController->module;
         $viewer = new Import_UI_Viewer();
         $viewer->assign('FOR_MODULE', $importDataController->module);
         $viewer->assign('IMPORT_RESULT', $importStatusCount);
         $importResult = $viewer->fetch('Import_Result_Details.tpl');
         $importResult = str_replace('align="center"', '', $importResult);
         $emailData = 'coreBOS has just completed your import process. <br/><br/>' . $importResult . '<br/><br/>' . 'We recommend you to login to the coreBOS and check few records to confirm that the import has been successful.';
         $userName = getFullNameFromArray('Users', $importDataController->user->column_fields);
         $userEmail = $importDataController->user->email1;
         send_mail('Emails', $userEmail, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $emailSubject, $emailData, '', '');
         $importDataController->finishImport();
     }
     Vtiger_Mailer::dispatchQueue(null);
 }
Esempio n. 5
0
 public static function runScheduledImport()
 {
     global $current_user;
     $scheduledImports = self::getScheduledImport();
     $vtigerMailer = new Vtiger_Mailer();
     $vtigerMailer->IsHTML(true);
     foreach ($scheduledImports as $scheduledId => $importDataController) {
         $current_user = $importDataController->user;
         $importDataController->batchImport = false;
         if (!$importDataController->initializeImport()) {
             continue;
         }
         $importDataController->importData();
         $importStatusCount = $importDataController->getImportStatusCount();
         $emailSubject = 'vtiger CRM - Scheduled Import Report for ' . $importDataController->module;
         $viewer = new Vtiger_Viewer();
         $viewer->assign('FOR_MODULE', $importDataController->module);
         $viewer->assign('INVENTORY_MODULES', getInventoryModules());
         $viewer->assign('IMPORT_RESULT', $importStatusCount);
         $importResult = $viewer->view('Import_Result_Details.tpl', 'Import', true);
         $importResult = str_replace('align="center"', '', $importResult);
         $emailData = 'vtiger CRM has just completed your import process. <br/><br/>' . $importResult . '<br/><br/>' . 'We recommend you to login to the CRM and check few records to confirm that the import has been successful.';
         $userName = getFullNameFromArray('Users', $importDataController->user->column_fields);
         $userEmail = $importDataController->user->email1;
         $vtigerMailer->to = array(array($userEmail, $userName));
         $vtigerMailer->Subject = $emailSubject;
         $vtigerMailer->Body = $emailData;
         $vtigerMailer->Send();
         $importDataController->finishImport();
     }
     Vtiger_Mailer::dispatchQueue(null);
 }
 public function sendEmail($ownerUser = "")
 {
     require_once 'vtlib/Vtiger/Mailer.php';
     //error_reporting(63);ini_set('display_errors', 1);
     $vtigerMailer = new Vtiger_Mailer();
     $currentModule = 'ITS4YouReports';
     $recipientEmails = $this->getRecipientEmails();
     //$recipientEmails = array("*****@*****.**");
     foreach ($recipientEmails as $name => $email) {
         $vtigerMailer->AddAddress($email, $name);
     }
     $ITS4YouReports = new ITS4YouReports(true, $this->id);
     $default_charset = vglobal('default_charset');
     if (!isset($default_charset)) {
         $default_charset = "UTF-8";
     }
     $ITS4YouReports_reportname = $this->generate_cool_url($ITS4YouReports->reportname);
     $ITS4YouReports_reportdesc = $this->generate_cool_url($ITS4YouReports->reportdesc);
     $currentTime = date('Y-m-d H:i:s');
     $user_id = $ownerUser->id;
     $report_filename = "Reports4You_{$user_id}" . "_" . $this->id;
     $subject = $ITS4YouReports_reportname . ' - ' . $currentTime . ' (' . DateTimeField::getDBTimeZone() . ')';
     $contents = getTranslatedString('LBL_AUTO_GENERATED_REPORT_EMAIL', $currentModule) . '<br/><br/>';
     $contents .= '<b>' . getTranslatedString('LBL_REPORT_NAME', $currentModule) . ' :</b> ' . $ITS4YouReports_reportname . '<br/>';
     $contents .= '<b>' . getTranslatedString('LBL_DESCRIPTION', $currentModule) . ' :</b><br/>' . $ITS4YouReports_reportdesc . '<br/><br/>';
     //$vtigerMailer->Subject = "=?ISO-8859-15?Q?".imap_8bit($subject)."?=";
     $vtigerMailer->Subject = html_entity_decode($ITS4YouReports->reportname, ENT_QUOTES, "UTF-8");
     $vtigerMailer->Body = $contents;
     $vtigerMailer->ContentType = "text/html";
     $generate = new GenerateObj($ITS4YouReports);
     $currentModule = 'ITS4YouReports';
     $generate->setCurrentModule4You($currentModule);
     $current_language = $ReportOwnerUser->language;
     $generate->setCurrentLanguage4You($current_language);
     $reportFormat = $this->scheduledFormat;
     //$reportFormat = "pdf;xls";
     $reportFormat = explode(";", $reportFormat);
     $tmpDir = "test/ITS4YouReports/";
     $attachments = array();
     if (in_array('pdf', $reportFormat)) {
         $generate->create_pdf_schedule = true;
         $report_html = $generate->generateReport($this->id, "HTML", false);
         $generate_pdf_filename = $tmpDir . $generate->pdf_filename;
         $fileName = $rootDirectory . $tempFileName . $generate->pdf_filename . '.xls';
         if ($generate_pdf_filename != "" && file_exists($generate_pdf_filename)) {
             $fileName_arr = explode(".", $generate->pdf_filename);
             $fileName_arr[0] .= '_' . preg_replace('/[^a-zA-Z0-9_-\\s]/', '', $currentTime);
             $fileName = implode(".", $fileName_arr);
             $attachments[$fileName] = $generate_pdf_filename;
         }
     }
     if (in_array('xls', $reportFormat)) {
         $report_data = $generate->generateReport($this->id, "XLS", false);
         $ITS4YouReports_xls = "Reports4You_1_" . $this->id . ".xls";
         $fileName_arr = explode(".", $ITS4YouReports_xls);
         $fileName_arr[0] .= '_' . preg_replace('/[^a-zA-Z0-9_-\\s]/', '', $currentTime);
         $fileName = implode(".", $fileName_arr);
         $fileName_path = $tmpDir . $ITS4YouReports_xls;
         $generate->writeReportToExcelFile($fileName_path, $report_data);
         $attachments[$fileName] = $fileName_path;
     }
     foreach ($attachments as $attachmentName => $path) {
         $vtigerMailer->AddAttachment($path, "=?ISO-8859-15?Q?" . imap_8bit(html_entity_decode($attachmentName, ENT_QUOTES, "UTF-8")) . "?=");
     }
     //exit;
     $send_result = $vtigerMailer->Send(true);
     echo "SEND RESULT -> " . $send_result . "<br />";
     foreach ($attachments as $attachmentName => $path) {
         unlink($path);
     }
     ITS4YouReports::cleanITS4YouReportsCacheFiles();
     //echo "<pre>EXIST ? = ";print_r(method_exists(ITS4YouReports, "cleanITS4YouReportsCacheFiles"));echo "</pre>";
 }
 public function sendEmail()
 {
     $currentModule = vglobal('currentModule');
     require_once 'vtlib/Vtiger/Mailer.php';
     $vtigerMailer = new Vtiger_Mailer();
     $recipientEmails = $this->getRecipientEmails();
     foreach ($recipientEmails as $name => $email) {
         $vtigerMailer->AddAddress($email, $name);
     }
     $currentTime = date('Y-m-d H:i:s');
     $subject = $this->reportname . ' - ' . $currentTime . ' (' . DateTimeField::getDBTimeZone() . ')';
     $contents = getTranslatedString('LBL_AUTO_GENERATED_REPORT_EMAIL', $currentModule) . '<br/><br/>';
     $contents .= '<b>' . getTranslatedString('LBL_REPORT_NAME', $currentModule) . ' :</b> ' . $this->reportname . '<br/>';
     $contents .= '<b>' . getTranslatedString('LBL_DESCRIPTION', $currentModule) . ' :</b><br/>' . $this->reportdescription . '<br/><br/>';
     $vtigerMailer->Subject = $subject;
     $vtigerMailer->Body = $contents;
     $vtigerMailer->ContentType = "text/html";
     $baseFileName = preg_replace('/[^a-zA-Z0-9_-\\s]/', '', $this->reportname) . '__' . preg_replace('/[^a-zA-Z0-9_-\\s]/', '', $currentTime);
     $oReportRun = ReportRun::getInstance($this->id);
     $reportFormat = $this->scheduledFormat;
     $attachments = array();
     if ($reportFormat == 'pdf' || $reportFormat == 'both') {
         $fileName = $baseFileName . '.pdf';
         $filePath = 'storage/' . $fileName;
         $attachments[$fileName] = $filePath;
         $pdf = $oReportRun->getReportPDF();
         $pdf->Output($filePath, 'F');
     }
     if ($reportFormat == 'excel' || $reportFormat == 'both') {
         $fileName = $baseFileName . '.xls';
         $filePath = 'storage/' . $fileName;
         $attachments[$fileName] = $filePath;
         $oReportRun->writeReportToExcelFile($filePath);
     }
     foreach ($attachments as $attachmentName => $path) {
         $vtigerMailer->AddAttachment($path, $attachmentName);
     }
     $vtigerMailer->Send(true);
     foreach ($attachments as $attachmentName => $path) {
         unlink($path);
     }
 }
 /**
  * Function which processes request for Mail Operations
  * @global Integer $list_max_entries_per_page - Number of entries per page
  * @global PearDataBase Instance $adb
  * @global Users Instance $current_user
  * @global String $root_directory 
  * @param MailManager_Request $request
  * @return MailManager_Response
  */
 function process(MailManager_Request $request)
 {
     global $list_max_entries_per_page, $adb, $current_user;
     $response = new MailManager_Response();
     if ('open' == $request->getOperationArg()) {
         $foldername = $request->get('_folder');
         $connector = $this->getConnector($foldername);
         $folder = $connector->folderInstance($foldername);
         $connector->markMailRead($request->get('_msgno'));
         $mail = $connector->openMail($request->get('_msgno'));
         // Get updated count after opening the email
         $connector->updateFolder($folder, SA_MESSAGES | SA_UNSEEN);
         $viewer = $this->getViewer();
         $viewer->assign('FOLDER', $folder);
         $viewer->assign('MAIL', $mail);
         $uicontent = $viewer->fetch($this->getModuleTpl('Mail.Open.tpl'));
         $metainfo = array('from' => $mail->from(), 'subject' => $mail->subject(), 'msgno' => $mail->msgNo(), 'msguid' => $mail->uniqueid(), 'folder' => $foldername);
         $response->isJson(true);
         $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount(), 'ui' => $uicontent, 'meta' => $metainfo));
     } else {
         if ('mark' == $request->getOperationArg()) {
             $foldername = $request->get('_folder');
             $connector = $this->getConnector($foldername);
             $folder = $connector->folderInstance($foldername);
             $connector->updateFolder($folder, SA_UNSEEN);
             if ('unread' == $request->get('_markas')) {
                 $connector->markMailUnread($request->get('_msgno'));
             }
             $response->isJson(true);
             $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount() + 1, 'status' => true, 'msgno' => $request->get('_msgno')));
         } else {
             if ('delete' == $request->getOperationArg()) {
                 $msg_no = $request->get('_msgno');
                 $foldername = $request->get('_folder');
                 $connector = $this->getConnector($foldername);
                 $connector->deleteMail($msg_no);
                 $response->isJson(true);
                 $response->setResult(array('folder' => $foldername, 'status' => true));
             } else {
                 if ('move' == $request->getOperationArg()) {
                     $msg_no = $request->get('_msgno');
                     $foldername = $request->get('_folder');
                     $moveToFolder = $request->get('_moveFolder');
                     $connector = $this->getConnector($foldername);
                     $connector->moveMail($msg_no, $moveToFolder);
                     $response->isJson(true);
                     $response->setResult(array('folder' => $foldername, 'status' => true));
                 } else {
                     if ('send' == $request->getOperationArg()) {
                         require_once 'modules/MailManager/config.inc.php';
                         // This is to handle larger uploads
                         $memory_limit = ConfigPrefs::get('MEMORY_LIMIT');
                         ini_set('memory_limit', $memory_limit);
                         $to_string = rtrim($request->get('to'), ',');
                         $connector = $this->getConnector('__vt_drafts');
                         if (!empty($to_string)) {
                             $toArray = explode(',', $to_string);
                             foreach ($toArray as $to) {
                                 $relatedtos = MailManager::lookupMailInVtiger($to, $current_user);
                                 if (!empty($relatedtos) && is_array($relatedtos)) {
                                     for ($i = 0; $i < count($relatedtos); $i++) {
                                         if ($i == count($relatedtos) - 1) {
                                             $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                             $parentIds .= $relateto[1] . "@1";
                                         } else {
                                             $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                             $parentIds .= $relateto[1] . "@1|";
                                         }
                                     }
                                 }
                             }
                         }
                         $cc_string = rtrim($request->get('cc'), ',');
                         $bcc_string = rtrim($request->get('bcc'), ',');
                         $subject = $request->get('subject');
                         $body = $request->get('body');
                         $fromEmail = $connector->getFromEmailAddress();
                         $userFullName = $current_user->first_name . ' ' . $current_user->last_name;
                         $mailer = new Vtiger_Mailer();
                         $mailer->IsHTML(true);
                         $mailer->ConfigSenderInfo($fromEmail, $userFullName, $current_user->email1);
                         $mailer->Subject = $subject;
                         $mailer->Body = $body;
                         $tos = explode(',', $to_string);
                         $ccs = empty($cc_string) ? array() : explode(',', $cc_string);
                         $bccs = empty($bcc_string) ? array() : explode(',', $bcc_string);
                         $emailId = $request->get('emailid');
                         $attachments = $connector->getAttachmentDetails($emailId);
                         foreach ($tos as $to) {
                             $mailer->AddAddress($to);
                         }
                         foreach ($ccs as $cc) {
                             $mailer->AddCC($cc);
                         }
                         foreach ($bccs as $bcc) {
                             $mailer->AddBCC($bcc);
                         }
                         global $root_directory;
                         if (is_array($attachments)) {
                             foreach ($attachments as $attachment) {
                                 $fileNameWithPath = $root_directory . $attachment['path'] . $attachment['fileid'] . "_" . $attachment['attachment'];
                                 if (is_file($fileNameWithPath)) {
                                     $mailer->AddAttachment($fileNameWithPath, $attachment['attachment']);
                                 }
                             }
                         }
                         $status = $mailer->Send(true);
                         if ($status === true) {
                             $email = CRMEntity::getInstance('Emails');
                             $email->column_fields['assigned_user_id'] = $current_user->id;
                             $email->column_fields['date_start'] = date('Y-m-d');
                             $email->column_fields['time_start'] = date('H:i');
                             $email->column_fields['parent_id'] = $parentIds;
                             $email->column_fields['subject'] = $mailer->Subject;
                             $email->column_fields['description'] = $mailer->Body;
                             $email->column_fields['activitytype'] = 'Emails';
                             $email->column_fields['from_email'] = $mailer->From;
                             $email->column_fields['saved_toid'] = $to_string;
                             $email->column_fields['ccmail'] = $cc_string;
                             $email->column_fields['bccmail'] = $bcc_string;
                             $email->column_fields['email_flag'] = 'SENT';
                             if (empty($emailId)) {
                                 $email->save('Emails');
                             } else {
                                 $email->id = $emailId;
                                 $email->mode = 'edit';
                                 $email->save('Emails');
                             }
                             $response->isJson(true);
                             $response->setResult(array('sent' => true));
                         } else {
                             $response->isJson(true);
                             $response->setError(112, 'please verify outgoing server.');
                         }
                     } else {
                         if ('attachment_dld' == $request->getOperationArg()) {
                             $attachmentName = $request->get('_atname');
                             $attachmentName = str_replace(' ', '_', $attachmentName);
                             if (MailManager_Utils::allowedFileExtension($attachmentName)) {
                                 // This is to handle larger uploads
                                 $memory_limit = ConfigPrefs::get('MEMORY_LIMIT');
                                 ini_set('memory_limit', $memory_limit);
                                 $mail = new MailManager_Model_Message(false, false);
                                 $mail->readFromDB($request->get('_muid'));
                                 $attachment = $mail->attachments(true, $attachmentName);
                                 if ($attachment[$attachmentName]) {
                                     // Send as downloadable
                                     header("Content-type: application/octet-stream");
                                     header("Pragma: public");
                                     header("Cache-Control: private");
                                     // JFV - convert attachment file name encoding
                                     //                    header("Content-Disposition: attachment; filename=$attachmentName");
                                     // refer - http://linux.ohwada.jp/modules/smartsection/item.php?itemid=516
                                     $name = $attachmentName;
                                     // ブラウザを判定する
                                     $ua = $_SERVER['HTTP_USER_AGENT'];
                                     $browser = 'unknown';
                                     if (strstr($ua, 'MSIE') && !strstr($ua, 'Opera')) {
                                         $browser = 'msie';
                                     } elseif (strstr($ua, 'Opera')) {
                                         $browser = 'opera';
                                     } elseif (strstr($ua, 'Firefox')) {
                                         $browser = 'firefox';
                                     } elseif (strstr($ua, "Chrome")) {
                                         $browser = 'chrome';
                                     } elseif (strstr($ua, "Safari")) {
                                         $browser = 'safari';
                                     }
                                     // 英数字だけかを判定する
                                     $ascii = mb_convert_encoding($name, "US-ASCII", "UTF-8");
                                     if ($ascii == $name) {
                                         $browser = 'ascii';
                                     }
                                     // ブラウザに応じた処理
                                     switch ($browser) {
                                         // urlencode する
                                         case 'ascii':
                                             $name = str_replace(' ', '_', $name);
                                             $name = rawurlencode($name);
                                             break;
                                             // SJIS に変換する
                                         // SJIS に変換する
                                         case 'msie':
                                             $name = mb_convert_encoding($name, "SJIS", "UTF-8");
                                             break;
                                             // RFC2231形式を使用する
                                         // RFC2231形式を使用する
                                         case 'firefox':
                                         case 'chrome':
                                         case 'opera':
                                             $name = "utf-8'ja'" . rawurlencode($name);
                                             $is_rfc2231 = true;
                                             break;
                                             // UTF-8 のまま
                                         // UTF-8 のまま
                                         case 'safari':
                                             break;
                                             // 諦めて代替えを使う
                                         // 諦めて代替えを使う
                                         default:
                                             //                    		$name = $name_alt;
                                             $name = str_replace(' ', '_', $name);
                                             $name = rawurlencode($name);
                                             break;
                                     }
                                     if ($is_rfc2231) {
                                         $dis = 'Content-Disposition: attachment; filename*=';
                                     } else {
                                         $dis = 'Content-Disposition: attachment; filename=';
                                     }
                                     header($dis . $name);
                                     // JFV END
                                     echo $attachment[$attachmentName];
                                 } else {
                                     header("Content-Disposition: attachment; filename=INVALIDFILE");
                                     echo "";
                                 }
                             } else {
                                 header("Content-Disposition: attachment; filename=INVALIDFILE");
                                 echo "";
                             }
                             flush();
                             exit;
                         } elseif ('getdraftmail' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftMail = $connector->getDraftMail($request);
                             $response->isJson(true);
                             $response->setResult(array($draftMail));
                         } elseif ('save' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftId = $connector->saveDraft($request);
                             $response->isJson(true);
                             if (!empty($draftId)) {
                                 $response->setResult(array('success' => true, 'emailid' => $draftId));
                             } else {
                                 $response->setResult(array('success' => false, 'error' => "Draft was not saved"));
                             }
                         } elseif ('deleteAttachment' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $deleteResponse = $connector->deleteAttachment($request);
                             $response->isJson(true);
                             $response->setResult(array('success' => $deleteResponse));
                         } elseif ('forward' == $request->getOperationArg()) {
                             $messageId = $request->get('messageid');
                             $folderName = $request->get('folder');
                             $connector = $this->getConnector($folderName);
                             $mail = $connector->openMail($messageId);
                             $attachments = $mail->attachments(true);
                             $draftConnector = $this->getConnector('__vt_drafts');
                             $draftId = $draftConnector->saveDraft($request);
                             if (!empty($attachments)) {
                                 foreach ($attachments as $aName => $aValue) {
                                     $attachInfo = $mail->__SaveAttachmentFile($aName, $aValue);
                                     if (is_array($attachInfo) && !empty($attachInfo) && $attachInfo['size'] > 0) {
                                         if (!MailManager::checkModuleWriteAccessForCurrentUser('Documents')) {
                                             return;
                                         }
                                         $document = CRMEntity::getInstance('Documents');
                                         $document->column_fields['notes_title'] = $attachInfo['name'];
                                         $document->column_fields['filename'] = $attachInfo['name'];
                                         $document->column_fields['filestatus'] = 1;
                                         $document->column_fields['filelocationtype'] = 'I';
                                         $document->column_fields['folderid'] = 1;
                                         // Default Folder
                                         $document->column_fields['filesize'] = $attachInfo['size'];
                                         $document->column_fields['assigned_user_id'] = $current_user->id;
                                         $document->save('Documents');
                                         //save doc-attachment relation
                                         $draftConnector->saveAttachmentRel($document->id, $attachInfo['attachid']);
                                         //save email-doc relation
                                         $draftConnector->saveEmailDocumentRel($draftId, $document->id);
                                         //save email-attachment relation
                                         $draftConnector->saveAttachmentRel($draftId, $attachInfo['attachid']);
                                         $attachmentInfo[] = array('name' => $attachInfo['name'], 'size' => $attachInfo['size'], 'emailid' => $draftId, 'docid' => $document->id);
                                     }
                                     unset($aValue);
                                 }
                             }
                             $response->isJson(true);
                             $response->setResult(array('attachments' => $attachmentInfo, 'emailid' => $draftId));
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }