|
<?php |
|
|
|
namespace App\Mail; |
|
|
|
use App\Models\Newsletter; |
|
use App\Models\Social; |
|
use App\Models\Subscriber; |
|
use Illuminate\Bus\Queueable; |
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
use Illuminate\Mail\Mailable; |
|
use Illuminate\Mail\Mailables\Attachment; |
|
use Illuminate\Mail\Mailables\Content; |
|
use Illuminate\Mail\Mailables\Envelope; |
|
use Illuminate\Notifications\Messages\MailMessage; |
|
use Illuminate\Queue\SerializesModels; |
|
|
|
class NewsletterMail extends Mailable implements ShouldQueue |
|
{ |
|
use Queueable, SerializesModels; |
|
|
|
/** |
|
* Create a new message instance. |
|
*/ |
|
public function __construct(protected Newsletter $newsletter, protected Subscriber $subscriber) |
|
{ |
|
$this->onQueue('email'); |
|
} |
|
|
|
/** |
|
* Get the message envelope. |
|
*/ |
|
public function envelope(): Envelope |
|
{ |
|
return new Envelope( |
|
subject: $this->newsletter?->subject ?? '๐ Fresh Newsletter', |
|
); |
|
} |
|
|
|
// public function build() |
|
// { |
|
// return $this->subject('My Subject') |
|
// ->html((new MailMessage) |
|
// ->lines('# The introduction to the notification.') |
|
// ->action('Notification Action', url('/')) |
|
// ->lines('Thank you for using our application!') |
|
// ->render() |
|
// ); |
|
// } |
|
|
|
/** |
|
* Get the message content definition. |
|
*/ |
|
public function content(): Content |
|
{ |
|
$page = app()->request->getSchemeAndHttpHost(); |
|
|
|
if (isset($this->newsletter->image)) { |
|
config(['mail.email.banner' => $page . "/image/" . $this->newsletter->image]); |
|
} |
|
|
|
foreach (Social::where('place', 'newsletter')->get() as $s) { |
|
config([ |
|
'mail.email.social.' . strtolower($s->name) => $s->url ?? $page, |
|
]); |
|
} |
|
|
|
$m = (new MailMessage) |
|
->subject($this->newsletter->subject ?? __('๐ Fresh Newsletter')) |
|
->greeting($this->replaceName($this->newsletter->title)) |
|
->lines($this->replaceLine($this->newsletter->line1)) |
|
->action($this->newsletter->button, $this->newsletter->url) |
|
->lines($this->replaceLine($this->newsletter->line2)) |
|
->theme('lara::theme.default') |
|
->markdown('lara::email.default', [ |
|
'code' => $this->newsletter->code ?? null, |
|
'date' => $this->newsletter->published_at->format('Y-m-d') ?? null, |
|
// Comment to hide |
|
// 'info' => 'Lorem ipsum dolor sit amet consectetur, adipisicing elit.', |
|
// 'products' => $products, |
|
// 'events' => $events, |
|
// 'news' => $news, |
|
])->render(); |
|
|
|
return new Content( |
|
htmlString: $m, |
|
); |
|
} |
|
|
|
/** |
|
* Get the attachments for the message. |
|
* |
|
* @return array<int, Attachment> |
|
*/ |
|
public function attachments(): array |
|
{ |
|
return []; |
|
} |
|
|
|
/** |
|
* Replace mustache attribute {name}. |
|
* |
|
* @param string $str |
|
* @return string |
|
*/ |
|
public function replaceName(string $str): string |
|
{ |
|
return str_ireplace('{name}', $this->subscriber->name, $str); |
|
} |
|
|
|
/** |
|
* Markdown multiple line format |
|
* |
|
* @param string $str |
|
* @return array |
|
*/ |
|
public function replaceLine(string $str): array |
|
{ |
|
return array_map(trim(...), preg_split('/\\r\\n|\\r|\\n/', $this->replaceName($str) ?? '')); |
|
} |
|
} |