Mails for Filament

repository·main·Indexed 41 days ago

https://github.com/backstagephp/mails

A Laravel package for Filament applications to track, log, and manage sent emails. It provides a UI for viewing email history, engagement statistics, attachments, and lifecycle events (such as delivered, opened, and bounced), with the ability to resend emails and customize resources via configuration.

Tokens
3.5K
Snippets
13
Records
23
Agent score
51%

What's inside Mails

  1. Publish Mails configuration and views

    main

    To customize the package behavior or the UI, publish the configuration file and the views.

    # Publish configuration
    php artisan vendor:publish --tag="mails-config"
    
    # Optionally publish views
    php artisan vendor:publish --tag="mails-views"
  2. Register Mails in Filament PanelProvider

    main

    To use the Mails package within a Filament application, you must register the routes and the plugin in your PanelProvider.

    Use authenticatedRoutes() to register the mail management routes. This ensures the routes are protected by the panel's authentication.

    Then, add the MailsPlugin to the panel.

    use Backstage\Mails\Facades\Mails;
    use Backstage\Mails\MailsPlugin;
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->authenticatedRoutes(fn () => Mails::routes())
            ->plugin(MailsPlugin::make());
    }
  3. Install the Mails package

    main

    Install the package via Composer and run the necessary migrations to set up the database schema.

    composer require backstage/mails
    
    php artisan vendor:publish --tag="mails-migrations"
    php artisan migrate
  4. View email details and statistics

    main

    The MailResource provides a detailed view of each email, organized into several tabs:

    • Sender Information: Displays the subject, sender (from), recipients (to, cc, bcc), and reply_to addresses.
    • Statistics: Shows engagement metrics including opens, clicks, and timestamps for sent_at, delivered_at, last_opened_at, last_clicked_at, complained_at, soft_bounced_at, and hard_bounced_at.
    • Events: A chronological list of lifecycle events (e.g., DELIVERED, CLICKED, OPENED, SOFT_BOUNCED, HARD_BOUNCED, COMPLAINED, UNSUBSCRIBED, ACCEPTED) associated with the mail.
    • Content: Provides three ways to view the email body:
      • Preview: A rendered view of the HTML content.
      • HTML: The raw HTML source code (copyable).
      • Text: The plain text version (with line breaks preserved).
    • Attachments: Lists filenames, sizes, and MIME types, with a download link for each attachment.
  5. Customize Mails resources via configuration

    main

    You can override the default Filament resources used by the package (such as MailResource, EventResource, or SuppressionResource) by specifying your own classes in the mails config file. Your custom classes must extend the original resource classes.

    return [
        'resources' => [
            'mail' => \App\Filament\Resources\MailResource::class,
            'event' => \App\Filament\Resources\EventResource::class,
            'suppression' => \App\Filament\Resources\SuppressionResource::class
        ],
    ];
  6. Install the Mails package

    main

    The package includes an installation command that automates the initial setup. Running the install command will:

    1. Publish the configuration file.
    2. Publish the database migrations.
    3. Prompt you to run the migrations.
    4. Prompt you to star the repository on GitHub.

    Note: The package uses spatie/laravel-package-tools for its installation lifecycle.

    # The exact command is provided by the package's install command
    php artisan mails:install
  7. Register the Mails plugin in Filament

    main

    To integrate mail logging and management into your Filament admin panel, register the MailsPlugin within your panel configuration. The plugin provides access to Mail, Event, and Suppression resources.

    By default, the plugin is available to all users. You can restrict access to the mail management features using the canManageMails method.

    use Backstage\Mails\MailsPlugin;
    
    // In your Panel Provider
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugins([
                MailsPlugin::make(),
            ]);
    }
  8. Protect Mails routes with Tenant middleware

    main

    If your application uses multi-tenancy and you want to protect the mail routes with your tenant middleware, use the authenticatedTenantRoutes() method instead of authenticatedRoutes().

    use Backstage\Mails\MailsPlugin;
    use Backstage\Mails\Facades\Mails;
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugin(MailsPlugin::make())
            ->authenticatedTenantRoutes(fn () => Mails::routes());
    }
  9. Restrict access to Mails using canManageMails()

    main

    By default, you can control which users have access to the mail resources and pages by providing a callback to the canManageMails() method on the MailsPlugin.

    use Backstage\Mails\MailsPlugin;
    use Illuminate\\Support\\Facades\\Auth;
    
    $panel
        ->plugins([
            MailsPlugin::make()
                ->canManageMails(function () {
                    $user = Auth::user();
    
                    // Allow access for users with specific roles
                    if ($user->hasRole('admin') || $user->hasRole('supervisor')) {
                        return true;
                    }
    
                    // Allow access for users with specific permissions
                    if ($user->hasPermissionTo('manage mails')) {
                        return true;
                    }
    
                    return false;
                }),
        ]);
  10. Configure Mails plugin resources

    main

    The plugin uses Laravel configuration to determine which Filament resources to register. You can override the default resource classes by setting the following keys in your config/mails.php file:

    • mails.resources.mail: The class for the Mail resource (defaults to Backstage\Mails\Resources\MailResource).
    • mails.resources.event: The class for the Event resource (defaults to Backstage\Mails\Resources\EventResource).
    • mails.resources.suppression: The class for the Suppression resource (defaults to Backstage\Mails\Resources\SuppressionResource).
  11. Configure the Mail resource class

    main

    The ListMails page in the Filament admin panel uses a configurable class to represent the Mail resource. You can override the default MailResource by setting the mails.resources.mail configuration key in your Laravel application.

    // Example configuration in config/mails.php
    'resources' => [
        'mail' => App\Models\YourCustomMailResource::class,
    ],