/** * Set template * @param string $template_path Relative path from resource_path * @param mixed{} $vars bind variables * @return $this */ public function set_template($template_path, $vars = []) { /** * @param string $path Email template resources root */ $resource_path = \ebi\Conf::get('resource_path', \ebi\Conf::resource_path('mail')); $path = \ebi\Util::path_absolute($resource_path, $template_path); if (!is_file($path)) { throw new \ebi\exception\InvalidArgumentException($template_path . ' not found'); } $xml = \ebi\Xml::extract(file_get_contents($path), 'mail'); try { try { $from = $xml->find_get('from'); $this->from($from->in_attr('address'), $from->in_attr('name')); } catch (\ebi\exception\NotFoundException $e) { } foreach ($xml->find('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } try { $this->return_path($xml->find_get('return_path')->in_attr('address')); } catch (\ebi\exception\NotFoundException $e) { } /** * @param string $xtc_name xtc query key */ $xtc_name = \ebi\Conf::get('xtc_name', 'xtc'); $xtc = self::xtc($template_path); $this->header['X-T-Code'] = $xtc; $vars['t'] = new \ebi\FlowHelper(); $vars['xtc'] = [$xtc_name => $xtc]; $subject = trim(str_replace(["\r\n", "\r", "\n"], '', $xml->find_get('subject')->value())); $template = new \ebi\Template(); $template->cp($vars); $body_xml = $xml->find_get('body'); $signature = $body_xml->in_attr('signature'); $signature_text = ''; if (!empty($signature)) { $sig_path = \ebi\Util::path_absolute($resource_path, $signature); if (!is_file($sig_path)) { throw new \ebi\exception\InvalidArgumentException($signature . ' not found'); } $sig_xml = \ebi\Xml::extract(file_get_contents($sig_path), 'mail'); $signature_text = \ebi\Util::plain_text(PHP_EOL . $sig_xml->find_get('signature')->value() . PHP_EOL); } $message = $template->get(\ebi\Util::plain_text(PHP_EOL . $body_xml->value() . PHP_EOL) . $signature_text); $this->message($message); $this->subject($template->get($subject)); try { $html = $xml->find_get('html'); $html_path = \ebi\Util::path_absolute($resource_path, $html->in_attr('src', preg_replace('/^(.+)\\.\\w+$/', '\\1', $path) . '.html')); foreach ($html->find('media') as $media) { $file = \ebi\Util::path_absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \ebi\exception\InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \ebi\Template(); $template->cp($vars); $this->html($template->read($html_path)); } catch (\ebi\exception\NotFoundException $e) { } foreach ($xml->find('attach') as $attach) { $file = \ebi\Util::path_absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \ebi\exception\InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } catch (\ebi\exception\NotFoundException $e) { throw new \ebi\exception\InvalidArgumentException($template_path . ' invalid data'); } }