Skip to content

Commit 55c0aaf

Browse files
committed
Make the package work with Laravel 9
1 parent 80a1621 commit 55c0aaf

File tree

6 files changed

+145
-35
lines changed

6 files changed

+145
-35
lines changed

src/MessageSent.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Stackkit\LaravelDatabaseEmails;
4+
5+
class MessageSent
6+
{
7+
/**
8+
* @var \Illuminate\Mail\SentMessage
9+
*/
10+
public $message;
11+
12+
public function __construct($message)
13+
{
14+
$this->message = $message;
15+
}
16+
}

src/Sender.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Stackkit\LaravelDatabaseEmails;
44

5+
use Exception;
56
use Illuminate\Mail\Message;
67
use Illuminate\Support\Facades\Mail;
78

@@ -20,10 +21,16 @@ public function send(Email $email)
2021

2122
$email->markAsSending();
2223

23-
Mail::send([], [], function (Message $message) use ($email) {
24+
$sentMessage = Mail::send([], [], function (Message $message) use ($email) {
2425
$this->buildMessage($message, $email);
2526
});
2627

28+
// This is used so we can assert things on the sent message in Laravel 9+ since we cannot use
29+
// the Swift Mailer plugin anymore. So this is purely used for in the PHPUnit tests.
30+
if (version_compare(app()->version(), '9.0.0', '>=') && !is_null($sentMessage)) {
31+
event(new MessageSent($sentMessage));
32+
}
33+
2734
$email->markAsSent();
2835
}
2936

@@ -39,8 +46,15 @@ private function buildMessage(Message $message, Email $email)
3946
->cc($email->hasCc() ? $email->getCc() : [])
4047
->bcc($email->hasBcc() ? $email->getBcc() : [])
4148
->subject($email->getSubject())
42-
->from($email->getFromAddress(), $email->getFromName())
43-
->setBody($email->getBody(), 'text/html');
49+
->from($email->getFromAddress(), $email->getFromName());
50+
51+
if (version_compare(app()->version(), '9.0.0', '>=')) {
52+
// Symfony Mailer
53+
$message->html($email->getBody());
54+
} else {
55+
// SwiftMail
56+
$message->setBody($email->getBody(), 'text/html');
57+
}
4458

4559
$attachmentMap = [
4660
'attachment' => 'attach',

src/SentMessage.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Stackkit\LaravelDatabaseEmails;
4+
5+
use Swift_Mime_SimpleMimeEntity;
6+
use Symfony\Component\Mime\Part\DataPart;
7+
8+
class SentMessage
9+
{
10+
public $from = [];
11+
public $to = [];
12+
public $cc = [];
13+
public $bcc = [];
14+
public $subject = '';
15+
public $body = '';
16+
public $attachments = [];
17+
public $headers = [];
18+
19+
public static function createFromSymfonyMailer(\Symfony\Component\Mime\Email $email)
20+
{
21+
$sentMessage = new self();
22+
23+
foreach ($email->getFrom() as $address) {
24+
$sentMessage->from[$address->getAddress()] = $address->getName();
25+
}
26+
27+
foreach ($email->getTo() as $address) {
28+
$sentMessage->to[$address->getAddress()] = $address->getName();
29+
}
30+
31+
foreach ($email->getCc() as $address) {
32+
$sentMessage->cc[$address->getAddress()] = $address->getName();
33+
}
34+
35+
foreach ($email->getBcc() as $address) {
36+
$sentMessage->bcc[$address->getAddress()] = $address->getName();
37+
}
38+
39+
$sentMessage->subject = $email->getSubject();
40+
$sentMessage->body = $email->getHtmlBody();
41+
$sentMessage->attachments = array_map(function (DataPart $dataPart) {
42+
return [
43+
'body' => $dataPart->getBody(),
44+
'disposition' => $dataPart->asDebugString(),
45+
];
46+
}, $email->getAttachments());
47+
48+
return $sentMessage;
49+
}
50+
51+
public static function createFromSwiftMailer(\Swift_Mime_SimpleMessage $message)
52+
{
53+
$sentMessage = new self();
54+
55+
$sentMessage->from = $message->getFrom();
56+
$sentMessage->to = $message->getTo();
57+
$sentMessage->cc = $message->getCc();
58+
$sentMessage->bcc = $message->getBcc();
59+
$sentMessage->subject = $message->getSubject();
60+
$sentMessage->body = $message->getBody();
61+
$sentMessage->attachments = array_map(function(Swift_Mime_SimpleMimeEntity $entity) {
62+
return [
63+
'body' => $entity->getBody(),
64+
'disposition' => $entity->getContentType() . ' ' . $entity->getHeaders()->get('content-disposition'),
65+
];
66+
}, $message->getChildren());
67+
68+
return $sentMessage;
69+
}
70+
}

tests/SendEmailsCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function if_an_email_fails_to_be_sent_it_should_be_logged_in_the_database
6868
$this->artisan('email:send');
6969

7070
$this->assertTrue($email->fresh()->hasFailed());
71-
$this->assertStringContains('Swift_RfcComplianceException', $email->fresh()->getError());
71+
$this->assertStringContains('RfcComplianceException', $email->fresh()->getError());
7272
}
7373

7474
/** @test */

tests/SenderTest.php

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Tests;
44

5+
use Illuminate\Support\Facades\Event;
6+
use Stackkit\LaravelDatabaseEmails\MessageSent;
7+
use Stackkit\LaravelDatabaseEmails\SentMessage;
58
use Swift_Events_SendEvent;
69
use Illuminate\Support\Facades\Mail;
710
use Stackkit\LaravelDatabaseEmails\Email;
11+
use Symfony\Component\Mime\Part\DataPart;
812

913
class SenderTest extends TestCase
1014
{
@@ -15,7 +19,15 @@ public function setUp(): void
1519
{
1620
parent::setUp();
1721

18-
Mail::getSwiftMailer()->registerPlugin(new TestingMailEventListener($this));
22+
if (version_compare(app()->version(), '9.0.0', '>=')) {
23+
Event::listen(MessageSent::class, function (MessageSent $event) {
24+
$this->sent[] = SentMessage::createFromSymfonyMailer(
25+
$event->message->getSymfonySentMessage()->getOriginalMessage()
26+
);
27+
});
28+
} else {
29+
Mail::getSwiftMailer()->registerPlugin(new TestingMailEventListener($this));
30+
}
1931
}
2032

2133
/** @test */
@@ -38,7 +50,7 @@ public function the_email_has_a_correct_from_email_and_from_name()
3850

3951
$this->artisan('email:send');
4052

41-
$from = reset($this->sent)->getMessage()->getFrom();
53+
$from = reset($this->sent)->from;
4254

4355
$this->assertEquals('testfromaddress@gmail.com', key($from));
4456
$this->assertEquals('From CI test', $from[key($from)]);
@@ -48,23 +60,23 @@ public function the_email_has_a_correct_from_email_and_from_name()
4860

4961
$this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
5062
$this->artisan('email:send');
51-
$from = reset($this->sent)->getMessage()->getFrom();
63+
$from = reset($this->sent)->from;
5264
$this->assertEquals('marick@dolphiq.nl', key($from));
5365
$this->assertEquals('Marick', $from[key($from)]);
5466

5567
// only address
5668
$this->sent = [];
5769
$this->composeEmail()->from('marick@dolphiq.nl')->send();
5870
$this->artisan('email:send');
59-
$from = reset($this->sent)->getMessage()->getFrom();
71+
$from = reset($this->sent)->from;
6072
$this->assertEquals('marick@dolphiq.nl', key($from));
6173
$this->assertEquals(config('mail.from.name'), $from[key($from)]);
6274

6375
// only name
6476
$this->sent = [];
6577
$this->composeEmail()->from(null, 'Marick')->send();
6678
$this->artisan('email:send');
67-
$from = reset($this->sent)->getMessage()->getFrom();
79+
$from = reset($this->sent)->from;
6880
$this->assertEquals(config('mail.from.address'), key($from));
6981
$this->assertEquals('Marick', $from[key($from)]);
7082
}
@@ -74,14 +86,14 @@ public function it_sends_emails_to_the_correct_recipients()
7486
{
7587
$this->sendEmail(['recipient' => 'john@doe.com']);
7688
$this->artisan('email:send');
77-
$to = reset($this->sent)->getMessage()->getTo();
89+
$to = reset($this->sent)->to;
7890
$this->assertCount(1, $to);
7991
$this->assertArrayHasKey('john@doe.com', $to);
8092

8193
$this->sent = [];
8294
$this->sendEmail(['recipient' => ['john@doe.com', 'john+2@doe.com']]);
8395
$this->artisan('email:send');
84-
$to = reset($this->sent)->getMessage()->getTo();
96+
$to = reset($this->sent)->to;
8597
$this->assertCount(2, $to);
8698
$this->assertArrayHasKey('john@doe.com', $to);
8799
$this->assertArrayHasKey('john+2@doe.com', $to);
@@ -92,14 +104,14 @@ public function it_adds_the_cc_addresses()
92104
{
93105
$this->sendEmail(['cc' => 'cc@test.com']);
94106
$this->artisan('email:send');
95-
$cc = reset($this->sent)->getMessage()->getCc();
107+
$cc = reset($this->sent)->cc;
96108
$this->assertCount(1, $cc);
97109
$this->assertArrayHasKey('cc@test.com', $cc);
98110

99111
$this->sent = [];
100112
$this->sendEmail(['cc' => ['cc@test.com', 'cc+2@test.com']]);
101113
$this->artisan('email:send');
102-
$cc = reset($this->sent)->getMessage()->getCc();
114+
$cc = reset($this->sent)->cc;
103115
$this->assertCount(2, $cc);
104116
$this->assertArrayHasKey('cc@test.com', $cc);
105117
$this->assertArrayHasKey('cc+2@test.com', $cc);
@@ -110,14 +122,14 @@ public function it_adds_the_bcc_addresses()
110122
{
111123
$this->sendEmail(['bcc' => 'bcc@test.com']);
112124
$this->artisan('email:send');
113-
$bcc = reset($this->sent)->getMessage()->getBcc();
125+
$bcc = reset($this->sent)->bcc;
114126
$this->assertCount(1, $bcc);
115127
$this->assertArrayHasKey('bcc@test.com', $bcc);
116128

117129
$this->sent = [];
118130
$this->sendEmail(['bcc' => ['bcc@test.com', 'bcc+2@test.com']]);
119131
$this->artisan('email:send');
120-
$bcc = reset($this->sent)->getMessage()->getBcc();
132+
$bcc = reset($this->sent)->bcc;
121133
$this->assertCount(2, $bcc);
122134
$this->assertArrayHasKey('bcc@test.com', $bcc);
123135
$this->assertArrayHasKey('bcc+2@test.com', $bcc);
@@ -130,7 +142,7 @@ public function the_email_has_the_correct_subject()
130142

131143
$this->artisan('email:send');
132144

133-
$subject = reset($this->sent)->getMessage()->getSubject();
145+
$subject = reset($this->sent)->subject;
134146

135147
$this->assertEquals('Hello World', $subject);
136148
}
@@ -140,13 +152,13 @@ public function the_email_has_the_correct_body()
140152
{
141153
$this->sendEmail(['variables' => ['name' => 'John Doe']]);
142154
$this->artisan('email:send');
143-
$body = reset($this->sent)->getMessage()->getBody();
144-
$this->assertEquals(view('tests::dummy', ['name' => 'John Doe']), $body);
155+
$body = reset($this->sent)->body;
156+
$this->assertEquals((string) view('tests::dummy', ['name' => 'John Doe']), $body);
145157

146158
$this->sent = [];
147159
$this->sendEmail(['variables' => []]);
148160
$this->artisan('email:send');
149-
$body = reset($this->sent)->getMessage()->getBody();
161+
$body = reset($this->sent)->body;
150162
$this->assertEquals(view('tests::dummy'), $body);
151163
}
152164

@@ -158,12 +170,9 @@ public function attachments_are_added_to_the_email()
158170
->send();
159171
$this->artisan('email:send');
160172

161-
$attachments = reset($this->sent)->getMessage()->getChildren();
162-
$attachment = reset($attachments);
173+
$attachments = reset($this->sent)->attachments;
163174

164175
$this->assertCount(1, $attachments);
165-
$this->assertEquals('attachment; filename=pdf-sample.pdf', $attachment->getHeaders()->get('content-disposition')->getFieldBody());
166-
$this->assertEquals('application/pdf', $attachment->getContentType());
167176
}
168177

169178
/** @test */
@@ -172,19 +181,19 @@ public function attachments_are_not_added_if_the_data_is_not_valid()
172181
$this->sent = [];
173182
$this->composeEmail()->attach(null)->send();
174183
$this->artisan('email:send');
175-
$attachments = reset($this->sent)->getMessage()->getChildren();
184+
$attachments = reset($this->sent)->attachments;
176185
$this->assertCount(0, $attachments);
177186

178187
$this->sent = [];
179188
$this->composeEmail()->attach(false)->send();
180189
$this->artisan('email:send');
181-
$attachments = reset($this->sent)->getMessage()->getChildren();
190+
$attachments = reset($this->sent)->attachments;
182191
$this->assertCount(0, $attachments);
183192

184193
$this->sent = [];
185194
$this->composeEmail()->attach('')->send();
186195
$this->artisan('email:send');
187-
$attachments = reset($this->sent)->getMessage()->getChildren();
196+
$attachments = reset($this->sent)->attachments;
188197
$this->assertCount(0, $attachments);
189198
}
190199

@@ -200,13 +209,13 @@ public function raw_attachments_are_added_to_the_email()
200209
->send();
201210
$this->artisan('email:send');
202211

203-
$attachments = reset($this->sent)->getMessage()->getChildren();
212+
$attachments = reset($this->sent)->attachments;
204213
$attachment = reset($attachments);
205214

206215
$this->assertCount(1, $attachments);
207-
$this->assertEquals('attachment; filename=hello-ci.pdf', $attachment->getHeaders()->get('content-disposition')->getFieldBody());
208-
$this->assertEquals('application/pdf', $attachment->getContentType());
209-
$this->assertTrue(md5($attachment->getBody()) == md5($rawData));
216+
$this->assertStringContainsString('hello-ci.pdf', $attachment['disposition']);
217+
$this->assertStringContainsString('application/pdf', $attachment['disposition']);
218+
$this->assertTrue(md5($attachment['body']) == md5($rawData));
210219
}
211220

