Author: Alejandro Celaya Alastrué
Inheritance: implements AcMailer\Service\MailServiceInterface, implements Zend\EventManager\EventManagerAwareInterface, implements AcMailer\Event\MailListenerAwareInterface
Ejemplo n.º 1
0
 public function testWithDefaultLayout()
 {
     $resolver = new TemplatePathStack();
     $resolver->addPath(__DIR__ . '/../../view');
     $this->mailService->getRenderer()->setResolver($resolver);
     $model = new ViewModel();
     $model->setTemplate('ac-mailer/mail-templates/layout.phtml');
     $this->mailService->setDefaultLayout(new DefaultLayout($model));
     $this->mailService->setTemplate('ac-mailer/mail-templates/mail.phtml');
     $this->assertInstanceOf('Zend\\Mime\\Message', $this->mailService->getMessage()->getBody());
 }
Ejemplo n.º 2
0
 public function testStringBypassedBodyIsWrappedIntoMimePartWithAttachments()
 {
     $cwd = getcwd();
     chdir(dirname(__DIR__));
     $this->mailService->setAttachments(array('attachments/file1', 'attachments/file2'));
     $this->mailService->getMessage()->setBody('Btpassed body as string');
     $result = $this->mailService->send();
     $this->assertTrue($result->isValid());
     /* @var Mime\Message $body */
     $body = $this->mailService->getMessage()->getBody();
     $this->assertInstanceOf('Zend\\Mime\\Message', $body);
     chdir($cwd);
 }
 /**
  * Create service with name
  *
  * @param ServiceLocatorInterface $sm
  * @param $name
  * @param $requestedName
  * @return mixed
  */
 public function createServiceWithName(ServiceLocatorInterface $sm, $name, $requestedName)
 {
     $specificServiceName = explode('.', $name)[2];
     $this->mailOptions = $sm->get(sprintf('%s.%s.%s', self::ACMAILER_PART, MailOptionsAbstractFactory::SPECIFIC_PART, $specificServiceName));
     // Create the service
     $message = $this->createMessage();
     $transport = $this->createTransport($sm);
     $renderer = $this->createRenderer($sm);
     $mailService = new MailService($message, $transport, $renderer);
     // Set subject
     $mailService->setSubject($this->mailOptions->getMessageOptions()->getSubject());
     // Set body, either by using a template or a raw body
     $body = $this->mailOptions->getMessageOptions()->getBody();
     if ($body->getUseTemplate()) {
         $defaultLayoutConfig = $body->getTemplate()->getDefaultLayout();
         if (isset($defaultLayoutConfig['path'])) {
             $params = isset($defaultLayoutConfig['params']) ? $defaultLayoutConfig['params'] : [];
             $captureTo = isset($defaultLayoutConfig['template_capture_to']) ? $defaultLayoutConfig['template_capture_to'] : 'content';
             $mailService->setDefaultLayout(new DefaultLayout($defaultLayoutConfig['path'], $params, $captureTo));
         }
         $mailService->setTemplate($body->getTemplate()->toViewModel(), ['charset' => $body->getCharset()]);
     } else {
         $mailService->setBody($body->getContent(), $body->getCharset());
     }
     // Attach files
     $files = $this->mailOptions->getMessageOptions()->getAttachments()->getFiles();
     $mailService->addAttachments($files);
     // Attach files from dir
     $dir = $this->mailOptions->getMessageOptions()->getAttachments()->getDir();
     if ($dir['iterate'] === true && is_string($dir['path']) && is_dir($dir['path'])) {
         $files = $dir['recursive'] === true ? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir['path'], \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) : new \DirectoryIterator($dir['path']);
         /* @var \SplFileInfo $fileInfo */
         foreach ($files as $fileInfo) {
             if ($fileInfo->isDir()) {
                 continue;
             }
             $mailService->addAttachment($fileInfo->getPathname());
         }
     }
     // Attach mail listeners
     $this->attachMailListeners($mailService, $sm);
     return $mailService;
 }
 public function createService(ServiceLocatorInterface $sm)
 {
     /* @var MailOptions $mailOptions */
     $mailOptions = $sm->get('AcMailer\\Options\\MailOptions');
     // Prepare Mail Message
     $message = new Message();
     $message->setFrom($mailOptions->getFrom(), $mailOptions->getFromName())->setTo($mailOptions->getTo())->setCc($mailOptions->getCc())->setBcc($mailOptions->getBcc());
     // Prepare Mail Transport
     $serviceName = $mailOptions->getMailAdapterService();
     /** @var TransportInterface $transport */
     $transport = isset($serviceName) ? $sm->get($serviceName) : $mailOptions->getMailAdapter();
     $this->setupSpecificConfig($transport, $mailOptions);
     // Prepare MailService
     $renderer = $this->createRenderer($sm);
     $mailService = new MailService($message, $transport, $renderer);
     $mailService->setSubject($mailOptions->getSubject());
     // Set body, either by using a template or the body option
     $template = $mailOptions->getTemplate();
     if ($template->getUseTemplate() === true) {
         $mailService->setTemplate($template->toViewModel());
     } else {
         $mailService->setBody($mailOptions->getBody());
     }
     // Attach files
     $files = $mailOptions->getAttachments()->getFiles();
     $mailService->addAttachments($files);
     // Attach files from dir
     $dir = $mailOptions->getAttachments()->getDir();
     if ($dir['iterate'] === true && is_string($dir['path']) && is_dir($dir['path'])) {
         $files = $dir['recursive'] === true ? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir['path'], \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) : new \DirectoryIterator($dir['path']);
         /* @var \SplFileInfo $fileInfo */
         foreach ($files as $fileInfo) {
             if ($fileInfo->isDir()) {
                 continue;
             }
             $mailService->addAttachment($fileInfo->getPathname());
         }
     }
     return $mailService;
 }