Skip to content

Commit b7b8a13

Browse files
committed
wip
1 parent fe9b3f5 commit b7b8a13

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
],
1818
"require": {
1919
"php": "^7.1",
20-
"illuminate/log": "5.7.*",
2120
"illuminate/container": "5.7.*",
2221
"illuminate/database": "^5.7",
22+
"illuminate/log": "5.7.*",
2323
"illuminate/routing": "5.7.*",
2424
"illuminate/support": "5.7.*",
2525
"zbateson/mail-mime-parser": "^1.1"

config/mailbox.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'path' => 'laravel-mailbox',
88

9+
'retention_in_days' => 1,
10+
911
'services' => [
1012

1113
'mailgun' => [

src/InboundEmail.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class InboundEmail extends Model
1515
{
16+
protected $table = 'mailbox_inbound_emails';
17+
1618
/** @var MimeMessage */
1719
protected $mimeMessage;
1820

@@ -23,10 +25,19 @@ class InboundEmail extends Model
2325
public static function fromMessage($message)
2426
{
2527
return new static([
26-
'message' => $message
28+
'message' => $message,
2729
]);
2830
}
2931

32+
protected static function boot()
33+
{
34+
parent::boot();
35+
36+
static::creating(function ($model) {
37+
$model->message_id = $model->id();
38+
});
39+
}
40+
3041
public function id(): string
3142
{
3243
return $this->message()->getHeaderValue('Message-Id', str_random());

src/MailboxRouter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,25 @@ protected function createRoute(string $subject, string $pattern, $action)
5353
public function callMailboxes(InboundEmail $email)
5454
{
5555
if ($email->isValid()) {
56+
57+
if ($this->shouldStoreInboundEmails()) {
58+
$this->storeEmail($email);
59+
}
60+
5661
$this->routes->match($email)->map(function (MailboxRoute $route) use ($email) {
5762
$route->run($email);
5863
});
5964
}
6065
}
6166

67+
protected function shouldStoreInboundEmails(): bool
68+
{
69+
return config('mailbox.retention_in_days') > 0;
70+
}
71+
72+
protected function storeEmail(InboundEmail $email)
73+
{
74+
$email->save();
75+
}
76+
6277
}

tests/InboundEmailTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Tests;
4+
5+
use BeyondCode\Mailbox\Facades\Mailbox;
6+
use BeyondCode\Mailbox\InboundEmail;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Support\Facades\Mail;
9+
10+
class InboundEmailTest extends TestCase
11+
{
12+
13+
protected function getEnvironmentSetUp($app)
14+
{
15+
parent::getEnvironmentSetUp($app);
16+
17+
$app['config']['mail.driver'] = 'log';
18+
$app['config']['mailbox.driver'] = 'log';
19+
}
20+
21+
/** @test */
22+
public function it_stores_inbound_emails()
23+
{
24+
Mailbox::from('[email protected]', function($email) {
25+
});
26+
27+
Mail::to('[email protected]')->send(new TestMail);
28+
Mail::to('[email protected]')->send(new TestMail);
29+
30+
$this->assertSame(2, InboundEmail::query()->count());
31+
}
32+
33+
}
34+
35+
class TestMail extends Mailable
36+
{
37+
public function build()
38+
{
39+
$this->from('[email protected]')
40+
->subject('This is a subject')
41+
->html('<html>Example email content</html>');
42+
}
43+
}

0 commit comments

Comments
 (0)