PHPackages                             asteriskpound/laravel-database-emails - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. asteriskpound/laravel-database-emails

ActiveLibrary[Database &amp; ORM](/categories/database)

asteriskpound/laravel-database-emails
=====================================

Store and send e-mails using the database

7.1.1(3mo ago)05MITPHP

Since Jun 29Pushed 3mo agoCompare

[ Source](https://github.com/AsteriskPound/laravel-database-emails)[ Packagist](https://packagist.org/packages/asteriskpound/laravel-database-emails)[ RSS](/packages/asteriskpound-laravel-database-emails/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (35)Used By (0)

[![Run tests](https://github.com/stackkit/laravel-database-emails/actions/workflows/run-tests.yml/badge.svg)](https://github.com/stackkit/laravel-database-emails/actions/workflows/run-tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/dab7648db8424ddabc2888543e04bc8841901f8f2c26822cfc84ef2a03b928b5/68747470733a2f2f706f7365722e707567782e6f72672f737461636b6b69742f6c61726176656c2d64617461626173652d656d61696c732f762f737461626c652e737667)](https://packagist.org/packages/stackkit/laravel-database-emails)[![Total Downloads](https://camo.githubusercontent.com/3354e1a0db2d8accac02a89bf4fcecb8a53f1afb87dbf279bdad45bd3c5feb3b/68747470733a2f2f706f7365722e707567782e6f72672f737461636b6b69742f6c61726176656c2d64617461626173652d656d61696c732f646f776e6c6f6164732e737667)](https://packagist.org/packages/stackkit/laravel-database-emails)

Introduction
============

[](#introduction)

This package allows you to store and send e-mails using the database.

Requirements
============

[](#requirements)

This package requires Laravel 10 or 11.

Installation
============

[](#installation)

Require the package using composer.

```
composer require stackkit/laravel-database-emails
```

Publish the configuration files.

```
php artisan vendor:publish --tag=database-emails-config
php artisan vendor:publish --tag=database-emails-migrations
```

Create the database table required for this package.

```
php artisan migrate
```

Add the e-mail cronjob to your scheduler

```
protected function schedule(Schedule $schedule)
{
     $schedule->command('email:send')->everyMinute()->withoutOverlapping(5);
}
```

Usage
=====

[](#usage)

### Send an email

[](#send-an-email)

E-mails are composed the same way mailables are created.

```
use Stackkit\LaravelDatabaseEmails\Email;
use Illuminate\Mail\Mailables\Content;
use Stackkit\LaravelDatabaseEmails\Attachment;
use Illuminate\Mail\Mailables\Envelope;

Email::compose()
    ->content(fn (Content $content) => $content
        ->view('tests::dummy')
        ->with(['name' => 'John Doe'])
    )
    ->envelope(fn (Envelope $envelope) => $envelope
        ->subject('Hello')
        ->from('johndoe@example.com', 'John Doe')
        ->to('janedoe@example.com', 'Jane Doe')
    )
    ->attachments([
        Attachment::fromStorageDisk('s3', '/invoices/john-doe/march-2024.pdf'),
    ])
    ->send();
])
```

### Sending emails to users in your application

[](#sending-emails-to-users-in-your-application)

```
Email::compose()
    ->user($user)
    ->send();
```

By default, the `name` column will be used to set the recipient's name. If you wish to use something different, you should implement the `preferredEmailName` method in your model.

```
class User extends Model
{
    public function preferredEmailName(): string
    {
        return $this->first_name;
    }
}
```

By default, the `email` column will be used to set the recipient's e-mail address. If you wish to use something different, you should implement the `preferredEmailAddress` method in your model.

```
class User extends Model
{
    public function preferredEmailAddress(): string
    {
        return $this->work_email;
    }
}
```

By default, the app locale will be used. If you wish to use something different, you should implement the `preferredEmailLocale` method in your model.

```
class User extends Model implements HasLocalePreference
{
    public function preferredLocale(): string
    {
        return $this->locale;
    }
}
```

### Using mailables

[](#using-mailables)

You may also pass a mailable to the e-mail composer.

```
Email::compose()
    ->mailable(new OrderShipped())
    ->send();
```

### Attachments

[](#attachments)

To start attaching files to your e-mails, you may use the `attachments` method like you normally would in Laravel. However, you will have to use this package's `Attachment` class.

```
use Stackkit\LaravelDatabaseEmails\Attachment;

Email::compose()
    ->attachments([
        Attachment::fromPath(__DIR__.'/files/pdf-sample.pdf'),
        Attachment::fromPath(__DIR__.'/files/my-file.txt')->as('Test123 file'),
        Attachment::fromStorageDisk('my-custom-disk', 'test.txt'),
    ])
    ->send();
```

Note

`Attachment::fromData()` and `Attachment::fromStorage()` are not supported as they work with raw data.

### Attaching models to e-mails

[](#attaching-models-to-e-mails)

You may attach a model to an e-mail. This can be useful to attach a user or another model that belongs to the e-mail.

```
Email::compose()
    ->model(User::find(1));
```

### Scheduling

[](#scheduling)

You may schedule an e-mail by calling `later` instead of `send`. You must provide a Carbon instance or a strtotime valid date.

```
Email::compose()
    ->later('+2 hours');
```

### Queueing e-mails

[](#queueing-e-mails)

Important

When queueing mail using the `queue` function, it is no longer necessary to schedule the `email:send` command.

```
Email::compose()->queue();

// On a specific connection
Email::compose()->queue(connection: 'sqs');

// On a specific queue
Email::compose()->queue(queue: 'email-queue');

// Delay (send mail in 10 minutes)
Email::compose()->queue(delay: now()->addMinutes(10));
```

If you need more flexibility, you may also pass your own job class:

```
Email::compose()->queue(jobClass: CustomSendEmailJob::class);
```

It could look like this:

```
