diff --git a/src/Loggable/Notifications/EmailChannel/EmailChannel.php b/src/Loggable/Notifications/EmailChannel/EmailChannel.php index 0cb981f..f4ad6b3 100644 --- a/src/Loggable/Notifications/EmailChannel/EmailChannel.php +++ b/src/Loggable/Notifications/EmailChannel/EmailChannel.php @@ -3,7 +3,6 @@ namespace Illuminated\Console\Loggable\Notifications\EmailChannel; use Monolog\Handler\DeduplicationHandler; -use Monolog\Handler\NativeMailerHandler; use Monolog\Handler\SwiftMailerHandler; use Monolog\Logger; use Swift_Mailer; @@ -37,35 +36,18 @@ protected function getEmailChannelHandler() $from = $this->getEmailNotificationsFrom(); $level = $this->getEmailNotificationsLevel(); - $driver = config('mail.driver'); - switch ($driver) { - case 'null': - return false; - - case 'mail': - case 'smtp': - case 'sendmail': - /** @var Swift_Mailer $mailer */ - $mailer = app('mailer')->getSwiftMailer(); - - /** @var Swift_Message $message */ - $message = $mailer->createMessage(); - $message->setSubject($subject); - $message->setFrom(to_swiftmailer_emails($from)); - $message->setTo(to_swiftmailer_emails($recipients)); - $message->setContentType('text/html'); - $message->setCharset('utf-8'); - - $mailerHandler = new SwiftMailerHandler($mailer, $message, $level); - break; - - default: - $to = to_rfc2822_email($recipients); - $from = to_rfc2822_email($from); - $mailerHandler = new NativeMailerHandler($to, $subject, $from, $level); - $mailerHandler->setContentType('text/html'); - break; - } + /** @var Swift_Mailer $mailer */ + $mailer = app('mailer')->getSwiftMailer(); + + /** @var Swift_Message $message */ + $message = $mailer->createMessage(); + $message->setSubject($subject); + $message->setFrom(to_swiftmailer_emails($from)); + $message->setTo(to_swiftmailer_emails($recipients)); + $message->setContentType('text/html'); + $message->setCharset('utf-8'); + + $mailerHandler = new SwiftMailerHandler($mailer, $message, $level); $mailerHandler->setFormatter(new MonologHtmlFormatter); if ($this->useEmailNotificationsDeduplication()) { diff --git a/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php b/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php index 3575211..d50ca37 100644 --- a/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php +++ b/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php @@ -8,7 +8,6 @@ use Illuminated\Console\Tests\App\Console\Commands\EmailNotificationsInvalidRecipientsCommand; use Illuminated\Console\Tests\TestCase; use Monolog\Handler\DeduplicationHandler; -use Monolog\Handler\NativeMailerHandler; use Monolog\Handler\SwiftMailerHandler; use Monolog\Logger; use Swift_Message; @@ -23,17 +22,6 @@ public function it_validates_and_filters_notification_recipients() $this->assertNotInstanceOf(SwiftMailerHandler::class, $command->emailChannelHandler()); } - /** @test */ - public function it_is_disabled_on_null_driver() - { - config(['mail.driver' => 'null']); - - /** @var EmailNotificationsCommand $command */ - $command = $this->runArtisan(new EmailNotificationsCommand); - - $this->assertFalse($command->createEmailChannelHandler()); - } - /** @test */ public function it_uses_configured_monolog_swift_mailer_handler_on_mail_driver() { @@ -71,21 +59,10 @@ public function it_uses_configured_monolog_swift_mailer_handler_on_sendmail_driv $this->assertMailerHandlersEqual($this->composeSwiftMailerHandler(), $command->emailChannelHandler()); } - /** @test */ - public function it_uses_configured_monolog_native_mailer_handler_on_other_drivers() - { - config(['mail.driver' => 'any-other']); - - /** @var EmailNotificationsCommand $command */ - $command = $this->runArtisan(new EmailNotificationsCommand); - - $this->assertMailerHandlersEqual($this->composeNativeMailerHandler(), $command->emailChannelHandler()); - } - /** @test */ public function it_uses_configured_monolog_deduplication_handler_if_deduplication_enabled() { - config(['mail.driver' => 'any-other']); + config(['mail.driver' => 'sendmail']); /** @var EmailNotificationsDeduplicationCommand $command */ $command = $this->runArtisan(new EmailNotificationsDeduplicationCommand); @@ -98,43 +75,18 @@ public function it_uses_configured_monolog_deduplication_handler_if_deduplicatio /** * Compose "swift mailer" handler. * + * @param string $name * @return \Monolog\Handler\SwiftMailerHandler */ - private function composeSwiftMailerHandler() + private function composeSwiftMailerHandler($name = 'email-notifications-command') { - $handler = new SwiftMailerHandler(app('mailer')->getSwiftMailer(), $this->composeMailerHandlerMessage(), Logger::NOTICE); + $handler = new SwiftMailerHandler(app('mailer')->getSwiftMailer(), $this->composeMailerHandlerMessage($name), Logger::NOTICE); $handler->setFormatter(new MonologHtmlFormatter); return $handler; } - /** - * Compose "native mailer" handler. - * - * @param string $name - * @return \Monolog\Handler\NativeMailerHandler - */ - private function composeNativeMailerHandler(string $name = 'email-notifications-command') - { - $handler = new NativeMailerHandler( - to_rfc2822_email([ - ['address' => 'john.doe@example.com', 'name' => 'John Doe'], - ['address' => 'jane.smith@example.com', 'name' => 'Jane Smith'], - ]), - "[TESTING] %level_name% in `{$name}` command", - to_rfc2822_email([ - 'address' => 'no-reply@example.com', - 'name' => 'ICLogger Notification', - ]), - Logger::NOTICE - ); - $handler->setContentType('text/html'); - $handler->setFormatter(new MonologHtmlFormatter); - - return $handler; - } - /** * Compose "deduplication" handler. * @@ -143,20 +95,21 @@ private function composeNativeMailerHandler(string $name = 'email-notifications- private function composeDeduplicationHandler() { return new DeduplicationHandler( - $this->composeNativeMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60 + $this->composeSwiftMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60 ); } /** * Compose mailer handler message. * + * @param string $name * @return \Swift_Message */ - private function composeMailerHandlerMessage() + private function composeMailerHandlerMessage($name = 'email-notifications-command') { /** @var Swift_Message $message */ $message = app('mailer')->getSwiftMailer()->createMessage(); - $message->setSubject('[TESTING] %level_name% in `email-notifications-command` command'); + $message->setSubject("[TESTING] %level_name% in `{$name}` command"); $message->setFrom(to_swiftmailer_emails([ 'address' => 'no-reply@example.com', 'name' => 'ICLogger Notification',