Skip to content

Commit 324150f

Browse files
committed
fix bugs
1 parent 2ef129a commit 324150f

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/SendGridChannel.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public function send($notifiable, Notification $notification)
3939
throw new Exception('toSendGrid must return an instance of SendGridMessage.');
4040
}
4141

42-
try {
43-
$this->sendGrid->send($message->build());
44-
} catch (Exception $e) {
45-
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
42+
$response = $this->sendGrid->send($message->build());
43+
44+
if ($response->statusCode() !== 200) {
45+
throw CouldNotSendNotification::serviceRespondedWithAnError(
46+
$response->body()
47+
);
4648
}
4749
}
4850
}

src/SendGridMessage.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public function payload($payload)
8888
*/
8989
public function build(): Mail
9090
{
91+
if (! $this->from) {
92+
// If from is not defined, use the values set in Laravel config
93+
$this->from(
94+
config('mail.from.address'),
95+
config('mail.from.name')
96+
);
97+
}
98+
9199
$email = new Mail(
92100
$this->from,
93101
$this->tos

tests/SendGridChannelTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use NotificationChannels\SendGrid\SendGridMessage;
1010
use PHPUnit\Framework\TestCase;
1111
use SendGrid;
12+
use SendGrid\Response;
1213

1314
class SendGridChannelTest extends TestCase
1415
{
@@ -26,6 +27,9 @@ public function testEmailIsSentViaSendGrid()
2627
$sendgrid = Mockery::mock(new SendGrid('x'))
2728
);
2829

30+
$response = Mockery::mock(Response::class);
31+
$response->shouldReceive('statusCode')->once()->andReturn(200);
32+
2933
$message = $notification->toSendGrid($notifiable);
3034

3135
$this->assertEquals($message->templateId, 'sendgrid-template-id');
@@ -37,7 +41,7 @@ public function testEmailIsSentViaSendGrid()
3741
$this->assertEquals($message->payload['baz'], 'foo2');
3842

3943
// TODO: Verify that the Mail instance passed contains all the info from above
40-
$sendgrid->shouldReceive('send')->once();
44+
$sendgrid->shouldReceive('send')->once()->andReturn($response);
4145

4246
$channel->send($notifiable, $notification);
4347
}

0 commit comments

Comments
 (0)