92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Mail;
|
|
|
|
use App\Models\Mailing;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Mail\Mailable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class MailingSummaryMail extends Mailable
|
|
{
|
|
use Queueable, SerializesModels;
|
|
|
|
public Mailing $mailing;
|
|
|
|
public function __construct(Mailing $mailing)
|
|
{
|
|
$this->mailing = $mailing;
|
|
}
|
|
|
|
public function build(): self
|
|
{
|
|
$mailing = $this->mailing;
|
|
|
|
$subject = '[' . config('app.name') . '] Mailing completed: ' . $mailing->title;
|
|
|
|
$durationStr = '';
|
|
if ($mailing->sending_started_at && $mailing->sent_at) {
|
|
$seconds = (int) $mailing->sending_started_at->diffInSeconds($mailing->sent_at, true);
|
|
$h = intdiv($seconds, 3600);
|
|
$m = intdiv($seconds % 3600, 60);
|
|
$s = $seconds % 60;
|
|
$durationStr = $h > 0
|
|
? sprintf('%dh %dm %ds', $h, $m, $s)
|
|
: ($m > 0 ? sprintf('%dm %ds', $m, $s) : sprintf('%ds', $s));
|
|
}
|
|
|
|
$successRate = ($mailing->recipients_count > 0)
|
|
? round($mailing->sent_count / $mailing->recipients_count * 100, 1)
|
|
: 0;
|
|
|
|
$bounceStats = $mailing->getBounceStats();
|
|
$suppressedCount = $mailing->getSuppressedRecipientsCount();
|
|
|
|
$lines = [];
|
|
$lines[] = 'Mailing Report: ' . $mailing->title;
|
|
$lines[] = str_repeat('-', 40);
|
|
$lines[] = '';
|
|
$lines[] = 'Mailing ID: ' . $mailing->id;
|
|
$lines[] = 'Status: ' . ucfirst($mailing->status);
|
|
if ($mailing->sending_started_at) {
|
|
$lines[] = 'Started sending: ' . $mailing->sending_started_at->format('d M Y H:i');
|
|
}
|
|
if ($mailing->sent_at) {
|
|
$lines[] = 'Completed at: ' . $mailing->sent_at->format('d M Y H:i');
|
|
}
|
|
if ($durationStr) {
|
|
$lines[] = 'Duration: ' . $durationStr;
|
|
}
|
|
$lines[] = '';
|
|
$lines[] = 'Total sent to: ' . number_format($mailing->recipients_count);
|
|
$lines[] = 'Successfully sent: ' . number_format($mailing->sent_count);
|
|
$lines[] = 'Failed (delivery): ' . number_format($mailing->failed_count);
|
|
if ($mailing->skipped_count > 0) {
|
|
$lines[] = 'Skipped (bounced): ' . number_format($mailing->skipped_count);
|
|
}
|
|
if ($suppressedCount > 0) {
|
|
$lines[] = 'Skipped (hard bounce): ' . number_format($suppressedCount);
|
|
}
|
|
if ($mailing->aborted_count > 0) {
|
|
$lines[] = 'Not sent (aborted): ' . number_format($mailing->aborted_count);
|
|
}
|
|
if ($bounceStats['hard'] > 0) {
|
|
$lines[] = 'Bounced (hard): ' . number_format($bounceStats['hard']);
|
|
}
|
|
if ($bounceStats['soft'] > 0) {
|
|
$lines[] = 'Bounced (soft): ' . number_format($bounceStats['soft']);
|
|
}
|
|
if ($bounceStats['complaint'] > 0) {
|
|
$lines[] = 'Complaints: ' . number_format($bounceStats['complaint']);
|
|
}
|
|
$lines[] = 'Success rate: ' . $successRate . '%';
|
|
$lines[] = '';
|
|
$lines[] = str_repeat('-', 40);
|
|
$lines[] = config('app.name');
|
|
|
|
return $this->subject($subject)->text('emails.mailing-summary-plain', [
|
|
'lines' => $lines,
|
|
]);
|
|
}
|
|
}
|