Ejemplo n.º 1
0
 /**
  * @param string $templateName
  * @param string $email
  * @param array $params
  * @return boolean
  */
 public function sendMail($templateName, $email, array $params = array())
 {
     $config = $this->getServiceLocator()->get('Config');
     $settings = $this->getServiceLocator()->get('Settings');
     /** @var \Doctrine\ORM\EntityManager $entityManager */
     $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     if (!isset($config['mailer']['templates_path'])) {
         throw new \Exception('Mail templates path is not set');
     }
     // get from addresss
     if (!isset($params['from'])) {
         $params['from'] = $settings->get('mail:from');
     }
     $from = $params['from'];
     if ($from == '') {
         throw new \Exception('Can\'t send mail - from address not given');
     }
     // get language
     if (!isset($params['language']) || $params['language'] == '') {
         throw new \Exception('Can\'t send mail - language not given');
     }
     $language = $params['language'];
     // get mail template
     //        $templatesTable = $this->getServiceLocator()->get('Msingi\Cms\Db\Table\MailTemplates');
     $templates_repository = $entityManager->getRepository('Msingi\\Cms\\Entity\\MailTemplate');
     $template = $templates_repository->fetchOrCreate($templateName);
     /** @var \Msingi|Cms\Repository\MailTemplatesI18n $templates_i18n_repository */
     $templates_i18n_repository = $entityManager->getRepository('Msingi\\Cms\\Entity\\MailTemplateI18n');
     /** @var \Msingi\Cms\Entity\MailTemplateI18n $i18n */
     $i18n = $templates_i18n_repository->fetchOrCreate($template, $language);
     // replace tokens
     $subject = $this->processTokens($i18n->getSubject(), $params);
     $message = $this->processTokens($i18n->getTemplate(), $params);
     // initialize renderer
     $renderer = new PhpRenderer();
     $resolver = new Resolver\AggregateResolver();
     $renderer->setResolver($resolver);
     $stack = new Resolver\TemplatePathStack(array('script_paths' => array($config['mailer']['templates_path'])));
     $resolver->attach($stack);
     // render message template
     $messageHtml = $renderer->render('default', array('content' => $message));
     // get text content of the message
     $html2text = new HTML2Text();
     $html2text->html2text($messageHtml);
     // create text message part
     $messageTextPart = new Mime\Part($html2text->get_text());
     $messageTextPart->type = 'text/plain';
     // create html message part
     $messageHtmlPart = new Mime\Part($messageHtml);
     $messageHtmlPart->type = 'text/html';
     // create message body
     $messageBody = new Mime\Message();
     $messageBody->setParts(array($messageTextPart, $messageHtmlPart));
     // @todo Implement attachements
     //
     $mail = new Mail\Message();
     $mail->setFrom($from);
     if (isset($params['reply-to']) && $params['reply-to'] != '') {
         $mail->setReplyTo($params['reply-to']);
     }
     $mail->addTo($email);
     $mail->setEncoding('UTF-8');
     $mail->setSubject($subject);
     $mail->setBody($messageBody);
     $mail->getHeaders()->get('Content-Type')->setType('multipart/alternative');
     //
     $mailer_config = $config['mailer'];
     // log message
     if ($settings->get('mail:log')) {
         if (!is_dir($config['mailer']['log_dir'])) {
             mkdir($config['mailer']['log_dir']);
         }
         $transport = new \Zend\Mail\Transport\File();
         $options = new \Zend\Mail\Transport\FileOptions(array('path' => $config['mailer']['log_dir'], 'callback' => function (\Zend\Mail\Transport\File $transport) use($templateName) {
             return date('Ymd.His') . '-' . $templateName . '-' . mt_rand() . '.txt';
         }));
         $transport->setOptions($options);
         $transport->send($mail);
     }
     // send message
     if ($settings->get('mail:send')) {
         if (isset($mailer_config['transport']) && $mailer_config['transport'] == 'smtp') {
             $transport = new SmtpTransport();
             $options = new Mail\Transport\SmtpOptions($mailer_config['smtp_options']);
             $transport->setOptions($options);
         } else {
             $transport = new SendmailTransport();
         }
         $transport->send($mail);
     }
     return true;
 }
