PHP code example of innoge / laravel-msgraph-mailbox

1. Go to this page and download the library: Download innoge/laravel-msgraph-mailbox library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innoge / laravel-msgraph-mailbox example snippets


use InnoGE\LaravelMsGraphMailbox\Events\MessageReceived;

Event::listen(function (MessageReceived $event) {
    $message = $event->message();

    Ticket::createFromEmail($message->from()->address, $message->subject(), $message->body());

    $message->archive();
});

'mailboxes' => ['[email protected]'],

use InnoGE\LaravelMsGraphMailbox\Enums\WellKnownFolder;
use InnoGE\LaravelMsGraphMailbox\Events\MessageReceived;
use InnoGE\LaravelMsGraphMailbox\Events\MessageRemoved;

'mailboxes' => [
    '[email protected]' => [
        'folders' => [WellKnownFolder::Inbox, 'Support/Escalations'],
        'events' => [MessageReceived::class, MessageRemoved::class],
    ],
],

use InnoGE\LaravelMsGraphMailbox\Facades\Mailbox;

Mailbox::observe(fn () => Team::pluck('support_mailbox'));

use Illuminate\Support\Facades\Schedule;

Schedule::command('msgraph-mailbox:subscribe')->hourly()->onOneServer()->withoutOverlapping();
Schedule::command('msgraph-mailbox:sync')->everyFifteenMinutes()->onOneServer();

'webhooks' => [
    'register_route' => false,
    // ...
],

use InnoGE\LaravelMsGraphMailbox\Facades\Mailbox;

Route::domain('hooks.example.com')->group(function () {
    Mailbox::routes();
});

public function handle(MessageMoved $event): void
{
    if ($event->movedTo(WellKnownFolder::Archive)) {
        // ...
    }
}

public function handle(MessageReceived $event): void
{
    $subject = $event->message()->subject();
}

use InnoGE\LaravelMsGraphMailbox\Facades\Mailbox;

$message = Mailbox::for('[email protected]')->message($id);

$message->subject();
$message->from();               // EmailAddress with ->address and ->name
$message->to();
$message->cc();
$message->receivedAt();         // CarbonImmutable
$message->internetMessageId();
$message->header('Auto-Submitted');
$message->isRead();
$message->flagStatus();         // FlagStatus enum
$message->categories();

use InnoGE\LaravelMsGraphMailbox\Enums\BodyType;

$message->body();
$message->body(BodyType::Html);
$message->body(unique: true);

if ($message->isAutoSubmitted() || $message->isFromSelf()) {
    return;
}

foreach ($message->attachments() as $attachment) {
    Storage::put("invoices/{$attachment->safeFilename()}", $attachment->stream());
}

$message->markRead()->categorize('Customer')->archive();

use InnoGE\LaravelMsGraphMailbox\Enums\FlagStatus;
use InnoGE\LaravelMsGraphMailbox\Enums\WellKnownFolder;

$message->archive();
$message->move('Support/Done');
$message->move('Support/Done', createMissing: true);
$message->copy(WellKnownFolder::Inbox);     // Returns the copy
$message->markRead();
$message->markUnread();
$message->flag(FlagStatus::Complete);
$message->unflag();
$message->categorize('Customer', 'Urgent'); // Adds categories
$message->setCategories(['Customer']);      // Replaces all categories
$message->delete();                         // Moves the message to Deleted Items
$message->permanentDelete();

use InnoGE\LaravelMsGraphMailbox\Jobs\MoveMessage;

MoveMessage::dispatch('[email protected]', $id, WellKnownFolder::Archive);

use InnoGE\LaravelMsGraphMailbox\Jobs\Middleware\ThrottleGraphRequests;

public function middleware(MessageReceived $event): array
{
    return [new ThrottleGraphRequests($event->mailbox)];
}

'mailboxes' => ['[email protected]'],

namespace App\Listeners;

use App\Models\Ticket;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Str;
use InnoGE\LaravelMsGraphMailbox\Events\MessageReceived;
use InnoGE\LaravelMsGraphMailbox\Jobs\Middleware\ThrottleGraphRequests;

class AttachReplyToTicket implements ShouldQueue
{
    public function handle(MessageReceived $event): void
    {
        $message = $event->message();

        if ($message->isAutoSubmitted() || $message->isFromSelf()) {
            return;
        }

        $ticket = Ticket::where('number', Str::match('/\[(TICKET-\d+)\]/', (string) $message->subject()))->first();

        if (! $ticket) {
            return;
        }

        $reply = $ticket->replies()->firstOrCreate(
            ['internet_message_id' => $message->internetMessageId()],
            ['from' => $message->from()?->address, 'body' => $message->body(unique: true)],
        );

        foreach ($message->attachments() as $attachment) {
            $reply->addMediaFromStream($attachment->stream())->usingFileName($attachment->safeFilename())->toMediaCollection();
        }

        $message->archive();
    }

    public function middleware(MessageReceived $event): array
    {
        return [new ThrottleGraphRequests($event->mailbox)];
    }
}

use InnoGE\LaravelMsGraphMailbox\Enums\WellKnownFolder;
use InnoGE\LaravelMsGraphMailbox\Facades\Mailbox;

it('attaches replies to their ticket', function () {
    $ticket = Ticket::factory()->create(['number' => 'TICKET-1042']);
    $mailbox = Mailbox::fake();

    $message = $mailbox->receive('[email protected]', [
        'subject' => "Re: [TICKET-1042] My order hasn't arrived",
        'from' => ['emailAddress' => ['address' => '[email protected]']],
        'uniqueBody' => ['content' => 'It arrived today, thanks!'],
    ]);

    expect($ticket->replies()->sole()->body)->toBe('It arrived today, thanks!');
    $mailbox->assertMoved($message, WellKnownFolder::Archive);
});

$mailbox = Mailbox::fake();

$message = $mailbox->receive('[email protected]', [
    'subject' => 'Invoice 2026-114',
    'body' => ['content' => 'Please find the invoice attached.'],
    'attachments' => [['id' => 'a1', 'name' => 'invoice.pdf', 'contentBytes' => base64_encode($pdf)]],
]);

$mailbox->assertMoved($message, WellKnownFolder::Archive);
$mailbox->assertCopied($message, 'Accounting');
$mailbox->assertMarkedRead($message->id);
$mailbox->assertMarkedUnread($message->id);
$mailbox->assertFlagged($message->id, FlagStatus::Complete);
$mailbox->assertCategorized($message, ['Invoice']);
$mailbox->assertDeleted($message->id);
$mailbox->assertPermanentlyDeleted($message->id);
$mailbox->assertNothingPerformed();
shell
php artisan msgraph-mailbox:subscribe
shell
php artisan msgraph-mailbox:status
ini
MSGRAPH_MAILBOX_SCHEDULE=false
ini
MSGRAPH_MAILBOX_WEBHOOKS=false