public function execute() { set_time_limit(0); $notifiedEmails = array(); $emailScheduling = SJB_Settings::getSettingByName('email_scheduling'); $numberEmails = SJB_Settings::getSettingByName('number_emails'); $emailsSend = SJB_Settings::getSettingByName('send_emails'); $limit = $numberEmails - $emailsSend; $limit = $limit > 0 ? $limit : 20; $letters = SJB_DB::query('SELECT * FROM `email_scheduling` ORDER BY `id` ASC LIMIT 0, ?n', $limit); if ($emailScheduling && $numberEmails || count($letters)) { foreach ($letters as $letter) { $params = $letter; unset($params['id']); $email = new SJB_Email($params['email']); $email->setSubject($params['subject']); $email->setText($params['text']); $email->setFile($params['file']); if ($email->send(true)) { SJB_DB::query('DELETE FROM `email_scheduling` WHERE `id` = ?n', $letter['id']); array_push($notifiedEmails, $params['email']); } } } $tp = SJB_System::getTemplateProcessor(); $tp->assign('notified_emails', $notifiedEmails); $schedulerLog = $tp->fetch('email_scheduler_log.tpl'); SJB_HelperFunctions::writeCronLogFile('email_scheduler.log', $schedulerLog); }
/** * retrieve email SJB_Email with defined options: * - from name * - from email * - attached file * - cc * ATTENTION: reserved index for $data: * - emailData * * @static * @param string $email * @param int $emailTplSID * @param array $data (message, subject, signature) are reserved indexes * @return SJB_Email */ public static function getEmail($email, $emailTplSID, $data = array()) { if (self::EMAIL_DO_NOT_SEND === $emailTplSID) { return new SJB_EmailDoNotSend(); } $templateData = self::prepareEmailData($emailTplSID); if (!$templateData) { return new SJB_EmailNone($email); } $message = SJB_Array::get($templateData, 'text'); $subject = SJB_Array::get($templateData, 'subject'); $signature = SJB_Settings::getSettingByName('system_email_signature'); $cc = SJB_Array::get($templateData, 'cc'); $fromEmail = SJB_Array::get($templateData, 'from_email'); $fromName = SJB_Array::get($templateData, 'from_name'); $file = SJB_Array::get($templateData, 'file'); self::replaceSmartyTags($fromName); self::replaceSmartyTags($message); self::replaceSmartyTags($subject); self::replaceSmartyTags($signature); $data[SJB_EmailInternal::EMAIL_DATA_LABEL] = array('fromName' => $fromName, 'message' => $message, 'subject' => $subject, 'signature' => $signature); $email = new SJB_Email($email, '../miscellaneous/email_theme.tpl', $data); if ($cc) { $email->addCC($cc); } if ($fromEmail) { $email->setFromEmail($fromEmail); } // file attachment if (SJB_Array::get($file, 'file_id')) { $file_link = SJB_UploadFileManager::getUploadedFileLink(SJB_Array::get($file, 'file_id'), false, true); if ($file_link) { $email->setFile(SJB_BASE_DIR . $file_link); } } return $email; }
/** * @param $emailSID * @throws Exception */ protected function resendEmailBySID($emailSID) { $emailToSend = SJB_EmailLogManager::getObjectBySID($emailSID); if (!$emailToSend instanceof SJB_EmailLog) { throw new Exception('Not valid email log ID to resend'); } $email = new SJB_Email($emailToSend->getPropertyValue('email')); $email->setSubject($emailToSend->getPropertyValue('subject')); $email->setText($emailToSend->getPropertyValue('message')); if (!$email->send()) { throw new Exception('Email log > Resend Function: The message were not resent. Please check the settings'); } }