|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EmailCampaigns\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
|
6
|
|
|
use Illuminate\Support\Facades\Mail; |
|
7
|
|
|
use Illuminate\Queue\SerializesModels; |
|
8
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
9
|
|
|
use Spatie\EmailCampaigns\Support\Config; |
|
10
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
11
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
12
|
|
|
use Spatie\RateLimitedMiddleware\RateLimited; |
|
13
|
|
|
use Spatie\EmailCampaigns\Models\CampaignSend; |
|
14
|
|
|
use Spatie\EmailCampaigns\Events\CampaignMailSent; |
|
15
|
|
|
use Spatie\EmailCampaigns\Actions\PersonalizeHtmlAction; |
|
16
|
|
|
|
|
17
|
|
|
class SendMailJob implements ShouldQueue |
|
18
|
|
|
{ |
|
19
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
20
|
|
|
|
|
21
|
|
|
public $deleteWhenMissingModels = true; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \Spatie\EmailCampaigns\Models\CampaignSend */ |
|
24
|
|
|
public $pendingSend; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
public $queue; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(CampaignSend $pendingSend) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->pendingSend = $pendingSend; |
|
32
|
|
|
|
|
33
|
|
|
$this->queue = config('email-campaigns.perform_on_queue.send_mail_job'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function handle() |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->pendingSend->wasAlreadySent()) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$action = Config::getActionClass('personalize_html_action', PersonalizeHtmlAction::class); |
|
43
|
|
|
|
|
44
|
|
|
$personalisedHtml = $action->execute( |
|
45
|
|
|
$this->pendingSend->campaign->email_html, |
|
46
|
|
|
$this->pendingSend, |
|
47
|
|
|
); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$campaignMail = $this->pendingSend->campaign->getMailable() |
|
50
|
|
|
->setCampaignSend($this->pendingSend) |
|
51
|
|
|
->setContent($personalisedHtml) |
|
52
|
|
|
->subject($this->pendingSend->campaign->subject); |
|
53
|
|
|
|
|
54
|
|
|
Mail::to($this->pendingSend->subscription->subscriber->email)->send($campaignMail); |
|
55
|
|
|
|
|
56
|
|
|
$this->pendingSend->markAsSent(); |
|
57
|
|
|
|
|
58
|
|
|
event(new CampaignMailSent($this->pendingSend)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function middleware() |
|
62
|
|
|
{ |
|
63
|
|
|
$throttlingConfig = config('email-campaigns.throttling'); |
|
64
|
|
|
|
|
65
|
|
|
$rateLimitedMiddleware = (new RateLimited()) |
|
66
|
|
|
->enabled($throttlingConfig['enabled']) |
|
67
|
|
|
->connectionName($throttlingConfig['redis_connection_name']) |
|
68
|
|
|
->allow($throttlingConfig['allowed_number_of_jobs_in_timespan']) |
|
69
|
|
|
->everySeconds($throttlingConfig['timespan_in_seconds']) |
|
70
|
|
|
->releaseAfterSeconds($throttlingConfig['release_in_seconds']); |
|
71
|
|
|
|
|
72
|
|
|
return [$rateLimitedMiddleware]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|