update
This commit is contained in:
@@ -2,14 +2,21 @@
|
||||
|
||||
namespace App\Jobs\Mail;
|
||||
|
||||
use App\Mail\Customer\ManualCustomerEmail;
|
||||
use App\Mail\GenericTemplateMail;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Mail as MailLog;
|
||||
use App\Models\MailCampaign;
|
||||
use App\Models\Notification;
|
||||
use App\Services\Email\MailCampaignRenderer;
|
||||
use App\Support\EmailRecipients;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class SendMailCampaignRecipient implements ShouldQueue
|
||||
@@ -18,87 +25,82 @@ class SendMailCampaignRecipient implements ShouldQueue
|
||||
|
||||
public function __construct(
|
||||
private readonly int $campaignId,
|
||||
private readonly int $customerId,
|
||||
private readonly string $subject,
|
||||
private readonly string $message,
|
||||
private readonly ?string $cc = null,
|
||||
private readonly ?string $bcc = null,
|
||||
private readonly ?int $customerId,
|
||||
private readonly string $email,
|
||||
) {}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$campaign = MailCampaign::find($this->campaignId);
|
||||
$customer = Customer::find($this->customerId);
|
||||
|
||||
if (!$campaign || !$customer || !$customer->email) {
|
||||
$this->markSent($campaign, failed: true);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$mailer = Mail::to($customer->email);
|
||||
if ($cc = $this->parseRecipients($this->cc)) {
|
||||
$mailer->cc($cc);
|
||||
}
|
||||
if ($bcc = $this->parseRecipients($this->bcc)) {
|
||||
$mailer->bcc($bcc);
|
||||
}
|
||||
$mailer->send(new ManualCustomerEmail($this->subject, $this->message));
|
||||
|
||||
\App\Models\Mail::create([
|
||||
'campaign_id' => $campaign->id,
|
||||
'template_id' => $campaign->template_id ?? 0,
|
||||
'subject' => $this->subject,
|
||||
'to' => $customer->email,
|
||||
'customer_id' => $customer->id,
|
||||
'message' => $this->message,
|
||||
'status' => 'sent',
|
||||
]);
|
||||
|
||||
$this->markSent($campaign, failed: false);
|
||||
} catch (\Throwable) {
|
||||
\App\Models\Mail::create([
|
||||
'campaign_id' => $campaign->id,
|
||||
'template_id' => $campaign->template_id ?? 0,
|
||||
'subject' => $this->subject,
|
||||
'to' => $customer->email,
|
||||
'customer_id' => $customer->id,
|
||||
'message' => $this->message,
|
||||
'status' => 'failed',
|
||||
]);
|
||||
|
||||
$this->markSent($campaign, failed: true);
|
||||
}
|
||||
}
|
||||
|
||||
private function parseRecipients(?string $value): array
|
||||
{
|
||||
if (!$value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return collect(preg_split('/[,;]+/', $value) ?: [])
|
||||
->map(fn ($email) => trim((string) $email))
|
||||
->filter(fn ($email) => $email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL))
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
private function markSent(?MailCampaign $campaign, bool $failed): void
|
||||
public function handle(MailCampaignRenderer $renderer): void
|
||||
{
|
||||
$campaign = MailCampaign::with('template')->find($this->campaignId);
|
||||
if (!$campaign) {
|
||||
return;
|
||||
}
|
||||
|
||||
$campaign->increment($failed ? 'failed_count' : 'sent_count');
|
||||
$campaign->refresh();
|
||||
$customer = $this->customerId ? Customer::find($this->customerId) : null;
|
||||
$rendered = $renderer->render($campaign, $renderer->recipientValues($campaign, $customer, $this->email));
|
||||
$cc = EmailRecipients::parse($campaign->cc);
|
||||
|
||||
if ($campaign->sent_count + $campaign->failed_count >= $campaign->total_recipients) {
|
||||
$campaign->update([
|
||||
'status' => $campaign->failed_count > 0 && $campaign->sent_count === 0 ? 'failed' : 'completed',
|
||||
$failed = false;
|
||||
try {
|
||||
$mailer = Mail::to($this->email);
|
||||
if ($cc) {
|
||||
$mailer->cc($cc);
|
||||
}
|
||||
if ($bcc = EmailRecipients::parse($campaign->bcc)) {
|
||||
$mailer->bcc($bcc);
|
||||
}
|
||||
$mailer->send(new GenericTemplateMail(
|
||||
$rendered['subject'],
|
||||
$rendered['html'],
|
||||
Arr::only($campaign->template?->mailConfig() ?? [], ['from_email', 'from_name', 'reply_to', 'reply_to_name']),
|
||||
));
|
||||
} catch (\Throwable $e) {
|
||||
$failed = true;
|
||||
Log::warning('Gửi email chiến dịch thất bại', [
|
||||
'campaign_id' => $campaign->id,
|
||||
'to' => $this->email,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
MailLog::create([
|
||||
'campaign_id' => $campaign->id,
|
||||
'template_id' => $campaign->template_id ?? 0,
|
||||
'subject' => $rendered['subject'],
|
||||
'to' => $this->email,
|
||||
'cc' => implode(', ', $cc),
|
||||
'customer_id' => $customer?->id,
|
||||
'message' => $rendered['html'],
|
||||
'status' => $failed ? 'failed' : 'sent',
|
||||
]);
|
||||
|
||||
$this->markDone($campaign, $failed);
|
||||
}
|
||||
|
||||
private function markDone(MailCampaign $campaign, bool $failed): void
|
||||
{
|
||||
$campaign->increment($failed ? 'failed_count' : 'sent_count');
|
||||
|
||||
$finished = MailCampaign::whereKey($campaign->id)
|
||||
->whereIn('status', ['queued', 'sending'])
|
||||
->whereRaw('sent_count + failed_count >= total_recipients')
|
||||
->update([
|
||||
'status' => DB::raw("CASE WHEN sent_count = 0 THEN 'failed' ELSE 'completed' END"),
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
|
||||
if ($finished && $campaign->created_by) {
|
||||
$campaign->refresh();
|
||||
Notification::create([
|
||||
'recipient_type' => 'admin',
|
||||
'recipient_id' => $campaign->created_by,
|
||||
'type' => 'mail_campaign_completed',
|
||||
'title' => $campaign->status === 'failed' ? 'Gửi email thất bại' : 'Đã gửi email thành công',
|
||||
'body' => "{$campaign->name}: {$campaign->sent_count}/{$campaign->total_recipients} email đã gửi"
|
||||
. ($campaign->failed_count ? ", {$campaign->failed_count} lỗi." : '.'),
|
||||
'link' => "/mail-campaigns/{$campaign->uuid}",
|
||||
'data' => ['campaign_uuid' => $campaign->uuid],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user