* @copyright Since 2013 Ovidiu Cimpean * @license Do not edit, modify or copy this file * * @version Release: 4 */ require_once dirname(__FILE__).'/NewsletterProPrepareNewsletter.php'; class NewsletterProSendManager { public $prepare; public $fwd_success_count = 0; public static $instance; private $template_name; public function __construct() { $this->context = Context::getContext(); $this->prepare = NewsletterProPrepareNewsletter::newInstance(); } public static function newInstance() { return new self(); } public static function getInstance() { if (!isset(self::$instance)) { self::$instance = self::newInstance(); } return self::$instance; } public function setTemplateNameForAttachment($template_name) { $this->template_name = $template_name; } /** * Send newsletter. * * @param string $subject * @param string $template * @param string $to * @param array $data * @param array $forward * @param bool $shut_down_register This should be true only in the progress of sending newsletters in the bakcofice * * @return array */ public function sendNewsletter($subject, $template, $to, $data = [], $forward = [], $shut_down_register = true, $exclude_email = false) { if ($shut_down_register) { register_shutdown_function([$this, 'sendNewslettersShutdown']); } $errors = []; $id_smtp = null; $send_method = null; $user = null; // id_smtp, send_method, user extract($data); if (isset($user)) { $this->setUserContext($user); } if (isset($send_method) && 'mail' == $send_method) { $mail = NewsletterProMail::getInstance(NewsletterProMail::getDefaultMail()); } elseif (isset($id_smtp)) { $mail = NewsletterProMail::newInstance((int) $id_smtp); } else { $mail = NewsletterProMail::getInstanceByContext(); } if ($mail) { if (isset($this->template_name)) { $mail->setTemplateNameForAttachment($this->template_name); } if ($exclude_email) { $errors[] = NewsletterPro::getInstance()->l('The email is excluded from the process of sending. The email exists into exclusion list.'); return $errors; } $this->fwd_success_count = 0; if (pqnp_config('DEBUG_MODE')) { $send = $mail->send($subject, $template, $to); } else { $send = @$mail->send($subject, $template, $to); } if ($send) { if ((bool) pqnp_config('FWD_FEATURE_ACTIVE') && self::isForward($forward)) { $mail->sendForward($forward['data'], $forward['type'], $forward['from'], (int) pqnp_config('SLEEP')); $this->fwd_success_count += $mail->getSuccessFwdCount(); } return true; } else { $errors = array_merge($errors, $mail->getErrors()); } } else { $errors[] = NewsletterPro::getInstance()->l('Cannot establish the email connection.'); } return $errors; } /** * Register send newsletter shutdown function. * * @return die */ public function sendNewslettersShutdown() { $errors = []; $response = [ 'status' => false, 'exit' => false, 'errors' => &$errors, ]; $ob_content = ob_get_contents(); @ob_end_clean(); if (preg_match('/Fatal error/', $ob_content)) { if (preg_match('/Maximum execution time/', $ob_content)) { $errors[] = NewsletterPro::getInstance()->l('PHP max execution time exceeded because of the followers. The newsletter will jump to the next email address.'); } else { $errors[] = $ob_content; } exit(Tools::jsonEncode($response)); } exit($ob_content); } /** * Set user context. * * @param object $user */ public function setUserContext($user) { if (isset($user) && 'object' == gettype($user)) { if ('customer' === $user->user_type || $user->hasEmployeeCustomerData()) { $this->context = NewsletterProCustomerContext::getContext((int) $user->id); } } } /** * Check if the email si forward. * * @param string $forward * * @return bool */ public static function isForward($forward) { if (is_array($forward) && !empty($forward) && isset($forward['data'], $forward['type'], $forward['from'])) { return true; } return false; } /** * Build forwarder. * * @param array $data * @param string $type * @param string $to * * @return array */ public static function buildForward($data, $type, $to) { return [ 'data' => $data, 'type' => $type, 'from' => $to, ]; } public static function sendTestTerminal($to, $id_smtp = 0) { $output = [ 'success' => [], 'errors' => [], ]; if ($id_smtp > 0) { $mail = NewsletterProMail::newInstance((int) $id_smtp); if (!Validate::isLoadedObject($mail)) { $output['errors'][] = 'Unable to load the mail object.'; return false; } } else { $mail = NewsletterProMail::getInstanceByContext(); if (!$mail) { $output['errors'][] = 'Unable to load the mail object.'; return false; } } $subject = Context::getContext()->shop->name.' - '.NewsletterPro::getInstance()->l('Test Email Connection'); $template = '