Best laravel framework open-source packages.

Laravel database emails

Store e-mails in the database and send them using a cronjob
Updated 2 months ago

Build Status Latest Stable Version License

Package Documentation

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

Contribution

The package is MIT licenced, meaning it's open source and you are free to copy or fork it and modify it any way you wish.

We feel the package is currently feature complete, but feel free to send a pull request or help improve existing code.

Requirements

This package requires Laravel 6.0 or higher.

Please check the Laravel support policy table for supported Laravel and PHP versions.

Installation

Require the package using composer.

composer require stackkit/laravel-database-emails

Publish the configuration files.

php artisan vendor:publish --tag=laravel-database-emails-config

Create the database table required for this package.

php artisan migrate

Add the e-mail cronjob to your scheduler

<?php

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
     $schedule->command('email:send')->everyMinute()->withoutOverlapping(5);
}

Usage

Send an email

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->label('welcome')
    ->recipient('john@doe.com')
    ->subject('This is a test')
    ->view('emails.welcome')
    ->variables([
        'name' => 'John Doe',
    ])
    ->send();

Specify multiple recipients

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->recipient([
        'john@doe.com',
        'jane@doe.com'
    ]);

CC and BCC

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->cc('john@doe.com')
    ->cc(['john@doe.com', 'jane@doe.com'])
    ->bcc('john@doe.com')
    ->bcc(['john@doe.com', 'jane@doe.com']);

Reply-To

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->replyTo(['john@doe.com', 'jane@doe.com']);

Email::compose()
    ->replyTo(new Address('john@doe.com', 'John Doe'));

Email::compose()
    ->replyTo([
        new Address('john@doe.com', 'John Doe'),
        new Address('jane@doe.com', 'Jane Doe'),
    ]);

Using mailables

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

<?php

use Stackkit\LaravelDatabaseEmails\Email;

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

Attachments

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->attach('/path/to/file');

Or for in-memory attachments:

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->attachData('<p>Your order has shipped!</p>', 'order.html');

Custom Sender

<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
    ->from('john@doe.com', 'John Doe');

Scheduling

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

<?php

use Stackkit\LaravelDatabaseEmails\Email;

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

Encryption (Optional)

If you wish to encrypt your e-mails, please enable the encrypt option in the configuration file. This is disabled by default. Encryption and decryption will be handled by Laravel's built-in encryption mechanism. Please note that by encrypting the e-mail it takes more disk space.

Without encryption

7    bytes (label)
16   bytes (recipient)
20   bytes (subject)
48   bytes (view name)
116  bytes (variables)
1874 bytes (e-mail content)
4    bytes (attempts, sending, failed, encrypted)
57   bytes (created_at, updated_at, deleted_at)
... x 10.000 rows = ± 21.55 MB

With encryption the table size is ± 50.58 MB.

Queueing e-mails

Important: When queueing mail using the queue function, it is no longer necessary to schedule the email:send command. Please make sure it is removed from app/Console/Kernel.php.

<?php

use Stackkit\LaravelDatabaseEmails\Email;

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

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

// on a specific queue
Email::compose()
    ->queue(null, 'email-queue');

// timeout (send mail in 10 minutes)
Email::compose()
    ->queue(null, null, now()->addMinutes(10));

Test mode (Optional)

When enabled, all newly created e-mails will be sent to the specified test e-mail address. This is turned off by default.

E-mails to send per minute

To configure how many e-mails should be sent each command, please check the limit option. The default is 20 e-mails every command.

Send e-mails immediately (Optional)

Useful during development when Laravel Scheduler is not running

To enable, set the following environment variable:

LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY=true

Pruning models

use Stackkit\LaravelDatabaseEmails\Email;

$schedule->command('model:prune', [
    '--model' => [Email::class],
])->everyMinute();

By default, e-mails are pruned when they are older than 6 months.

You may change that by adding the following to the AppServiceProvider.php:

use Stackkit\LaravelDatabaseEmails\Email;

public function register(): void
{
    Email::pruneWhen(function (Email $email) {
        return $email->where(...);
    });
}