Skip to content

Added option to send e-mails immediately #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/laravel-database-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
10 changes: 10 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 7 additions & 1 deletion src/EmailComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function prepare(EmailComposer $composer)
$this->prepareAttachments($composer);

$this->prepareScheduled($composer);

$this->prepareImmediately($composer);
}

/**
Expand Down Expand Up @@ -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]);
}
}
}
18 changes: 18 additions & 0 deletions tests/SenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand Down