Skip to content

Commit bf7862c

Browse files
committed
Added option to send e-mails immediately
1 parent c91e98b commit bf7862c

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

config/laravel-database-emails.php

Lines changed: 13 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ public function send()
259259

260260
$this->email->save();
261261

262-
return $this->email->fresh();
262+
$this->email->refresh();
263+
264+
if (Config::sendImmediately()) {
265+
$this->email->send();
266+
}
267+
268+
return $this->email;
263269
}
264270
}

src/Preparer.php

Lines changed: 14 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Tests;
44

55
use Dompdf\Dompdf;
6+
use Stackkit\LaravelDatabaseEmails\Config;
7+
use Stackkit\LaravelDatabaseEmails\Email;
68
use Swift_Events_SendEvent;
79
use Illuminate\Support\Facades\Mail;
810

@@ -189,4 +191,20 @@ public function raw_attachments_are_added_to_the_email()
189191
$this->assertEquals('application/pdf', $attachment->getContentType());
190192
$this->assertContains('Hello CI!', $attachment->getBody());
191193
}
194+
195+
/** @test */
196+
public function emails_can_be_sent_immediately()
197+
{
198+
$this->app['config']->set('laravel-database-emails.immediately', false);
199+
$this->sendEmail();
200+
$this->assertCount(0, $this->sent);
201+
Email::truncate();
202+
203+
$this->app['config']->set('laravel-database-emails.immediately', true);
204+
$this->sendEmail();
205+
$this->assertCount(1, $this->sent);
206+
207+
$this->artisan('email:send');
208+
$this->assertCount(1, $this->sent);
209+
}
192210
}

0 commit comments

Comments
 (0)