protected function send($level, \org\rhaco\Log $log) { if (empty($this->template_base)) { $this->template_base = \org\rhaco\Conf::get('template_base', \org\rhaco\io\File::resource_path('log_mail')); } $template = \org\rhaco\net\Path::absolute($this->template_base, $level . '_log.xml'); if (is_file($template)) { $mail = new \org\rhaco\net\mail\Mail(); $mail->send_template($template, array('log' => $log, 'env' => new \org\rhaco\lang\Env())); } }
public function __construct($app_url = null) { $entry_file = $this->entry_file(); $branch_url = basename($entry_file); $rewrite = \org\rhaco\Conf::get('rewrite_entry'); $this->app_url = \org\rhaco\Conf::get('app_url'); $this->media_url = \org\rhaco\Conf::get('media_url'); $path = function ($url) { if (!empty($url)) { $url = str_replace('\\', '/', $url); if (substr($url, -1) != '/') { $url .= '/'; } } return $url; }; if (empty($this->app_url)) { $host = \org\rhaco\Request::host(); if (!empty($host)) { $hasport = (bool) preg_match('/:\\d+/', $host); $this->app_url = $host . ($hasport ? '' : '/' . dirname(preg_replace("/.+\\/workspace\\/(.+)/", "\\1", $entry_file))); if (!isset($rewrite)) { $rewrite = $hasport ? false : true; } } else { $this->app_url = 'http://localhost:8000/'; $rewrite = false; } } if (substr($branch_url, 0, 1) == '/') { $branch_url = substr($branch_url, 1); } if ($rewrite === null || $rewrite === true) { $branch_url = basename($branch_url, '.php'); if ($branch_url == 'index') { $branch_url = ''; } } $this->app_url = str_replace('https://', 'http://', $path($this->app_url)); $this->branch_url = $this->app_url . $path($branch_url); $this->media_url = $path(empty($this->media_url) ? $this->app_url . 'resources/media/' : $this->media_url); $this->template_path = $path(\org\rhaco\Conf::get('template_path', \org\rhaco\io\File::resource_path('templates'))); $this->template = new \org\rhaco\Template(); }
/** * テンプレートから内容を取得しセットする * * テンプレートサンプル * <mail> * <from address="*****@*****.**" name="tokushima" /> * <subject>メールのタイトル</subject> * <body> * メールの本文 * </body> * </mail> * * @param string $template_path テンプレートファイルパス * @param mixed{} $vars テンプレートへ渡す変数 * @return $this */ public function set_template($template_path, $vars = array()) { $resource_path = empty($this->resource_path) ? \org\rhaco\Conf::get('resource_path', \org\rhaco\io\File::resource_path('mail')) : $this->resource_path; $template_path = \org\rhaco\net\Path::absolute($resource_path, $template_path); if (!is_file($template_path)) { throw new \InvalidArgumentException($template_path . ' not found'); } if (\org\rhaco\Xml::set($xml, file_get_contents($template_path), 'mail')) { $from = $xml->f('from'); if ($from !== null) { $this->from($from->in_attr('address'), $from->in_attr('name')); } foreach ($xml->in('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } $return_path = $xml->f('return_path'); if ($return_path !== null) { $this->return_path($return_path->in_attr('address')); } $notification = $xml->f('notification'); if ($notification !== null) { $this->notification($notification->in_attr('address')); } $reply_to = $xml->f('reply_to'); if ($reply_to !== null) { $this->reply_to($reply_to->in_attr('address')); } $errors_to = $xml->f('errors_to'); if ($errors_to !== null) { $this->errors_to($errors_to->in_attr('address')); } $subject = trim(str_replace(array("\r\n", "\r", "\n"), '', $xml->f('subject.value()'))); $body = $xml->f('body.value()'); $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->subject($template->get($subject)); $this->message(\org\rhaco\lang\Text::plain("\n" . $template->get($body) . "\n")); $html = $xml->f('html'); if ($html !== null) { $html_path = \org\rhaco\net\Path::absolute($resource_path, $html->in_attr('src')); foreach ($html->in('media') as $media) { $file = \org\rhaco\net\Path::absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->html($template->read($html_path)); } foreach ($xml->in('attach') as $attach) { $file = \org\rhaco\net\Path::absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } throw new \InvalidArgumentException($template_path . ' invalid data'); }