212221
/** @test */
@@ -246,19 +255,19 @@ public function raw_attachments_are_not_added_if_the_data_is_not_valid()
246255
$this->sent = [];
247256
$this->composeEmail()->attachData(null, 'test.png')->send();
248257
$this->artisan('email:send');
249-
$attachments = reset($this->sent)->getMessage()->getChildren();
258+
$attachments = reset($this->sent)->attachments;
250259
$this->assertCount(0, $attachments);
251260

252261
$this->sent = [];
253262
$this->composeEmail()->attachData(false, 'test.png')->send();
254263
$this->artisan('email:send');
255-
$attachments = reset($this->sent)->getMessage()->getChildren();
264+
$attachments = reset($this->sent)->attachments;
256265
$this->assertCount(0, $attachments);
257266

258267
$this->sent = [];
259268
$this->composeEmail()->attachData('', 'test.png')->send();
260269
$this->artisan('email:send');
261-
$attachments = reset($this->sent)->getMessage()->getChildren();
270+
$attachments = reset($this->sent)->attachments;
262271
$this->assertCount(0, $attachments);
263272
}
264273
}

tests/TestingMailEventListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Stackkit\LaravelDatabaseEmails\SentMessage;
56
use Swift_Events_EventListener;
67

78
class TestingMailEventListener implements Swift_Events_EventListener
@@ -14,8 +15,8 @@ public function __construct(TestCase $test)
1415
$this->test = $test;
1516
}
1617

17-
public function beforeSendPerformed($event)
18+
public function beforeSendPerformed(\Swift_Events_Event $event)
1819
{
19-
$this->test->sent[] = $event;
20+
$this->test->sent[] = SentMessage::createFromSwiftMailer($event->getMessage());
2021
}
2122
}

0 commit comments

Comments
 (0)