- Mail campaign: mẫu nhắc gia hạn thanh toán QR, chọn dịch vụ + chu kỳ từng dòng (renewal_services) - Hosting: nhiều server Plesk cho một gói (service_plan_servers), chuyển server khi server chết - Admin xem/sửa giấy tờ định danh khách, tuỳ chọn gia hạn cho khách, nhật ký cấp phát dịch vụ - Đánh dấu mẫu email dịch vụ là non-marketing Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
144 lines
5.6 KiB
PHP
144 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Mail;
|
|
|
|
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 App\Support\RenewalCampaign;
|
|
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
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public function __construct(
|
|
private readonly int $campaignId,
|
|
private readonly ?int $customerId,
|
|
private readonly string $email,
|
|
private readonly ?int $serviceId = null,
|
|
) {}
|
|
|
|
public function handle(MailCampaignRenderer $renderer): void
|
|
{
|
|
$campaign = MailCampaign::with('template')->find($this->campaignId);
|
|
if (!$campaign || $campaign->status === MailCampaign::STATUS_CANCELLED) {
|
|
return;
|
|
}
|
|
|
|
$customer = $this->customerId ? Customer::find($this->customerId) : null;
|
|
if ($customer && $campaign->isMarketing() && !$customer->acceptsMarketingEmail()) {
|
|
$this->markDone($campaign, true);
|
|
|
|
return;
|
|
}
|
|
|
|
$values = $renderer->recipientValues($campaign, $customer, $this->email);
|
|
if ($this->serviceId !== null) {
|
|
$renewal = $customer ? RenewalCampaign::valuesFor($campaign, $customer, $this->serviceId) : null;
|
|
if ($renewal === null) {
|
|
MailLog::create([
|
|
'campaign_id' => $campaign->id,
|
|
'template_id' => $campaign->template_id ?? 0,
|
|
'subject' => $campaign->subject,
|
|
'to' => $this->email,
|
|
'customer_id' => $customer?->id,
|
|
'service_id' => $this->serviceId,
|
|
'sent_by' => $campaign->created_by,
|
|
'message' => 'Không gửi: dịch vụ không còn thuộc khách hàng hoặc không thể tính giá gia hạn.',
|
|
'status' => 'failed',
|
|
]);
|
|
$this->markDone($campaign, true);
|
|
|
|
return;
|
|
}
|
|
$values = [...$values, ...$renewal];
|
|
}
|
|
|
|
$rendered = $renderer->render($campaign, $values);
|
|
$cc = EmailRecipients::parse($campaign->cc);
|
|
$bcc = EmailRecipients::parse($campaign->bcc);
|
|
$mailConfig = Arr::only($campaign->template?->mailConfig() ?? [], ['from_email', 'from_name', 'reply_to', 'reply_to_name']);
|
|
|
|
$failed = false;
|
|
try {
|
|
$mailer = Mail::to($this->email);
|
|
if ($cc) {
|
|
$mailer->cc($cc);
|
|
}
|
|
if ($bcc) {
|
|
$mailer->bcc($bcc);
|
|
}
|
|
$failed = $mailer->send(new GenericTemplateMail(
|
|
$rendered['subject'],
|
|
$rendered['html'],
|
|
$mailConfig,
|
|
storedFiles: $campaign->attachments ?? [],
|
|
)) === null;
|
|
} 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'],
|
|
'from_address' => $mailConfig['from_email'] ?? config('mail.from.address'),
|
|
'to' => $this->email,
|
|
'cc' => implode(', ', $cc),
|
|
'bcc' => implode(', ', $bcc),
|
|
'customer_id' => $customer?->id,
|
|
'service_id' => $this->serviceId,
|
|
'sent_by' => $campaign->created_by,
|
|
'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', MailCampaign::ACTIVE_STATUSES)
|
|
->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],
|
|
]);
|
|
}
|
|
}
|
|
}
|