Skip to content

Commit 075a21c

Browse files
authored
Merge branch 'master' into mailcare-driver
2 parents 27299e9 + 8b7ac78 commit 075a21c

File tree

14 files changed

+168
-14
lines changed

14 files changed

+168
-14
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Mailbox::from('{username}@gmail.com', function (InboundEmail $email, $username)
1616
});
1717
```
1818

19+
[![https://phppackagedevelopment.com](https://beyondco.de/courses/phppd.jpg)](https://phppackagedevelopment.com)
20+
21+
If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming [PHP Package Development](https://phppackagedevelopment.com) video course.
22+
23+
1924
## Installation
2025

2126
You can install the package via composer:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"illuminate/log": "5.7.*",
2323
"illuminate/routing": "5.7.*",
2424
"illuminate/support": "5.7.*",
25+
"willdurand/email-reply-parser": "^2.8",
2526
"zbateson/mail-mime-parser": "^1.1"
2627
},
2728
"require-dev": {

config/mailbox.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
* The driver to use when listening for incoming emails.
77
* It defaults to the mail driver that you are using.
88
*
9-
* Supported drivers: "log", "mailgun", "sendgrid"
9+
* Supported drivers: "log", "mailgun", "sendgrid", "postmark"
1010
*/
1111
'driver' => env('MAILBOX_DRIVER', 'log'),
1212

13+
/*
14+
* The model class to use when converting an incoming email to a message.
15+
* It must extend the default model class
16+
*/
17+
'model' => \BeyondCode\Mailbox\InboundEmail::class,
18+
1319
/*
1420
* The path for driver specific routes. This is where
1521
* you need to point your driver specific callbacks

src/Console/CleanEmails.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ public function handle()
2020

2121
$cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
2222

23-
$amountDeleted = InboundEmail::where('created_at', '<', $cutOffDate)->delete();
23+
/** @var InboundEmail $modelClass */
24+
$modelClass = config('mailbox.model');
25+
26+
$models = $modelClass::where('created_at', '<', $cutOffDate)->get();
27+
28+
$models->each->delete();
29+
30+
$amountDeleted = $models->count();
2431

2532
$this->info("Deleted {$amountDeleted} record(s) from the Mailbox logs.");
2633

src/Drivers/Log.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55
use BeyondCode\Mailbox\InboundEmail;
66
use BeyondCode\Mailbox\Facades\Mailbox;
7-
use Illuminate\Log\Events\MessageLogged;
7+
use Illuminate\Mail\Events\MessageSent;
88

99
class Log implements DriverInterface
1010
{
1111
public function register()
1212
{
13-
app('events')->listen(MessageLogged::class, [$this, 'processLog']);
13+
app('events')->listen(MessageSent::class, [$this, 'processLog']);
1414
}
1515

16-
public function processLog(MessageLogged $log)
16+
public function processLog(MessageSent $event)
1717
{
18-
$email = InboundEmail::fromMessage($log->message);
18+
if (config('mail.driver') !== 'log') {
19+
return;
20+
}
21+
22+
/** @var InboundEmail $modelClass */
23+
$modelClass = config('mailbox.model');
24+
$email = $modelClass::fromMessage($event->message);
1925

2026
Mailbox::callMailboxes($email);
2127
}

src/Drivers/Postmark.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Drivers;
4+
5+
use Illuminate\Support\Facades\Route;
6+
use BeyondCode\Mailbox\Http\Controllers\PostmarkController;
7+
8+
class Postmark implements DriverInterface
9+
{
10+
public function register()
11+
{
12+
Route::prefix(config('mailbox.path'))->group(function () {
13+
Route::post('/postmark', PostmarkController::class);
14+
});
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Http\Controllers;
4+
5+
use Illuminate\Routing\Controller;
6+
use BeyondCode\Mailbox\Facades\Mailbox;
7+
use BeyondCode\Mailbox\Http\Requests\PostmarkRequest;
8+
9+
class PostmarkController extends Controller
10+
{
11+
public function __construct()
12+
{
13+
$this->middleware('laravel-mailbox');
14+
}
15+
16+
public function __invoke(PostmarkRequest $request)
17+
{
18+
Mailbox::callMailboxes($request->email());
19+
}
20+
}

src/Http/Requests/MailgunRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public function validator()
2727

2828
public function email()
2929
{
30-
return InboundEmail::fromMessage($this->get('body-mime'));
30+
/** @var InboundEmail $modelClass */
31+
$modelClass = config('mailbox.model');
32+
33+
return $modelClass::fromMessage($this->get('body-mime'));
3134
}
3235

3336
protected function verifySignature()

src/Http/Requests/PostmarkRequest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Http\Requests;
4+
5+
use BeyondCode\Mailbox\InboundEmail;
6+
use Illuminate\Support\Facades\Validator;
7+
use Illuminate\Foundation\Http\FormRequest;
8+
9+
class PostmarkRequest extends FormRequest
10+
{
11+
public function validator()
12+
{
13+
return Validator::make($this->all(), [
14+
'RawEmail' => 'required',
15+
]);
16+
}
17+
18+
public function email()
19+
{
20+
/** @var InboundEmail $modelClass */
21+
$modelClass = config('mailbox.model');
22+
23+
return $modelClass::fromMessage($this->get('RawEmail'));
24+
}
25+
}

src/Http/Requests/SendGridRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function validator()
1717

1818
public function email()
1919
{
20-
return InboundEmail::fromMessage($this->get('email'));
20+
/** @var InboundEmail $modelClass */
21+
$modelClass = config('mailbox.model');
22+
23+
return $modelClass::fromMessage($this->get('email'));
2124
}
2225
}

0 commit comments

Comments
 (0)