Код: Выделить всё
class MailBatch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:mail-batch';
/**
* The console command description.
*
* @var string
*/
protected $description = 'send mail in pending mode';
/**
* Execute the console command.
*/
public function handle()
{
$mailToSend = Mail::where('status', 'pending')->take('50')->orderBy('created_at')->get();
$emailLists = [];
foreach ($mailToSend as $mail) {
$emailBody = json_decode($mail->content);
// $sendEmail = new GraphApiController();
$statusSend = 'send';
$recipients = $emailBody->message->toRecipients;
$recipientsArray = [];
$ccArray = [];
$bccArray = [];
foreach ($recipients as $recipient) {
$recipientsArray[] = $recipient->emailAddress->address;
if (isset($recipient->emailAddress->cc)) {
$ccArray[] = $recipient->emailAddress->cc;
}
if(isset($recipient->emailAddress->bcc)) {
$bccArray[] = $recipient->emailAddress->bcc;
}
}
$replyTo = null;
if(isset($emailBody->message->replyTo)) {
$replyTo = $emailBody->message->replyTo;
}
try {
$emailLists[] = Mailer::send(new NotifyUsers(
$recipientsArray,
$emailBody->message->subject,
$emailBody->message->body->content,
$ccArray,
$bccArray,
$replyTo
));
} catch (\Exception $e) {
$statusSend = 'error';
$emailLists[] = $e->getMessage();
}
$mail->status = $statusSend;
$mail->send_date = date("Y-m-d H:i:s", time());
$mail->save();
}
}
В моем классе NotifyUsers я использую шаблон NotifyUsers, например:
Код: Выделить всё
[AllowDynamicProperties] class NotifyUsers extends Mailable
{
use Queueable, SerializesModels;
public array $receivers;
public array $ccs;
public array $bccs;
public string $messageSubject;
public string $emailBody;
/**
* Create a new message instance.
*/
public function __construct($receivers, $messageSubject, $emailBody, $ccs = [], $bccs = [], $replyTo = null)
{
$this->receivers = $receivers;
$this->ccs = $ccs;
$this->bccs = $bccs;
$this->messageSubject = $messageSubject;
$this->emailBody = $emailBody;
$this->replyToUser = $replyTo;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
from: new Address(Config::get('constants.support_email'), Config::get('constants.support_name')),
to: $this->receivers,
cc: $this->ccs,
bcc: $this->bccs,
replyTo: $this->replyToUser,
subject: $this->messageSubject,
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'email.notifyUsers',
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
$files= File::allFiles(public_path('logo'));
$latestFile = collect($files)->sortByDesc(function ($file) {
return $file->getMTime();
})->first();
$totalPath = public_path('logo').'/'.$latestFile->getFileName();
return [
Attachment::fromPath($totalPath)
];
}
Вот код шаблона notifyusers.
Код: Выделить всё
@extends('email.layout')
@section("content")
{!! $emailBody !!}
@endsection
Я использовал такие команды использования, как php optimze:clear и php view:clear, но мой макет, похоже, никогда не меняется.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -laravel-s