New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

pod-point/laravel-mail-export

A mailable trait to export mails to a storage disk once being sent
56,161 7
Install
composer require pod-point/laravel-mail-export
Latest Version:2.2.0
PHP:^8.1
Laravel:9 · 10 · 11
Older releases:5 – 8
License:MIT
Last Updated:Nov 7, 2024
Links: GitHub  ·  Packagist
Maintainer: pod-point

Laravel Mail Export

Latest Version on Packagist tests Software License Total Downloads

This package can export any mail sent with Laravel's Mailable class to any desired filesystem disk and path as a .eml file.

This can be useful when wanting to store emails sent for archive purposes.

Installation

You can install the package via composer:

For Laravel 9.x, 10.x, 11.x (requires PHP version 8.2 or higher)

composer require pod-point/laravel-mail-export

For Laravel 7.x and 8.x

composer require pod-point/laravel-mail-export:^1.0

For Laravel 5.x and 6.x

composer require pod-point/laravel-mail-export:^0.1

Publishing the config file

The configuration for this package comes with some sensible values but you can optionally publish the config file with:

php artisan vendor:publish --provider="PodPoint\MailExport\MailExportServiceProvider"

You will be able to specify:

  • enabled: whether this package is enabled or not. Once installed, it's enabled by default but the MAIL_EXPORT environment variable can be used to configure this.
  • disk: which disk to use by default. null will use the default disk from your application filesystem.
  • path: the default path, within the configured disk, where mail will be exported.

See our config/mail-export.php for more details.

Usage

Simply add the Exportable trait and the ShouldExport interface to any Mailable class that you want to persist into any storage disk.

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use PodPoint\MailExport\Concerns\Exportable;
use PodPoint\MailExport\Contracts\ShouldExport;

class OrderShipped extends Mailable implements ShouldExport
{
    use Exportable;

    // ...
}

This will use the default filesystem disk and path from the configuration and will also generate a unique filename for you.

The default filename is using a timestamp, the mail recipients, the subject and will look like so:

2021_03_26_150142_jane_at_example_com_this_is_the_subject.eml

You can also specify the disk, path or filename to use for a specific Mailable using properties:

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use PodPoint\MailExport\Concerns\Exportable;
use PodPoint\MailExport\Contracts\ShouldExport;

class OrderShipped extends Mailable implements ShouldExport
{
    use Exportable;

    public $exportDisk = 'some_disk';

    public $exportPath = 'some_path';

    public $exportFilename = 'some_filename';

    // ...
}

You can also use methods if you need more flexibility:

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use PodPoint\MailExport\Concerns\Exportable;
use PodPoint\MailExport\Contracts\ShouldExport;

class OrderShipped extends Mailable implements ShouldExport
{
    use Exportable;

    // ...

    public function exportDisk(): string
    {
        return 'some_disk';
    }

    public function exportPath(): string
    {
        return 'some_path';
    }

    public function exportFilename(): string
    {
        return 'some_filename';
    }
}

Then you can keep using your Mailable as usual:

Mail::to($request->user())->send(new OrderShipped($order));

Even with Notifications too:

<?php

namespace App\Notifications;

use App\Mail\OrderShipped as Mailable;
use Illuminate\Notifications\Notification;

class OrderShipped extends Notification
{
    // ...

    public function toMail($notifiable)
    {
        return (new Mailable($this->order))->to($notifiable->email);
    }
}

Testing

Run the tests with:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.


Travel shouldn't damage the earth 🌍

Made with ❤️  at Pod Point

Related Packages

laravel-junkies/backblaze

With Backblaze you can create Backups astonishingly easy.

0 1
runcloudio/b2-sdk-php

An SDK for working with B2 cloud storage.

2,915 1
ringlesoft/db-archive

A Laravel package for archiving old database entries automatically

609 4
websolutionfalcon/laravel-simple-cold-storage

A simple, extensible Laravel package for storing data using filesystem with type...

3,516 5
obregonco/backblaze-b2

An SDK for working with B2 cloud storage.

35,953 27

Version History

Version Released PHP Laravel License
2.2.0 ^8.1 ^9.0|^10.0|^11.0 MIT
2.1.0 ^8.1 ^9.0|^10.0 MIT
2.0.0 ^8.0 ^9.0 MIT
1.0.0 ^7.2.5|^8.0 ~7.0|^8.0 MIT
0.2.2 ^7.2.5|^8.0 ~7.0|^8.0 MIT