Skip to content

Commit ac18e60

Browse files
committed
added option to queue e-mail
1 parent d38c7e7 commit ac18e60

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/EmailComposer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ public function later($scheduledAt)
194194
return $this->send();
195195
}
196196

197+
/**
198+
* Queue the e-mail.
199+
*
200+
* @return Email
201+
*/
202+
public function queue($connection = null, $queue = null)
203+
{
204+
$connection = $connection ?: config('queue.default');
205+
$queue = $queue ?: 'default';
206+
207+
$this->setData('queued', true);
208+
$this->setData('connection', $connection);
209+
$this->setData('queue', $queue);
210+
211+
return $this->send();
212+
}
213+
197214
/**
198215
* Set the Mailable.
199216
*
@@ -273,6 +290,14 @@ public function send()
273290

274291
$this->email->refresh();
275292

293+
if ($this->getData('queued') === true) {
294+
dispatch(new SendEmailJob($this->email))
295+
->onConnection($this->getData('connection'))
296+
->onQueue($this->getData('queue'));
297+
298+
return $this->email;
299+
}
300+
276301
if (Config::sendImmediately()) {
277302
$this->email->send();
278303
}

src/SendEmailJob.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
4+
namespace Stackkit\LaravelDatabaseEmails;
5+
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class SendEmailJob implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
private $email;
17+
18+
public function __construct(Email $email)
19+
{
20+
$this->email = $email;
21+
}
22+
23+
public function handle()
24+
{
25+
(new Sender())->send($this->email);
26+
}
27+
}

0 commit comments

Comments
 (0)