This is a package that stores and queues e-mails using a database table. Easily send e-mails using a cronjob and schedule e-mails that should be sent at a specific date and time.
First, require the package using composer.
composer require buildcode/laravel-database-emails
If you're running Laravel 5.5 or later you may skip this step. Add the service provider to your application.
Buildcode\LaravelDatabaseEmails\LaravelDatabaseEmailsServiceProvider::class,
Publish the configuration files.
php artisan vendor:publish --provider=Buildcode\\LaravelDatabaseEmails\\LaravelDatabaseEmailsServiceProvider
Create the database table required for this package.
php artisan migrate
Now add the e-mail cronjob to your scheduler.
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('email:send', ['--timeout' => 300])->everyMinute()->withoutOverlapping(5);
}
Using the above configuration, the email:send
process will exit after 5 minutes (--timeout
) and won't overlap if the process still runs after 5 minutes (withoutOverlapping
)
use Buildcode\LaravelDatabaseEmails\Email;
Email::compose()
->label('welcome')
->recipient('[email protected]')
->subject('This is a test')
->view('emails.welcome')
->variables([
'name' => 'John Doe',
])
->send();
use Buildcode\LaravelDatabaseEmails\Email;
Buildcode\LaravelDatabaseEmails\Email::compose()
->recipient([
'[email protected]',
'[email protected]'
]);
use Buildcode\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.
use Buildcode\LaravelDatabaseEmails\Email;
Email::compose()
->mailable(new OrderShipped())
->send();
use Buildcode\LaravelDatabaseEmails\Email;
Email::compose()
->attach('/path/to/file');
Or for in-memory attachments:
use Buildcode\LaravelDatabaseEmails\Email;
Email::compose()
->attachData('<p>Your order has shipped!</p>', 'order.html');
use Buildcode\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.
use Buildcode\LaravelDatabaseEmails\Email;
Email::compose()
->later('+2 hours');
php artisan email:resend
php artisan email:resend 1
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.
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.