From 64768e2903c7b0cf3f0b1c0a57b4fba39e4dee23 Mon Sep 17 00:00:00 2001 From: Thomas Dutrion Date: Mon, 12 May 2014 11:09:31 +0100 Subject: [PATCH 1/2] Add multipart/alternative to HTML messages to support Office 365 --- src/GoalioMailService/Mail/Service/Message.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GoalioMailService/Mail/Service/Message.php b/src/GoalioMailService/Mail/Service/Message.php index 2f348b2..88f9213 100644 --- a/src/GoalioMailService/Mail/Service/Message.php +++ b/src/GoalioMailService/Mail/Service/Message.php @@ -73,7 +73,9 @@ public function createHtmlMessage($from, $to, $subject, $nameOrModel, $values = $body = new MimeMessage(); $body->setParts(array($text, $html)); - return $this->getDefaultMessage($from, 'utf-8', $to, $subject, $body); + $message = $this->getDefaultMessage($from, 'utf-8', $to, $subject, $body); + $message->getHeaders()->get('content-type')->setType(\Zend\Mime\Mime::MULTIPART_ALTERNATIVE); + return $message; } /** @@ -181,4 +183,4 @@ protected function getDefaultMessage($from, $encoding, $to, $subject, $body) { return $message; } -} \ No newline at end of file +} From b3c6aa9d1ac0465f1121cfeaa4f06c16df1c539c Mon Sep 17 00:00:00 2001 From: Thomas Dutrion Date: Mon, 12 May 2014 11:12:48 +0100 Subject: [PATCH 2/2] PSR2 syntax fix --- config/module.config.php | 2 +- .../Service/TransportOptionsFactory.php | 10 ++- .../Mail/Options/TransportOptions.php | 24 +++-- .../Mail/Service/Message.php | 88 +++++++++++-------- .../Transport/Service/TransportFactory.php | 14 +-- .../Mail/View/MailPhpRendererFactory.php | 8 +- src/GoalioMailService/Module.php | 15 ++-- .../Service/TransportOptionsFactoryTest.php | 12 +-- .../Mail/Options/TransportOptionsTest.php | 27 +++--- .../Mail/Service/MessageTest.php | 38 ++++---- .../Service/TransportFactoryTest.php | 15 ++-- .../Mail/View/MailPhpRendererFactoryTest.php | 16 ++-- .../Util/ServiceManagerFactory.php | 2 +- tests/testing.config.php | 2 +- 14 files changed, 158 insertions(+), 115 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index 2670143..5b61b0e 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,3 +1,3 @@ get('Config'); + return new TransportOptions(isset($config['goaliomailservice']) ? $config['goaliomailservice'] : array()); } -} \ No newline at end of file +} diff --git a/src/GoalioMailService/Mail/Options/TransportOptions.php b/src/GoalioMailService/Mail/Options/TransportOptions.php index 462395a..ebc379b 100644 --- a/src/GoalioMailService/Mail/Options/TransportOptions.php +++ b/src/GoalioMailService/Mail/Options/TransportOptions.php @@ -4,8 +4,8 @@ use Zend\Stdlib\AbstractOptions; -class TransportOptions extends AbstractOptions { - +class TransportOptions extends AbstractOptions +{ /** @var string */ protected $transportClass = 'Zend\Mail\Transport\File'; @@ -22,7 +22,8 @@ class TransportOptions extends AbstractOptions { * * @return $this */ - public function setTransportOptions($transportOptions) { + public function setTransportOptions($transportOptions) + { $this->transportOptions = $transportOptions; return $this; @@ -31,7 +32,8 @@ public function setTransportOptions($transportOptions) { /** * @return array */ - public function getTransportOptions() { + public function getTransportOptions() + { return $this->transportOptions; } @@ -40,7 +42,8 @@ public function getTransportOptions() { * * @return $this */ - public function setOptionsClass($optionsClass) { + public function setOptionsClass($optionsClass) + { $this->optionsClass = $optionsClass; return $this; @@ -49,7 +52,8 @@ public function setOptionsClass($optionsClass) { /** * @return string */ - public function getOptionsClass() { + public function getOptionsClass() + { return $this->optionsClass; } @@ -58,7 +62,8 @@ public function getOptionsClass() { * * @return $this */ - public function setTransportClass($transportClass) { + public function setTransportClass($transportClass) + { $this->transportClass = $transportClass; return $this; @@ -67,8 +72,9 @@ public function setTransportClass($transportClass) { /** * @return string */ - public function getTransportClass() { + public function getTransportClass() + { return $this->transportClass; } -} \ No newline at end of file +} diff --git a/src/GoalioMailService/Mail/Service/Message.php b/src/GoalioMailService/Mail/Service/Message.php index 88f9213..37d095c 100644 --- a/src/GoalioMailService/Mail/Service/Message.php +++ b/src/GoalioMailService/Mail/Service/Message.php @@ -7,8 +7,8 @@ use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; -class Message implements ServiceManagerAwareInterface { - +class Message implements ServiceManagerAwareInterface +{ /** * * @var ServiceManager @@ -17,11 +17,13 @@ class Message implements ServiceManagerAwareInterface { /** * - * @param ServiceManager $serviceManager + * @param ServiceManager $serviceManager * @return AbstractService */ - public function setServiceManager(ServiceManager $serviceManager) { + public function setServiceManager(ServiceManager $serviceManager) + { $this->serviceManager = $serviceManager; + return $this; } @@ -29,7 +31,8 @@ public function setServiceManager(ServiceManager $serviceManager) { * * @return ServiceManager */ - public function getServiceManager() { + public function getServiceManager() + { return $this->serviceManager; } @@ -48,19 +51,20 @@ public function getServiceManager() { /** * Return a HTML message ready to be sent * - * @param array|string $from - * A string containing the sender e-mail address, or if array with keys email and name - * @param array|string $to - * An array containing the recipients of the mail - * @param string $subject - * Subject of the mail - * @param string|\Zend\View\Model\ModelInterface $nameOrModel - * Either the template to use, or a ViewModel - * @param null|array $values - * Values to use when the template is rendered + * @param array|string $from + * A string containing the sender e-mail address, or if array with keys email and name + * @param array|string $to + * An array containing the recipients of the mail + * @param string $subject + * Subject of the mail + * @param string|\Zend\View\Model\ModelInterface $nameOrModel + * Either the template to use, or a ViewModel + * @param null|array $values + * Values to use when the template is rendered * @return Message */ - public function createHtmlMessage($from, $to, $subject, $nameOrModel, $values = array()) { + public function createHtmlMessage($from, $to, $subject, $nameOrModel, $values = array()) + { $renderer = $this->getRenderer(); $content = $renderer->render($nameOrModel, $values); @@ -74,26 +78,28 @@ public function createHtmlMessage($from, $to, $subject, $nameOrModel, $values = $body->setParts(array($text, $html)); $message = $this->getDefaultMessage($from, 'utf-8', $to, $subject, $body); - $message->getHeaders()->get('content-type')->setType(\Zend\Mime\Mime::MULTIPART_ALTERNATIVE); - return $message; + $message->getHeaders()->get('content-type')->setType(\Zend\Mime\Mime::MULTIPART_ALTERNATIVE); + + return $message; } /** * Return a text message ready to be sent * - * @param array|string $from - * A string containing the sender e-mail address, or if array with keys email and name - * @param array|string $to - * An array containing the recipients of the mail - * @param string $subject - * Subject of the mail - * @param string|\Zend\View\Model\ModelInterface $nameOrModel - * Either the template to use, or a ViewModel - * @param null|array $values - * Values to use when the template is rendered + * @param array|string $from + * A string containing the sender e-mail address, or if array with keys email and name + * @param array|string $to + * An array containing the recipients of the mail + * @param string $subject + * Subject of the mail + * @param string|\Zend\View\Model\ModelInterface $nameOrModel + * Either the template to use, or a ViewModel + * @param null|array $values + * Values to use when the template is rendered * @return Message */ - public function createTextMessage($from, $to, $subject, $nameOrModel, $values = array()) { + public function createTextMessage($from, $to, $subject, $nameOrModel, $values = array()) + { $renderer = $this->getRenderer(); $content = $renderer->render($nameOrModel, $values); @@ -105,7 +111,8 @@ public function createTextMessage($from, $to, $subject, $nameOrModel, $values = * * @param MailMessage $message */ - public function send(MailMessage $message) { + public function send(MailMessage $message) + { $this->getTransport() ->send($message); } @@ -115,8 +122,9 @@ public function send(MailMessage $message) { * * @return \Zend\View\Renderer\RendererInterface */ - public function getRenderer() { - if($this->renderer === null) { + public function getRenderer() + { + if ($this->renderer === null) { $serviceManager = $this->getServiceManager(); $this->renderer = $serviceManager->get('goaliomailservice_renderer'); } @@ -129,7 +137,8 @@ public function getRenderer() { * * @return $this */ - public function setRenderer($renderer) { + public function setRenderer($renderer) + { $this->renderer = $renderer; return $this; @@ -140,8 +149,9 @@ public function setRenderer($renderer) { * * @return \Zend\Mail\Transport\TransportInterface */ - public function getTransport() { - if($this->transport === null) { + public function getTransport() + { + if ($this->transport === null) { $this->transport = $this->getServiceManager() ->get('goaliomailservice_transport'); } @@ -154,7 +164,8 @@ public function getTransport() { * * @return $this */ - public function setTransport($transport) { + public function setTransport($transport) + { $this->transport = $transport; return $this; @@ -169,8 +180,9 @@ public function setTransport($transport) { * * @return MailMessage */ - protected function getDefaultMessage($from, $encoding, $to, $subject, $body) { - if(is_string($from)) { + protected function getDefaultMessage($from, $encoding, $to, $subject, $body) + { + if (is_string($from)) { $from = array('email' => $from, 'name' => $from); } diff --git a/src/GoalioMailService/Mail/Transport/Service/TransportFactory.php b/src/GoalioMailService/Mail/Transport/Service/TransportFactory.php index 2608e65..22f338f 100644 --- a/src/GoalioMailService/Mail/Transport/Service/TransportFactory.php +++ b/src/GoalioMailService/Mail/Transport/Service/TransportFactory.php @@ -6,14 +6,14 @@ use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\FactoryInterface; -class TransportFactory implements FactoryInterface { - - public function createService(ServiceLocatorInterface $serviceLocator) { - +class TransportFactory implements FactoryInterface +{ + public function createService(ServiceLocatorInterface $serviceLocator) + { /** @var TransportOptions $options */ $options = $serviceLocator->get('goaliomailservice_options'); - if(!class_exists($options->getTransportClass())) { + if (!class_exists($options->getTransportClass())) { throw new \Exception('Transport class has to be configured'); } @@ -22,7 +22,7 @@ public function createService(ServiceLocatorInterface $serviceLocator) { /** @var TransportInterface $transport */ $transport = new $transportClass; - if(class_exists($options->getOptionsClass())) { + if (class_exists($options->getOptionsClass())) { $transportOptionsClass = $options->getOptionsClass(); $transportOptions = new $transportOptionsClass; $transportOptions->setFromArray($options->getTransportOptions()); @@ -31,4 +31,4 @@ public function createService(ServiceLocatorInterface $serviceLocator) { return $transport; } -} \ No newline at end of file +} diff --git a/src/GoalioMailService/Mail/View/MailPhpRendererFactory.php b/src/GoalioMailService/Mail/View/MailPhpRendererFactory.php index 13663ad..862addd 100644 --- a/src/GoalioMailService/Mail/View/MailPhpRendererFactory.php +++ b/src/GoalioMailService/Mail/View/MailPhpRendererFactory.php @@ -6,8 +6,8 @@ use Zend\ServiceManager\ServiceLocatorInterface; use Zend\View\Renderer\PhpRenderer; -class MailPhpRendererFactory implements FactoryInterface { - +class MailPhpRendererFactory implements FactoryInterface +{ public function createService(ServiceLocatorInterface $serviceLocator) { $renderer = new PhpRenderer(); @@ -21,7 +21,7 @@ public function createService(ServiceLocatorInterface $serviceLocator) $application = $serviceLocator->get('Application'); $event = $application->getMvcEvent(); - if($event !== null) { + if ($event !== null) { $model = $serviceLocator->get('Application')->getMvcEvent()->getViewModel(); $modelHelper = $renderer->plugin('view_model'); @@ -30,4 +30,4 @@ public function createService(ServiceLocatorInterface $serviceLocator) return $renderer; } -} \ No newline at end of file +} diff --git a/src/GoalioMailService/Module.php b/src/GoalioMailService/Module.php index 67b1b08..99723ee 100644 --- a/src/GoalioMailService/Module.php +++ b/src/GoalioMailService/Module.php @@ -1,13 +1,13 @@ array( StandardAutoloader::LOAD_NS => array( @@ -17,11 +17,13 @@ public function getAutoloaderConfig() { ); } - public function getConfig() { + public function getConfig() + { return include __DIR__ . '/../../config/module.config.php'; } - public function getServiceConfig() { + public function getServiceConfig() + { return array( 'shared' => array( 'goaliomailservice_message' => false @@ -37,4 +39,3 @@ public function getServiceConfig() { ); } } - diff --git a/tests/GoalioMailServiceTest/Mail/Options/Service/TransportOptionsFactoryTest.php b/tests/GoalioMailServiceTest/Mail/Options/Service/TransportOptionsFactoryTest.php index 82ca3e7..ba3f378 100644 --- a/tests/GoalioMailServiceTest/Mail/Options/Service/TransportOptionsFactoryTest.php +++ b/tests/GoalioMailServiceTest/Mail/Options/Service/TransportOptionsFactoryTest.php @@ -5,20 +5,22 @@ use GoalioMailService\Mail\Options\Service\TransportOptionsFactory; use GoalioMailServiceTest\Util\ServiceManagerFactory; -class TransportOptionsFactoryTest extends \PHPUnit_Framework_TestCase { - - public function testCreateViaServiceManager() { +class TransportOptionsFactoryTest extends \PHPUnit_Framework_TestCase +{ + public function testCreateViaServiceManager() + { $sm = ServiceManagerFactory::getServiceManager(); $options = $sm->get('goaliomailservice_options'); $this->assertInstanceOf('GoalioMailService\Mail\Options\TransportOptions', $options); } - public function testCreateService() { + public function testCreateService() + { $sm = ServiceManagerFactory::getServiceManager(); $factory = new TransportOptionsFactory(); $options = $factory->createService($sm, 'goaliomailserviceoptions', 'GoalioMailServiceTest\Mail\Options\TransportOptions'); $this->assertInstanceOf('GoalioMailService\Mail\Options\TransportOptions', $options); } -} \ No newline at end of file +} diff --git a/tests/GoalioMailServiceTest/Mail/Options/TransportOptionsTest.php b/tests/GoalioMailServiceTest/Mail/Options/TransportOptionsTest.php index fe25963..c366c71 100644 --- a/tests/GoalioMailServiceTest/Mail/Options/TransportOptionsTest.php +++ b/tests/GoalioMailServiceTest/Mail/Options/TransportOptionsTest.php @@ -4,46 +4,53 @@ use GoalioMailService\Mail\Options\TransportOptions; -class TransportOptionsTest extends \PHPUnit_Framework_TestCase { - +class TransportOptionsTest extends \PHPUnit_Framework_TestCase +{ /** * @var TransportOptions $options */ protected $transportOptions; - public function setUp() { + public function setUp() + { $transportOptions = new TransportOptions(); $this->transportOptions = $transportOptions; } - public function testGetTransportClass() { + public function testGetTransportClass() + { $this->assertEquals('Zend\Mail\Transport\File', $this->transportOptions->getTransportClass()); } - public function testSetGetTransportClass() { + public function testSetGetTransportClass() + { $transportClass = 'NewTransportClass'; $this->transportOptions->setTransportClass($transportClass); $this->assertEquals($transportClass, $this->transportOptions->getTransportClass()); } - public function testGetOptionsClass() { + public function testGetOptionsClass() + { $this->assertEquals('Zend\Mail\Transport\FileOptions', $this->transportOptions->getOptionsClass()); } - public function testSetGetOptionsClass() { + public function testSetGetOptionsClass() + { $optionsClass = 'NewOptionsClass'; $this->transportOptions->setOptionsClass($optionsClass); $this->assertEquals($optionsClass, $this->transportOptions->getOptionsClass()); } - public function testGetTransportOptions() { + public function testGetTransportOptions() + { $this->assertEquals(array('path' => 'data/mail/'), $this->transportOptions->getTransportOptions()); } - public function testSetGetTransportOptions() { + public function testSetGetTransportOptions() + { $transportOptions = 'NewOptions'; $this->transportOptions->setTransportOptions($transportOptions); $this->assertEquals($transportOptions, $this->transportOptions->getTransportOptions()); } -} \ No newline at end of file +} diff --git a/tests/GoalioMailServiceTest/Mail/Service/MessageTest.php b/tests/GoalioMailServiceTest/Mail/Service/MessageTest.php index 64a546f..699fbc4 100644 --- a/tests/GoalioMailServiceTest/Mail/Service/MessageTest.php +++ b/tests/GoalioMailServiceTest/Mail/Service/MessageTest.php @@ -5,12 +5,13 @@ use GoalioMailService\Mail\Service\Message; use GoalioMailServiceTest\Util\ServiceManagerFactory; -class MessageTest extends \PHPUnit_Framework_TestCase { - +class MessageTest extends \PHPUnit_Framework_TestCase +{ /** @var Message */ protected $message; - public function setUp() { + public function setUp() + { $renderer = $this->getMock('Zend\View\Renderer\PhpRenderer', array('render')); $renderer->expects($this->any()) ->method('render') @@ -25,8 +26,8 @@ public function setUp() { $this->message->setTransport($transport); } - - public function testCreateHtmlMessage() { + public function testCreateHtmlMessage() + { $from = 'foo@test.dev'; $to = 'bar@test.dev'; $subject = 'Test'; @@ -39,8 +40,8 @@ public function testCreateHtmlMessage() { $this->assertInstanceOf('Zend\Mail\Message', $message); } - - public function testCreateTextMessage() { + public function testCreateTextMessage() + { $from = 'foo@test.dev'; $to = 'bar@test.dev'; $subject = 'Test'; @@ -53,7 +54,8 @@ public function testCreateTextMessage() { $this->assertInstanceOf('Zend\Mail\Message', $message); } - public function testSend() { + public function testSend() + { $transport = $this->getMock('Zend\Mail\Transport\File', array('send')); $transport->expects($this->once()) ->method('send'); @@ -64,33 +66,39 @@ public function testSend() { $this->message->send($message); } - public function testGetServiceManager() { + public function testGetServiceManager() + { $this->assertEquals(null, $this->message->getServiceManager()); } - public function testSetGetServiceManager() { + public function testSetGetServiceManager() + { $sm = ServiceManagerFactory::getServiceManager(); $this->message->setServiceManager($sm); $this->assertEquals($sm, $this->message->getServiceManager()); } - public function testGetRenderer() { + public function testGetRenderer() + { $this->assertInstanceOf('Zend\View\Renderer\PhpRenderer', $this->message->getRenderer()); } - public function testSetGetRenderer() { + public function testSetGetRenderer() + { $renderer = 'NewRenderer'; $this->message->setRenderer($renderer); $this->assertEquals($renderer, $this->message->getRenderer()); } - public function testGetTransport() { + public function testGetTransport() + { $this->assertInstanceOf('Zend\Mail\Transport\TransportInterface', $this->message->getTransport()); } - public function testSetGetTransport() { + public function testSetGetTransport() + { $transport = 'NewTransport'; $this->message->setTransport($transport); $this->assertEquals($transport, $this->message->getTransport()); } -} \ No newline at end of file +} diff --git a/tests/GoalioMailServiceTest/Mail/Transport/Service/TransportFactoryTest.php b/tests/GoalioMailServiceTest/Mail/Transport/Service/TransportFactoryTest.php index 4fa013d..81d5f9c 100644 --- a/tests/GoalioMailServiceTest/Mail/Transport/Service/TransportFactoryTest.php +++ b/tests/GoalioMailServiceTest/Mail/Transport/Service/TransportFactoryTest.php @@ -5,16 +5,18 @@ use GoalioMailService\Mail\Transport\Service\TransportFactory; use GoalioMailServiceTest\Util\ServiceManagerFactory; -class TransportFactoryTest extends \PHPUnit_Framework_TestCase { - - public function testCreateViaServiceManager() { +class TransportFactoryTest extends \PHPUnit_Framework_TestCase +{ + public function testCreateViaServiceManager() + { $sm = ServiceManagerFactory::getServiceManager(); $transport = $sm->get('goaliomailservice_transport'); $this->assertInstanceOf('Zend\Mail\Transport\File', $transport); } - public function testCreateService() { + public function testCreateService() + { $sm = ServiceManagerFactory::getServiceManager(); $factory = new TransportFactory(); $transport = $factory->createService($sm, 'goaliomailservicetransport'); @@ -25,7 +27,8 @@ public function testCreateService() { /** * @expectedException Exception */ - public function testServiceThrowsExceptionWhenTransportClassNotExists() { + public function testServiceThrowsExceptionWhenTransportClassNotExists() + { $sm = ServiceManagerFactory::getServiceManager(); $options = $sm->get('goaliomailservice_options'); @@ -33,4 +36,4 @@ public function testServiceThrowsExceptionWhenTransportClassNotExists() { $transport = $sm->get('goaliomailservice_transport'); } -} \ No newline at end of file +} diff --git a/tests/GoalioMailServiceTest/Mail/View/MailPhpRendererFactoryTest.php b/tests/GoalioMailServiceTest/Mail/View/MailPhpRendererFactoryTest.php index 7264034..71a6e66 100644 --- a/tests/GoalioMailServiceTest/Mail/View/MailPhpRendererFactoryTest.php +++ b/tests/GoalioMailServiceTest/Mail/View/MailPhpRendererFactoryTest.php @@ -5,26 +5,28 @@ use GoalioMailService\Mail\View\MailPhpRendererFactory; use GoalioMailServiceTest\Util\ServiceManagerFactory; -class MailPhpRendererFactoryTest extends \PHPUnit_Framework_TestCase { - +class MailPhpRendererFactoryTest extends \PHPUnit_Framework_TestCase +{ protected $sm; - public function setUp() { - + public function setUp() + { } - public function testCreateViaServiceManager() { + public function testCreateViaServiceManager() + { $sm = ServiceManagerFactory::getServiceManager(); $options = $sm->get('goaliomailservice_renderer'); $this->assertInstanceOf('Zend\View\Renderer\PhpRenderer', $options); } - public function testCreateService() { + public function testCreateService() + { $sm = ServiceManagerFactory::getServiceManager(); $factory = new MailPhpRendererFactory(); $options = $factory->createService($sm, 'goaliomailservicerenderer'); $this->assertInstanceOf('Zend\View\Renderer\PhpRenderer', $options); } -} \ No newline at end of file +} diff --git a/tests/GoalioMailServiceTest/Util/ServiceManagerFactory.php b/tests/GoalioMailServiceTest/Util/ServiceManagerFactory.php index 4d0f831..aee0f30 100644 --- a/tests/GoalioMailServiceTest/Util/ServiceManagerFactory.php +++ b/tests/GoalioMailServiceTest/Util/ServiceManagerFactory.php @@ -37,4 +37,4 @@ public static function getServiceManager() //$serviceManager->setAllowOverride(true); return $serviceManager; } -} \ No newline at end of file +} diff --git a/tests/testing.config.php b/tests/testing.config.php index 3a6e96d..93a73ba 100644 --- a/tests/testing.config.php +++ b/tests/testing.config.php @@ -13,4 +13,4 @@ 'goaliomailservice_renderer' => 'GoalioMailService\Mail\View\MailPhpRendererFactory', ), ), -); \ No newline at end of file +);