Skip to content

Commit 41edf1c

Browse files
committed
Always use SwiftMailerHandler
1 parent 633fa71 commit 41edf1c

File tree

2 files changed

+25
-103
lines changed

2 files changed

+25
-103
lines changed

src/Loggable/Notifications/EmailChannel/EmailChannel.php

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Illuminated\Console\Loggable\Notifications\EmailChannel;
44

5-
use Monolog\Logger;
6-
use Monolog\Handler\MandrillHandler;
7-
use Monolog\Handler\SwiftMailerHandler;
8-
use Monolog\Handler\NativeMailerHandler;
95
use Monolog\Handler\DeduplicationHandler;
6+
use Monolog\Handler\SwiftMailerHandler;
7+
use Monolog\Logger;
8+
use Swift_Mailer;
9+
use Swift_Message;
1010

1111
trait EmailChannel
1212
{
@@ -26,37 +26,18 @@ protected function getEmailChannelHandler()
2626
$from = $this->getEmailNotificationsFrom();
2727
$level = $this->getEmailNotificationsLevel();
2828

29-
$driver = config('mail.driver');
30-
switch ($driver) {
31-
case 'null':
32-
return false;
33-
34-
case 'mail':
35-
case 'smtp':
36-
case 'sendmail':
37-
case 'mandrill':
38-
$mailer = app('swift.mailer');
39-
$message = $mailer->createMessage();
40-
$message->setSubject($subject);
41-
$message->setFrom(to_swiftmailer_emails($from));
42-
$message->setTo(to_swiftmailer_emails($recipients));
43-
$message->setContentType('text/html');
44-
$message->setCharset('utf-8');
45-
46-
if ($driver == 'mandrill') {
47-
$mailerHandler = new MandrillHandler(config('services.mandrill.secret'), $message, $level);
48-
} else {
49-
$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
50-
}
51-
break;
52-
53-
default:
54-
$to = to_rfc2822_email($recipients);
55-
$from = to_rfc2822_email($from);
56-
$mailerHandler = new NativeMailerHandler($to, $subject, $from, $level);
57-
$mailerHandler->setContentType('text/html');
58-
break;
59-
}
29+
/** @var Swift_Mailer $mailer */
30+
$mailer = app('mailer')->getSwiftMailer();
31+
32+
/** @var Swift_Message $message */
33+
$message = $mailer->createMessage();
34+
$message->setSubject($subject);
35+
$message->setFrom(to_swiftmailer_emails($from));
36+
$message->setTo(to_swiftmailer_emails($recipients));
37+
$message->setContentType('text/html');
38+
$message->setCharset('utf-8');
39+
40+
$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
6041
$mailerHandler->setFormatter(new MonologHtmlFormatter);
6142

6243
if ($this->useEmailNotificationsDeduplication()) {

tests/ConsoleLogger/Loggable/Notifications/EmailChannel/EmailChannelTest.php

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
namespace Illuminated\Console\ConsoleLogger\Tests\Loggable\Notifications\EmailChannel;
44

5-
use Monolog\Logger;
65
use EmailNotificationsCommand;
7-
use Monolog\Handler\MandrillHandler;
8-
use Monolog\Handler\SwiftMailerHandler;
9-
use Monolog\Handler\NativeMailerHandler;
10-
use Monolog\Handler\DeduplicationHandler;
116
use EmailNotificationsDeduplicationCommand;
127
use EmailNotificationsInvalidRecipientsCommand;
138
use Illuminated\Console\ConsoleLogger\Tests\TestCase;
149
use Illuminated\Console\Loggable\Notifications\EmailChannel\MonologHtmlFormatter;
10+
use Monolog\Handler\DeduplicationHandler;
11+
use Monolog\Handler\SwiftMailerHandler;
12+
use Monolog\Logger;
1513

1614
class EmailChannelTest extends TestCase
1715
{
@@ -22,16 +20,6 @@ public function it_validates_and_filters_notification_recipients()
2220
$this->assertNotInstanceOf(SwiftMailerHandler::class, $handler);
2321
}
2422

25-
/** @test */
26-
public function it_is_disabled_on_null_driver()
27-
{
28-
config(['mail.driver' => 'null']);
29-
30-
$handler = $this->runArtisan(new EmailNotificationsCommand)->createEmailChannelHandler();
31-
32-
$this->assertFalse($handler);
33-
}
34-
3523
/** @test */
3624
public function it_uses_configured_monolog_swift_mailer_handler_on_mail_driver()
3725
{
@@ -59,81 +47,34 @@ public function it_uses_configured_monolog_swift_mailer_handler_on_sendmail_driv
5947
$this->assertMailerHandlersEqual($this->composeSwiftMailerHandler(), $handler);
6048
}
6149

62-
/** @test */
63-
public function it_uses_configured_monolog_mandrill_mailer_handler_on_mandrill_driver()
64-
{
65-
config(['mail.driver' => 'mandrill', 'services.mandrill.secret' => 'secret']);
66-
$handler = $this->runArtisan(new EmailNotificationsCommand)->emailChannelHandler();
67-
68-
$this->assertMailerHandlersEqual($this->composeMandrillMailerHandler(), $handler);
69-
}
70-
71-
/** @test */
72-
public function it_uses_configured_monolog_native_mailer_handler_on_other_drivers()
73-
{
74-
config(['mail.driver' => 'any-other']);
75-
$handler = $this->runArtisan(new EmailNotificationsCommand)->emailChannelHandler();
76-
77-
$this->assertMailerHandlersEqual($this->composeNativeMailerHandler(), $handler);
78-
}
79-
8050
/** @test */
8151
public function it_uses_configured_monolog_deduplication_handler_if_deduplication_enabled()
8252
{
83-
config(['mail.driver' => 'any-other']);
53+
config(['mail.driver' => 'sendmail']);
8454
$handler = $this->runArtisan(new EmailNotificationsDeduplicationCommand)->emailChannelHandler();
8555
$handler->flush();
8656

8757
$this->assertMailerHandlersEqual($this->composeDeduplicationHandler(), $handler);
8858
}
8959

90-
private function composeSwiftMailerHandler()
60+
private function composeSwiftMailerHandler($name = 'email-notifications-command')
9161
{
92-
$handler = new SwiftMailerHandler(app('swift.mailer'), $this->composeMailerHandlerMessage(), Logger::NOTICE);
62+
$handler = new SwiftMailerHandler(app('swift.mailer'), $this->composeMailerHandlerMessage($name), Logger::NOTICE);
9363
$handler->setFormatter(new MonologHtmlFormatter);
9464
return $handler;
9565
}
9666

97-
private function composeMandrillMailerHandler()
98-
{
99-
$handler = new MandrillHandler(
100-
config('services.mandrill.secret'), $this->composeMailerHandlerMessage(), Logger::NOTICE
101-
);
102-
$handler->setFormatter(new MonologHtmlFormatter);
103-
return $handler;
104-
}
105-
106-
private function composeNativeMailerHandler($name = 'email-notifications-command')
107-
{
108-
$handler = new NativeMailerHandler(
109-
to_rfc2822_email([
110-
['address' => '[email protected]', 'name' => 'John Doe'],
111-
['address' => '[email protected]', 'name' => 'Jane Smith'],
112-
]),
113-
"[TESTING] %level_name% in `{$name}` command",
114-
to_rfc2822_email([
115-
'address' => '[email protected]',
116-
'name' => 'ICLogger Notification',
117-
]),
118-
Logger::NOTICE
119-
);
120-
$handler->setContentType('text/html');
121-
$handler->setFormatter(new MonologHtmlFormatter);
122-
123-
return $handler;
124-
}
125-
12667
private function composeDeduplicationHandler()
12768
{
12869
return new DeduplicationHandler(
129-
$this->composeNativeMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
70+
$this->composeSwiftMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
13071
);
13172
}
13273

133-
private function composeMailerHandlerMessage()
74+
private function composeMailerHandlerMessage($name = 'email-notifications-command')
13475
{
13576
$message = app('swift.mailer')->createMessage();
136-
$message->setSubject('[TESTING] %level_name% in `email-notifications-command` command');
77+
$message->setSubject("[TESTING] %level_name% in `{$name}` command");
13778
$message->setFrom(to_swiftmailer_emails([
13879
'address' => '[email protected]',
13980
'name' => 'ICLogger Notification',

0 commit comments

Comments
 (0)