This package allows you to store and send e-mails using a database.
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.
This package requires Laravel 5.6 or higher.
Please check the table below for supported Laravel and PHP versions:
Laravel Version | PHP Version |
---|---|
5.6 | 7.2 or 7.3 |
5.7 | 7.2 or 7.3 |
5.8 | 7.2 or 7.3 or 7.4 |
6.x | 7.2 or 7.3 or 7.4 or 8.0 |
7.x | 7.2 or 7.3 or 7.4 or 8.0 |
8.x | 7.3 or 7.4 or 8.0 |
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);
}
<?php
use Stackkit\LaravelDatabaseEmails\Email;
Email::compose()
->label('welcome')
->recipient('[email protected]')
->subject('This is a test')
->view('emails.welcome')
->variables([
'name' => 'John Doe',
])
->send();
<?php
use Stackkit\LaravelDatabaseEmails\Email;
Email::compose()
->recipient([
'[email protected]',
'[email protected]'
]);
<?php
use Stackkit\LaravelDatabaseEmails\Email;
Email::compose()
->cc('[email protected]')
->cc(['[email protected]', '[email protected]'])
->bcc('[email protected]')
->bcc(['[email protected]', '[email protected]']);
You may also pass a mailable to the e-mail composer.
<?php
use Stackkit\LaravelDatabaseEmails\Email;
Email::compose()
->mailable(new OrderShipped())
->send();
<?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');
<?php
use Stackkit\LaravelDatabaseEmails\Email;
Email::compose()
->from('[email protected]', 'John Doe');
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');
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.
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));
When enabled, all newly created e-mails will be sent to the specified test e-mail address. This is turned off by default.
To configure how many e-mails should be sent each command, please check the limit
option. The default is 20
e-mails every command.
Useful during development when Laravel Scheduler is not running
To enable, set the following environment variable:
LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY=true