18
loading...
This website collects cookies to deliver better user experience
composer required beyondcode/laravel-mailbox
php artisan vendor:publish --provider="BeyondCode\Mailbox\MailboxServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="BeyondCode\Mailbox\MailboxServiceProvider" --tag="config"
php artisan migrate
use BeyondCode\Mailbox\InboundEmail;
use BeyondCode\Mailbox\Facades\Mailbox;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// when send email someone to [email protected], InboundEmailHandler class will working
// InboundEmailHandler is a Invokable Class
Mailbox::to('[email protected]', InboundEmailHandler::class);
// You can use like this.
Mailbox::to('[email protected]' function (InboundEmail $email) {
// InboundEmail some available methods
$id = $email->id();
$date = $email->date();
$html = $email->html();
$text = $email->text();
$subject = $email->subject();
$from = $email->from();
// Handle incoming email
});
}
}
// AppServiceProvider.php
use BeyondCode\Mailbox\Facades\Mailbox;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// You can use userEmail as a variable in CreateTaskHandler invoke method.
Mailbox::from('{userEmail}@domain.com', CreateTaskHandler::class);
}
}
// app/InboundEmails/CreateTaskHandler.php
namespace App\InboundEmails;
use BeyondCode\Mailbox\InboundEmail;
class CreateTaskHandler {
public function __invoke(InboundEmail $email, $userEmail)
{
$user = User::where('inbound_email', $userEmail)->first();
if ($user) {
$task = $user->tasks()->create([
'title' => $email->subject(),
'description' => $email->text(),
]);
// $email->cc() return array and you can attach cc users to task users
$task->users()->attach($email->cc());
}
}
}