Beispiel #1
0
 /**
  * @param string $type
  * @return string
  */
 function generateTemplate($type = 'html')
 {
     $entity = $this->getEntity();
     $config = $this->getConfig();
     $view = new \Zend\View\Renderer\PhpRenderer();
     $view->setResolver($this->pathStack);
     if (strlen($entity->getTemplate()) > 0) {
         $templatePath = 'email/';
         if (strpos($entity->getTemplate(), '/') === false) {
             $templatePath .= $entity->getTemplate();
         } else {
             $templatePath = $entity->getTemplate();
         }
     } else {
         $templatePath = 'email/default';
     }
     if ($type == 'html') {
         $content = $entity->getHtml();
     } else {
         $content = $entity->getText();
     }
     $viewModel = new \Zend\View\Model\ViewModel(array_merge(array('content' => $content), $entity->getVars()));
     $viewModel->setTemplate($templatePath);
     $layout = new \Zend\View\Model\ViewModel(array('content' => $view->render($viewModel), 'site_name' => $config['site_name']));
     $layout->setTerminal(true);
     $layout->setTemplate('layout/' . $type);
     return $view->render($layout);
 }
Beispiel #2
0
 public function getControllerPluginConfig()
 {
     return array('factories' => array('mailerZF2' => function ($sm) {
         $serviceLocator = $sm->getServiceLocator();
         $config = $serviceLocator->get('Configuration');
         if (!isset($config['email'])) {
             throw new \Exception('Configurazione delle mails non impostata.');
         }
         $renderer = new \Zend\View\Renderer\PhpRenderer();
         $resolver = new \Zend\View\Resolver\TemplatePathStack();
         $resolver->setPaths(array(__DIR__ . '/../../../module/'));
         $renderer->setResolver($resolver);
         $controllerPlugin = new Controller\Plugin\Mailer($renderer);
         $mail = $config['email']['credentials']['from_mail'];
         $name = $config['email']['credentials']['from_name'];
         $controllerPlugin->setFrom($mail, $name);
         switch ($config['email']['transport']) {
             case 'smtp':
                 $transport = new \Zend\Mail\Transport\Smtp();
                 $options = new \Zend\Mail\Transport\SmtpOptions(array('host' => $config['email']['smtp']['host'], 'port' => $config['email']['smtp']['port'], 'connection_class' => $config['email']['smtp']['connection_class'], 'connection_config' => $config['email']['smtp']['connection_config']));
                 $transport->setOptions($options);
                 $controllerPlugin->setMailTransport($transport);
                 break;
             case 'sendmail':
             default:
                 $transport = new \Zend\Mail\Transport\Sendmail();
                 $controllerPlugin->setMailTransport($transport);
                 break;
         }
         return $controllerPlugin;
     }));
 }
Beispiel #3
0
 /**
  * Get a working renderer.
  *
  * @param array  $plugins Custom VuFind plug-ins to register
  * @param string $theme   Theme directory to load from
  *
  * @return \Zend\View\Renderer\PhpRenderer
  */
 protected function getPhpRenderer($plugins = [], $theme = 'blueprint')
 {
     $resolver = new \Zend\View\Resolver\TemplatePathStack();
     // This assumes that all themes will be testing inherit directly
     // from root with no intermediate themes.  Probably safe for most
     // test situations, though other scenarios are possible.
     $resolver->setPaths([$this->getPathForTheme('root'), $this->getPathForTheme($theme)]);
     $renderer = new \Zend\View\Renderer\PhpRenderer();
     $renderer->setResolver($resolver);
     if (!empty($plugins)) {
         $pluginManager = $renderer->getHelperPluginManager();
         foreach ($plugins as $key => $value) {
             $pluginManager->setService($key, $value);
         }
     }
     return $renderer;
 }
<?php

