Skip to content

Commit e87f655

Browse files
Merge pull request #24 from stackkit/feature/send-emails-immediately
Added option to send e-mails immediately
2 parents 162f5dd + 143fb0c commit e87f655

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

config/laravel-database-emails.php

+13
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,17 @@
5858
*/
5959

6060
'limit' => 20,
61+
62+
/*
63+
|--------------------------------------------------------------------------
64+
| Send E-mails Immediately
65+
|--------------------------------------------------------------------------
66+
|
67+
| Sends e-mails immediately after calling send() or schedule(). Useful for development
68+
| when you don't have Laravel Scheduler running or don't want to wait up to
69+
| 60 seconds for each e-mail to be sent.
70+
|
71+
*/
72+
73+
'immediately' => env('LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY', false),
6174
];

src/Config.php

+10
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ public static function cronjobEmailLimit()
5353
{
5454
return config('laravel-database-emails.limit', 20);
5555
}
56+
57+
/**
58+
* Determine if e-mails should be sent immediately.
59+
*
60+
* @return bool
61+
*/
62+
public static function sendImmediately()
63+
{
64+
return (bool) config('laravel-database-emails.immediately', false);
65+
}
5666
}

src/EmailComposer.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ public function send()
271271

272272
$this->email->save();
273273

274-
return $this->email->fresh();
274+
$this->email->refresh();
275+
276+
if (Config::sendImmediately()) {
277+
$this->email->send();
278+
}
279+
280+
return $this->email;
275281
}
276282
}

src/Preparer.php

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function prepare(EmailComposer $composer)
3434
$this->prepareAttachments($composer);
3535

3636
$this->prepareScheduled($composer);
37+
38+
$this->prepareImmediately($composer);
3739
}
3840

3941
/**
@@ -221,4 +223,16 @@ private function prepareScheduled(EmailComposer $composer)
221223
'scheduled_at' => $scheduled->toDateTimeString(),
222224
]);
223225
}
226+
227+
/**
228+
* Prepare the e-mail so it can be sent immediately.
229+
*
230+
* @param EmailComposer $composer
231+
*/
232+
private function prepareImmediately(EmailComposer $composer)
233+
{
234+
if (Config::sendImmediately()) {
235+
$composer->getEmail()->fill(['sending' => 1]);
236+
}
237+
}
224238
}

tests/SenderTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Dompdf\Dompdf;
66
use Swift_Events_SendEvent;
77
use Illuminate\Support\Facades\Mail;
8+
use Stackkit\LaravelDatabaseEmails\Email;
9+
use Stackkit\LaravelDatabaseEmails\Config;
810

911
class SenderTest extends TestCase
1012
{
@@ -212,6 +214,22 @@ public function raw_attachments_are_added_to_the_email()
212214
$this->assertContains('Hello CI!', $attachment->getBody());
213215
}
214216

217+
/** @test */
218+
public function emails_can_be_sent_immediately()
219+
{
220+
$this->app['config']->set('laravel-database-emails.immediately', false);
221+
$this->sendEmail();
222+
$this->assertCount(0, $this->sent);
223+
Email::truncate();
224+
225+
$this->app['config']->set('laravel-database-emails.immediately', true);
226+
$this->sendEmail();
227+
$this->assertCount(1, $this->sent);
228+
229+
$this->artisan('email:send');
230+
$this->assertCount(1, $this->sent);
231+
}
232+
215233
/** @test */
216234
public function raw_attachments_are_not_added_if_the_data_is_not_valid()
217235
{

0 commit comments

Comments
 (0)