Skip to content

Commit adf8daf

Browse files
committed
Always use SwiftMailerHandler
1 parent e881b40 commit adf8daf

File tree

2 files changed

+20
-82
lines changed

2 files changed

+20
-82
lines changed

src/Loggable/Notifications/EmailChannel/EmailChannel.php

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Illuminated\Console\Loggable\Notifications\EmailChannel;
44

55
use Monolog\Handler\DeduplicationHandler;
6-
use Monolog\Handler\NativeMailerHandler;
76
use Monolog\Handler\SwiftMailerHandler;
87
use Monolog\Logger;
98
use Swift_Mailer;
@@ -37,35 +36,18 @@ protected function getEmailChannelHandler()
3736
$from = $this->getEmailNotificationsFrom();
3837
$level = $this->getEmailNotificationsLevel();
3938

40-
$driver = config('mail.driver');
41-
switch ($driver) {
42-
case 'null':
43-
return false;
44-
45-
case 'mail':
46-
case 'smtp':
47-
case 'sendmail':
48-
/** @var Swift_Mailer $mailer */
49-
$mailer = app('swift.mailer');
50-
51-
/** @var Swift_Message $message */
52-
$message = $mailer->createMessage();
53-
$message->setSubject($subject);
54-
$message->setFrom(to_swiftmailer_emails($from));
55-
$message->setTo(to_swiftmailer_emails($recipients));
56-
$message->setContentType('text/html');
57-
$message->setCharset('utf-8');
58-
59-
$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
60-
break;
61-
62-
default:
63-
$to = to_rfc2822_email($recipients);
64-
$from = to_rfc2822_email($from);
65-
$mailerHandler = new NativeMailerHandler($to, $subject, $from, $level);
66-
$mailerHandler->setContentType('text/html');
67-
break;
68-
}
39+
/** @var Swift_Mailer $mailer */
40+
$mailer = app('mailer')->getSwiftMailer();
41+
42+
/** @var Swift_Message $message */
43+
$message = $mailer->createMessage();
44+
$message->setSubject($subject);
45+
$message->setFrom(to_swiftmailer_emails($from));
46+
$message->setTo(to_swiftmailer_emails($recipients));
47+
$message->setContentType('text/html');
48+
$message->setCharset('utf-8');
49+
50+
$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
6951
$mailerHandler->setFormatter(new MonologHtmlFormatter);
7052

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

tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ public function it_validates_and_filters_notification_recipients()
2222
$this->assertNotInstanceOf(SwiftMailerHandler::class, $handler);
2323
}
2424

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-
3525
/** @test */
3626
public function it_uses_configured_monolog_swift_mailer_handler_on_mail_driver()
3727
{
@@ -62,20 +52,10 @@ public function it_uses_configured_monolog_swift_mailer_handler_on_sendmail_driv
6252
$this->assertMailerHandlersEqual($this->composeSwiftMailerHandler(), $handler);
6353
}
6454

65-
/** @test */
66-
public function it_uses_configured_monolog_native_mailer_handler_on_other_drivers()
67-
{
68-
config(['mail.driver' => 'any-other']);
69-
70-
$handler = $this->runArtisan(new EmailNotificationsCommand)->emailChannelHandler();
71-
72-
$this->assertMailerHandlersEqual($this->composeNativeMailerHandler(), $handler);
73-
}
74-
7555
/** @test */
7656
public function it_uses_configured_monolog_deduplication_handler_if_deduplication_enabled()
7757
{
78-
config(['mail.driver' => 'any-other']);
58+
config(['mail.driver' => 'sendmail']);
7959

8060
/** @var \Monolog\Handler\DeduplicationHandler $handler */
8161
$handler = $this->runArtisan(new EmailNotificationsDeduplicationCommand)->emailChannelHandler();
@@ -87,43 +67,18 @@ public function it_uses_configured_monolog_deduplication_handler_if_deduplicatio
8767
/**
8868
* Compose "swift mailer" handler.
8969
*
70+
* @param string $name
9071
* @return \Monolog\Handler\SwiftMailerHandler
9172
*/
92-
private function composeSwiftMailerHandler()
73+
private function composeSwiftMailerHandler($name = 'email-notifications-command')
9374
{
94-
$handler = new SwiftMailerHandler(app('swift.mailer'), $this->composeMailerHandlerMessage(), Logger::NOTICE);
75+
$handler = new SwiftMailerHandler(app('swift.mailer'), $this->composeMailerHandlerMessage($name), Logger::NOTICE);
9576

9677
$handler->setFormatter(new MonologHtmlFormatter);
9778

9879
return $handler;
9980
}
10081

101-
/**
102-
* Compose "native mailer" handler.
103-
*
104-
* @param string $name
105-
* @return \Monolog\Handler\NativeMailerHandler
106-
*/
107-
private function composeNativeMailerHandler(string $name = 'email-notifications-command')
108-
{
109-
$handler = new NativeMailerHandler(
110-
to_rfc2822_email([
111-
['address' => 'john.doe@example.com', 'name' => 'John Doe'],
112-
['address' => 'jane.smith@example.com', 'name' => 'Jane Smith'],
113-
]),
114-
"[TESTING] %level_name% in `{$name}` command",
115-
to_rfc2822_email([
116-
'address' => 'no-reply@example.com',
117-
'name' => 'ICLogger Notification',
118-
]),
119-
Logger::NOTICE
120-
);
121-
$handler->setContentType('text/html');
122-
$handler->setFormatter(new MonologHtmlFormatter);
123-
124-
return $handler;
125-
}
126-
12782
/**
12883
* Compose "deduplication" handler.
12984
*
@@ -132,20 +87,21 @@ private function composeNativeMailerHandler(string $name = 'email-notifications-
13287
private function composeDeduplicationHandler()
13388
{
13489
return new DeduplicationHandler(
135-
$this->composeNativeMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
90+
$this->composeSwiftMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
13691
);
13792
}
13893

13994
/**
14095
* Compose mailer handler message.
14196
*
97+
* @param string $name
14298
* @return \Swift_Message
14399
*/
144-
private function composeMailerHandlerMessage()
100+
private function composeMailerHandlerMessage($name = 'email-notifications-command')
145101
{
146102
/** @var Swift_Message $message */
147103
$message = app('swift.mailer')->createMessage();
148-
$message->setSubject('[TESTING] %level_name% in `email-notifications-command` command');
104+
$message->setSubject("[TESTING] %level_name% in `{$name}` command");
149105
$message->setFrom(to_swiftmailer_emails([
150106
'address' => 'no-reply@example.com',
151107
'name' => 'ICLogger Notification',

0 commit comments

Comments
 (0)