mivita/app/Mail/MailCredit.php
2026-02-20 17:55:06 +01:00

80 lines
2.3 KiB
PHP

<?php
namespace App\Mail;
use App\Models\UserCredit;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Storage;
class MailCredit extends Mailable
{
use Queueable, SerializesModels;
protected $user_credit;
public $subject;
public function __construct(UserCredit $user_credit)
{
$this->user_credit = $user_credit;
$this->subject = __('email.credit_title');
}
public function build()
{
$title = 'Hallo ';
$copy1line = __('email.credit_copy1line');
if (isset($this->user_credit->user->account)) {
$title .= $this->user_credit->user->account->m_first_name.',';
}
$disk = $this->user_credit->disk;
// 1. Deutsches Original immer anhängen
$pathDe = $this->user_credit->getDownloadPath();
$filenameDe = $this->user_credit->filename;
if (! Storage::disk($disk)->exists($pathDe)) {
return;
}
$fileDe = Storage::disk($disk)->path($pathDe);
$fileDe = str_replace('//', '/', $fileDe);
$mimeDe = Storage::disk($disk)->mimeType($pathDe);
$mail = $this->view('emails.blank')->with([
'title' => $title,
'copy1line' => $copy1line,
])->attach($fileDe, [
'as' => $filenameDe,
'mime' => $mimeDe,
]);
// 2. Lokalisierte Version zusätzlich anhängen (wenn vorhanden und != DE)
$locale = $this->user_credit->user
? $this->user_credit->user->getLocale()
: 'de';
if ($locale && $locale !== 'de') {
$pathLocale = $this->user_credit->getDownloadPathLocale($locale);
$filenameLocale = $this->user_credit->getFilenameLocale($locale);
// Nur anhängen wenn lokalisierte Version existiert und nicht gleich dem Original ist
if (Storage::disk($disk)->exists($pathLocale) && $pathLocale !== $pathDe) {
$fileLocale = Storage::disk($disk)->path($pathLocale);
$fileLocale = str_replace('//', '/', $fileLocale);
$mimeLocale = Storage::disk($disk)->mimeType($pathLocale);
$mail->attach($fileLocale, [
'as' => $filenameLocale,
'mime' => $mimeLocale,
]);
}
}
return $mail;
}
}