Example #1
0
 public function testAlwaysGetSingleSender()
 {
     $message = new Message();
     $message->setSender('*****@*****.**');
     $this->assertEquals(['*****@*****.**' => null], $message->getSender(), "Simple email address as string does not work for setSender().");
     $message->setSender(['*****@*****.**', '*****@*****.**']);
     $this->assertEquals(['*****@*****.**' => null], $message->getSender(), "Only first of the two emails should have been set for setSender().");
     $message->setSender(['*****@*****.**']);
     $this->assertEquals(['*****@*****.**' => null], $message->getSender(), "Simple array with one email address should work with setSender().");
 }
 /**
  * This method constructs a Message from the given twig mail template.
  *
  * A valid twig mail template is a file with a '.mail.twig' extension,
  * that has multiple blocks with content:
  *
  * - 'subject' - subject of the message
  * - 'from' - email address of creator
  * - 'sender' - email address of sender (if different from creator)
  * - 'to' - email address of main recipient
  * - 'cc' - email address of carbon-copy receiver
  * - 'bcc' - email address of blind-carbon-copy receiver
  * - 'reply_to' - default email address for replies
  * - 'return_path' - email address to be used for bounce handling
  * - 'body_html' - HTML body part
  * - 'body_text' - plain text body part
  *
  * Only blocks, that exist in the template will be rendered and set.
  *
  * @param mixed $identifier usually the name of the template
  * @param array $variables array of placeholders for the twig template
  * @param array $options array of additional options for the renderer
  *
  * @return Message mail message for further customization
  */
 public function createMessageFromTemplate($identifier, array $variables = array(), array $options = array())
 {
     if (!isset($options['template_extension'])) {
         $options['template_extension'] = '.mail.twig';
         //$this->config->get('template_extension', '.mail.twig');
     }
     if (!isset($options['add_agavi_assigns'])) {
         $options['add_agavi_assigns'] = $this->config->get('add_agavi_assigns', true);
     }
     if (!$options['add_agavi_assigns']) {
         $twig_template = $this->loadTemplate($identifier, $options);
     } else {
         // add all assigns from the renderer parameters to the variables
         $layer = $this->getLayer($identifier, $options);
         $renderer = $layer->getRenderer();
         $context = AgaviContext::getInstance();
         $assigns = [];
         foreach ($renderer->getParameter('assigns', []) as $item => $var) {
             $getter = 'get' . StringToolkit::asStudlyCaps($item);
             if (is_callable([$context, $getter])) {
                 if (null === $var) {
                     continue;
                 }
                 $assigns[$var] = call_user_func([$context, $getter]);
             }
         }
         $variables = array_merge($variables, $assigns);
         $twig_template = $renderer->loadTemplate($layer);
     }
     $message = new Message();
     if ($twig_template->hasBlock('subject')) {
         $message->setSubject($twig_template->renderBlock('subject', $variables));
     }
     if ($twig_template->hasBlock('body_html')) {
         $message->setBodyHtml($twig_template->renderBlock('body_html', $variables));
     }
     if ($twig_template->hasBlock('body_text')) {
         $message->setBodyText($twig_template->renderBlock('body_text', $variables));
     }
     if ($twig_template->hasBlock('from')) {
         $message->setFrom($twig_template->renderBlock('from', $variables));
     }
     if ($twig_template->hasBlock('to')) {
         $message->setTo($twig_template->renderBlock('to', $variables));
     }
     if ($twig_template->hasBlock('cc')) {
         $message->setCc($twig_template->renderBlock('cc', $variables));
     }
     if ($twig_template->hasBlock('bcc')) {
         $message->setBcc($twig_template->renderBlock('bcc', $variables));
     }
     if ($twig_template->hasBlock('return_path')) {
         $message->setReturnPath($twig_template->renderBlock('return_path', $variables));
     }
     if ($twig_template->hasBlock('sender')) {
         $message->setSender($twig_template->renderBlock('sender', $variables));
     }
     if ($twig_template->hasBlock('reply_to')) {
         $message->setReplyTo($twig_template->renderBlock('reply_to', $variables));
     }
     return $message;
 }
 public function testSuccessfulMessageCreationWithCustomMailerConfig()
 {
     $config = [LoggingSwiftMailer::DEFAULT_MAILER_NAME => ['default_subject' => 'default subject'], LoggingSwiftMailer::DEFAULT_MAILERS_KEY => ['default' => ['default_subject' => 'default subject'], 'trololo' => ['default_subject' => 'trololo subject']]];
     $message = new Message();
     $message->setSender('*****@*****.**')->setTo('*****@*****.**');
     $mail_service = $this->getTestMailer($config);
     $mail_service->send($message, 'trololo');
     $swift_mail = $mail_service->getLastCreatedMail();
     $this->assertEquals('trololo subject', $swift_mail->getSubject(), 'Subject was not taken from custom mailer config');
 }