require './vendor/autoload.php';
$vm = new \Zend\View\Model\ViewModel(array('nom' => 'tintin'));
$vm->setVariables(array('title' => 'tintin', 'description' => 'bande dessinée', 'link' => 'http://manews.fr'));
$vm->setTemplate('liste');
$resol = new Zend\View\Resolver\TemplateMapResolver(array('liste' => __DIR__ . '/liste.phtml'));
$rendu = new \Zend\View\Renderer\PhpRenderer();
$rendu->setResolver($resol);
echo $rendu->render($vm);
Beispiel #5
0
 public function lostPasswordAction()
 {
     $viewModel = new ViewModel();
     $form = new LostPasswordForm();
     $accountFilter = new AccountFIlter();
     $filter = new InputFilter();
     $filter->add($accountFilter->get('email'));
     $form->setInputFilter($filter);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $data = $request->getPost();
         $form->setData($data);
         if ($form->isValid()) {
             $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
             // 找到使用者
             if ($user = $em->getRepository('Base\\Entity\\User')->findOneBy(array('email' => $form->get('email')->getValue()))) {
                 $md5 = md5(time() . $user->getUserName() . $user->getEmail());
                 $user->setCheckCode($md5);
                 $em->persist($user);
                 $em->flush();
                 $view = new \Zend\View\Renderer\PhpRenderer();
                 $resolver = new \Zend\View\Resolver\TemplateMapResolver();
                 $resolver->setMap(array('mailLayout' => __DIR__ . '/../../../../Application/view/layout/layout-mail.phtml', 'mailTemplate' => __DIR__ . '/../../../view/user/sign/lost-password-mail.phtml'));
                 $view->setResolver($resolver);
                 $uri = $this->getRequest()->getUri();
                 $scheme = $uri->getScheme();
                 $host = $uri->getHost();
                 $base = sprintf('%s://%s%s', $scheme, $host, $this->getRequest()->getBasePath());
                 $viewModel->setTemplate('mailTemplate')->setVariables(array('user' => $user, 'url' => $base));
                 $content = $view->render($viewModel);
                 $viewLayout = new ViewModel();
                 $viewLayout->setTemplate('mailLayout')->setVariables(array('content' => $content));
                 $body = $view->render($viewLayout);
                 $bodyPart = new \Zend\Mime\Message();
                 $bodyMessage = new \Zend\Mime\Part($body);
                 $bodyMessage->type = 'text/html';
                 $bodyPart->setParts(array($bodyMessage));
                 $message = new Mail\Message();
                 $message->addTo($user->getEmail(), $user->getusername());
                 $message->setFrom('*****@*****.**', '系統通知信');
                 $message->setSubject('重設密碼通知信');
                 $message->setBody($bodyPart);
                 $message->setEncoding('UTF-8');
                 $transport = new Mail\Transport\Sendmail();
                 $transport->send($message);
                 // $viewModel->setTemplate('user/login/send-mail.phtml');
                 $this->flashmessenger()->addMessage($user->getEmail());
                 return $this->redirect()->toRoute('user/default', array('controller' => 'sign', 'action' => 'send-mail'));
                 // return $viewModel;
             } else {
                 $this->flashmessenger()->addMessage('您輸入了錯誤的帳號或電子郵件');
             }
         }
     }
     $viewModel->setVariable('form', $form);
     $viewModel->setVariable('flashMessages', $this->flashMessenger()->getMessages());
     return $viewModel;
 }
<?php

