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')->everyMinute();
}
Email::compose()
->label('welcome-mail-1.0')
->recipient('[email protected]')
->subject('This is a test')
->view('emails.welcome')
->variables([
'name' => 'John Doe',
])
->send();
$one = '[email protected]';
$multiple = ['[email protected]', '[email protected]'];
Email::compose()->recipient($one);
Email::compose()->recipient($multiple);
Email::compose()->cc($one);
Email::compose()->cc($multiple);
Email::compose()->bcc($one);
Email::compose()->bcc($multiple);
You may also pass a mailable to the e-mail composer.
Email::compose()
->mailable(new OrderShipped())
->send();
Email::compose()
->attach('/path/to/file');
Or for in-memory attachments...
Email::compose()
->attachData('<p>Your order has shipped!</p>', 'order.html');
You may schedule an e-mail by calling later
instead of send
at the end of the chain. You must provide a Carbon instance or a strtotime valid date.
Email::compose()
->later('+2 hours');
If you're not running the cronjob and wish to send the queued e-mails, you can run the email:send
command.
$ php artisan email:send
By default, we will attempt to send an e-mail 3 times if it fails. If it still fails the 3rd time, it will permanently be marked as failed. You can change the number of times an e-mail should be attempted to be sent using the retry.attempts
configuration.
If you wish to retry sending failed e-mails, you may call the email:retry
command. The command will grab any failed e-mail and push it onto the queue. You may also provide the id of a specific e-mail.
$ php artisan email:retry
# or...
$ php artisan email:retry 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 encrypting the e-mail body takes a lot of disk space.
If you wish to send e-mails to a test address but don't necessarily want to use a service like mailtrap, please take a look at the testing
configuration. This is turned off by default.
During the creation of an e-mail, the recipient will be replaced by the test e-mail. This is useful for local development or testing on a staging server.