diff --git a/config/laravel-database-emails.php b/config/laravel-database-emails.php index 61d9164..ef9c2f4 100644 --- a/config/laravel-database-emails.php +++ b/config/laravel-database-emails.php @@ -58,4 +58,17 @@ */ 'limit' => 20, + + /* + |-------------------------------------------------------------------------- + | Send E-mails Immediately + |-------------------------------------------------------------------------- + | + | Sends e-mails immediately after calling send() or schedule(). Useful for development + | when you don't have Laravel Scheduler running or don't want to wait up to + | 60 seconds for each e-mail to be sent. + | + */ + + 'immediately' => env('LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY', false), ]; diff --git a/src/Config.php b/src/Config.php index af8cd1e..ab5a16d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -53,4 +53,14 @@ public static function cronjobEmailLimit() { return config('laravel-database-emails.limit', 20); } + + /** + * Determine if e-mails should be sent immediately. + * + * @return bool + */ + public static function sendImmediately() + { + return (bool) config('laravel-database-emails.immediately', false); + } } diff --git a/src/EmailComposer.php b/src/EmailComposer.php index 09b16c4..a7d25ff 100644 --- a/src/EmailComposer.php +++ b/src/EmailComposer.php @@ -271,6 +271,12 @@ public function send() $this->email->save(); - return $this->email->fresh(); + $this->email->refresh(); + + if (Config::sendImmediately()) { + $this->email->send(); + } + + return $this->email; } } diff --git a/src/Preparer.php b/src/Preparer.php index b2d69ac..b4e98fd 100644 --- a/src/Preparer.php +++ b/src/Preparer.php @@ -34,6 +34,8 @@ public function prepare(EmailComposer $composer) $this->prepareAttachments($composer); $this->prepareScheduled($composer); + + $this->prepareImmediately($composer); } /** @@ -221,4 +223,16 @@ private function prepareScheduled(EmailComposer $composer) 'scheduled_at' => $scheduled->toDateTimeString(), ]); } + + /** + * Prepare the e-mail so it can be sent immediately. + * + * @param EmailComposer $composer + */ + private function prepareImmediately(EmailComposer $composer) + { + if (Config::sendImmediately()) { + $composer->getEmail()->fill(['sending' => 1]); + } + } } diff --git a/tests/SenderTest.php b/tests/SenderTest.php index 0ab3b17..cd16a0e 100644 --- a/tests/SenderTest.php +++ b/tests/SenderTest.php @@ -5,6 +5,8 @@ use Dompdf\Dompdf; use Swift_Events_SendEvent; use Illuminate\Support\Facades\Mail; +use Stackkit\LaravelDatabaseEmails\Email; +use Stackkit\LaravelDatabaseEmails\Config; class SenderTest extends TestCase { @@ -212,6 +214,22 @@ public function raw_attachments_are_added_to_the_email() $this->assertContains('Hello CI!', $attachment->getBody()); } + /** @test */ + public function emails_can_be_sent_immediately() + { + $this->app['config']->set('laravel-database-emails.immediately', false); + $this->sendEmail(); + $this->assertCount(0, $this->sent); + Email::truncate(); + + $this->app['config']->set('laravel-database-emails.immediately', true); + $this->sendEmail(); + $this->assertCount(1, $this->sent); + + $this->artisan('email:send'); + $this->assertCount(1, $this->sent); + } + /** @test */ public function raw_attachments_are_not_added_if_the_data_is_not_valid() {