require_once './autoloader.php';
use Zend\Mail\Message;
use Zend\Mail\Transport;
use Zend\Di\Di;
use Zend\Di\Config as DiConfig;
$diConfig = array('instance' => array('Zend\\Mail\\Transport\\FileOptions' => array('parameters' => array('path' => __DIR__)), 'Zend\\Mail\\Transport\\File' => array('injections' => array('Zend\\Mail\\Transport\\FileOptions')), 'Zend\\Mail\\Transport\\SmtpOptions' => array('parameters' => array('name' => 'sendgrid', 'host' => 'smtp.sendgrid.net', 'port' => 25, 'connectionClass' => 'login', 'connectionConfig' => array('username' => '*****@*****.**', 'password' => 'password'))), 'Zend\\Mail\\Message' => array('parameters' => array('headers' => 'Zend\\Mail\\Headers', 'Zend\\Mail\\Message::setTo:emailOrAddressList' => '*****@*****.**', 'Zend\\Mail\\Message::setTo:name' => 'EvaEngine', 'Zend\\Mail\\Message::setFrom:emailOrAddressList' => '*****@*****.**', 'Zend\\Mail\\Message::setFrom:name' => 'EvaEngine', 'setBody' => 'Zend\\View\\Renderer\\PhpRenderer::render')), 'Zend\\Mail\\Transport\\Smtp' => array('injections' => array('Zend\\Mail\\Transport\\SmtpOptions'))));
$di = new Di();
$di->configure(new DiConfig($diConfig));
$transport = $di->get('Zend\\Mail\\Transport\\Smtp');
$transport = $di->get('Zend\\Mail\\Transport\\Sendmail');
$transport = $di->get('Zend\\Mail\\Transport\\File');
$message = $di->get('Zend\\Mail\\Message');
$view = new Zend\View\Renderer\PhpRenderer();
$resolver = new Zend\View\Resolver\TemplatePathStack();
$resolver->setPaths(array('mailTemplate' => __DIR__));
$view->setResolver($resolver);
$viewModel = new Zend\View\Model\ViewModel();
$viewModel->setTemplate('mail/template')->setVariables(array('user' => 'AlloVince'));
$message->setSubject("Zend Mail with Template")->setBody($view->render($viewModel));
$transport->send($message);
 /**
  * Send reminders for a user.
  *
  * @param \Finna\Db\Table\Row\User $user        User.
  * @param array                    $remindLoans Loans to be reminded.
  *
  * @return boolean success.
  */
 protected function sendReminder($user, $remindLoans)
 {
     if (!$user->email || trim($user->email) == '') {
         $this->msg("User {$user->username} (id {$user->id})" . ' does not have an email address, bypassing due date reminders');
         return false;
     }
     list($userInstitution, ) = explode(':', $user['username'], 2);
     if (!$this->currentInstitution || $userInstitution != $this->currentInstitution) {
         $templateDirs = ["{$this->baseDir}/themes/finna/templates"];
         if (!($viewPath = $this->resolveViewPath($userInstitution))) {
             $this->err("Could not resolve view path for user {$user->username}" . " (id {$user->id})");
             return false;
         } else {
             $templateDirs[] = "{$viewPath}/themes/custom/templates";
         }
         $this->currentInstitution = $userInstitution;
         $this->currentViewPath = $viewPath;
         $resolver = new AggregateResolver();
         $this->renderer->setResolver($resolver);
         $stack = new TemplatePathStack(['script_paths' => $templateDirs]);
         $resolver->attach($stack);
         $siteConfig = $viewPath . '/local/config/vufind/config.ini';
         $this->currentSiteConfig = parse_ini_file($siteConfig, true);
     }
     $language = isset($this->currentSiteConfig['Site']['language']) ? $this->currentSiteConfig['Site']['language'] : 'fi';
     $validLanguages = array_keys($this->currentSiteConfig['Languages']);
     if (!empty($user->finna_language) && in_array($user->finna_language, $validLanguages)) {
         $language = $user->finna_language;
     }
     $this->translator->addTranslationFile('ExtendedIni', null, 'default', $language)->setLocale($language);
     $key = $this->dueDateReminderTable->getUnsubscribeSecret($this->hmac, $user, $user->id);
     $params = ['id' => $user->id, 'type' => 'reminder', 'key' => $key];
     $unsubscribeUrl = $this->urlHelper->__invoke('myresearch-unsubscribe') . '?' . http_build_query($params);
     $urlParts = explode('/', $this->currentViewPath);
     $urlView = array_pop($urlParts);
     $urlInstitution = array_pop($urlParts);
     $baseUrl = 'https://' . $urlInstitution . '.finna.fi';
     if ($urlView != $this::DEFAULT_PATH) {
         $baseUrl .= "/{$urlView}";
     }
     $params = ['loans' => $remindLoans, 'url' => $baseUrl . $this->urlHelper->__invoke('myresearch-checkedout'), 'unsubscribeUrl' => $baseUrl . $unsubscribeUrl, 'baseUrl' => $baseUrl];
     $subject = $this->translator->translate('due_date_email_subject');
     $message = $this->renderer->render("Email/due-date-reminder.phtml", $params);
     try {
         $to = $user->email;
         $from = $this->currentSiteConfig['Site']['email'];
         $this->serviceManager->get('VuFind\\Mailer')->send($to, $from, $subject, $message);
     } catch (\Exception $e) {
         $this->err("Failed to send due date reminders to user {$user->username} " . " (id {$user->id})");
         $this->err('   ' . $e->getMessage());
         return false;
     }
     foreach ($remindLoans as $loan) {
         $params = ['user_id' => $user->id, 'loan_id' => $loan['loanId']];
         $this->dueDateReminderTable->delete($params);
         $dueDate = new \DateTime($loan['dueDate']);
         $params['due_date'] = $dueDate->format($this::DUE_DATE_FORMAT);
         $params['notification_date'] = gmdate($this::DUE_DATE_FORMAT, time());
         $this->dueDateReminderTable->insert($params);
     }
     return true;
 }
<?php

return array('service_manager' => array('factories' => array('MailHtmlGenerator' => function ($sm) {
    $config = $sm->get('Config');
    $mail = new \EmailHtmlGenerator\Service\MailHtmlGenerator();
    $transport = new \Zend\Mail\Transport\Sendmail();
    $mail->setTransport($transport);
    $viewRenderer = new \Zend\View\Renderer\PhpRenderer();
    $resolver = new \Zend\View\Resolver\AggregateResolver();
    $viewRenderer->setResolver($resolver);
    $viewsMap = new \Zend\View\Resolver\TemplatePathStack();
    $viewsMap->addPaths($config['view_manager']['template_path_stack']);
    $resolver->attach($viewsMap);
    $mail->setViewRenderer($viewRenderer);
    return $mail;
})));