/**
  * 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;
 }
 /**
  * @param mixed $value email string or array with emails to check
  * @param string $setting_name
  * @param string $mailer_name
  * @param AgaviXmlConfigDomDocument $document
  * @param string $fieldname fieldname like 'to' etc. if it is a nested setting like 'address_overrides'
  *
  * @return void
  *
  * @throws ConfigError if given value is not a valid email or array of valid emails
  */
 protected function validateEmailValue($value, $setting_name, $mailer_name, $document, $fieldname = null)
 {
     if (is_array($value)) {
         foreach ($value as $email) {
             if (!is_string($email) || empty($email) || !Message::isValidEmail($email)) {
                 throw new ConfigError(sprintf('Configuration file "%s" specifies an invalid email address for ' . 'the "%s" setting %s on mailer "%s": %s', $document->documentURI, $setting_name, empty($fieldname) ? '' : "of the {$fieldname} field ", $mailer_name, $email));
             }
         }
     } else {
         if (empty($value) || !Message::isValidEmail($value)) {
             throw new ConfigError(sprintf('Configuration file "%s" specifies an invalid email address for ' . 'the "%s" setting %s on mailer "%s": %s', $document->documentURI, $setting_name, empty($fieldname) ? '' : "of the {$fieldname} field ", $mailer_name, $value));
         }
     }
 }
 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');
 }
Example #4
0
 public function testSettingToValuesWorks()
 {
     $message = new Message();
     $message->setTo(['*****@*****.**' => 'to someone']);
     $this->assertEquals(['*****@*****.**' => 'to someone'], $message->getTo('*****@*****.**'), "Simple email address with display name does not work for setTo().");
     $message->setTo('*****@*****.**');
     $this->assertEquals(['*****@*****.**' => null], $message->getTo('*****@*****.**'), "Simple email address as string does not work for setTo().");
     $message->setTo(['*****@*****.**']);
     $this->assertEquals(['*****@*****.**' => null], $message->getTo(['*****@*****.**']), "Simple array with one default email address should work with getTo().");
     $message->setTo(['*****@*****.**' => 'to someone', '*****@*****.**']);
     $this->assertEquals(['*****@*****.**' => 'to someone', '*****@*****.**' => null], $message->getTo(['*****@*****.**', '*****@*****.**']), "Setting multiple addresses should have worked for getTo().");
 }