Ejemplo n.º 2
0
function tiki_mail_setup()
{
    static $done = false;
    if ($done) {
        return;
    }
    global $tiki_maillib__zend_mail_default_transport;
    global $prefs;
    if ($prefs['zend_mail_handler'] == 'smtp') {
        $options = array('host' => $prefs['zend_mail_smtp_server']);
        if ($prefs['zend_mail_smtp_auth']) {
            $options['connection_class'] = $prefs['zend_mail_smtp_auth'];
            $options['connection_config'] = array('username' => $prefs['zend_mail_smtp_user'], 'password' => $prefs['zend_mail_smtp_pass']);
        }
        if ($prefs['zend_mail_smtp_port']) {
            $options['port'] = $prefs['zend_mail_smtp_port'];
        }
        if ($prefs['zend_mail_smtp_security']) {
            $options['connection_config']['ssl'] = $prefs['zend_mail_smtp_security'];
        }
        if ($prefs['openpgp_gpg_pgpmimemail'] == 'y') {
            $transport = new OpenPGP_Zend_Mail_Transport_Smtp();
        } else {
            $transport = new Zend\Mail\Transport\Smtp();
        }
        $transportOptions = new Zend\Mail\Transport\SmtpOptions($options);
        $transport->setOptions($transportOptions);
    } elseif ($prefs['zend_mail_handler'] == 'file') {
        $transport = new Zend\Mail\Transport\File();
        $transportOptions = new Zend\Mail\Transport\FileOptions(array('path' => TIKI_PATH . '/temp', 'callback' => function ($transport) {
            return 'Mail_' . date('YmdHis') . '_' . mt_rand() . '.tmp';
        }));
        $transport->setOptions($transportOptions);
    } elseif ($prefs['zend_mail_handler'] == 'sendmail' && !empty($prefs['sender_email'])) {
        // from http://framework.zend.com/manual/1.12/en/zend.mail.introduction.html#zend.mail.introduction.sendmail
        $transport = new Zend\Mail\Transport\Sendmail('-f' . $prefs['sender_email']);
    } else {
        $transport = new Zend\Mail\Transport\Sendmail();
    }
    $tiki_maillib__zend_mail_default_transport = $transport;
    $done = true;
}
Ejemplo n.º 3
0
                 if ($state->universal['email']['smtp']['connection_class'] !== 'smtp') {
                     $transportConfig['connection_config'] = ['username' => $state->universal['email']['smtp']['username'], 'password' => $state->universal['email']['smtp']['password']];
                 }
                 if (!empty($state->universal['email']['smtp']['disable_tls'])) {
                     $transportConfig['connection_config']['port'] = !empty($state->universal['email']['smtp']['port']) ? $state->universal['email']['smtp']['port'] : 25;
                 } else {
                     $transportConfig['connection_config']['ssl'] = 'tls';
                     $transportConfig['port'] = !empty($state->universal['email']['smtp']['port']) ? $state->universal['email']['smtp']['port'] : 587;
                 }
                 $transport->setOptions(new \Zend\Mail\Transport\SmtpOptions($transportConfig));
                 break;
             case 'File':
                 $transport = new Zend\Mail\Transport\File();
                 /** @noinspection PhpUnusedParameterInspection */
                 $transport->setOptions(new \Zend\Mail\Transport\FileOptions(['path' => !empty($state->universal['email']['file']['path']) ? $state->universal['email']['file']['path'] : ROOT . '/files/email', 'callback' => function (Zend\Mail\Transport\File $t) : string {
                     return \implode('_', ['Message', \date('YmdHis'), \Airship\uniqueId(12) . '.txt']);
                 }]));
                 break;
             case 'Sendmail':
                 if (!empty($state->universal['email']['sendmail']['parameters'])) {
                     $transport = new Zend\Mail\Transport\Sendmail($state->universal['email']['sendmail']['parameters']);
                 } else {
                     $transport = new Zend\Mail\Transport\Sendmail();
                 }
                 break;
             default:
                 throw new Exception(\trk('errors.email.invalid_transport', \print_r($state->universal['email']['transport'], true)));
         }
     }
     $state->mailer = $transport;
 }