private bool $attachFile = false;
+ private array $templateVariables;
/**
$this->users = $usersBuilder->get();
- $this->replaceVariables();
+
}
/**
])));
}
- private function replaceVariables()
- {
- if($this->html === null) {
- return;
- }
-
- $replace = [
- '{{name}}' => '%recipient.name%',
- ];
-
- $this->html = str_replace(array_keys($replace), array_values($replace), $this->html);
- }
-
/**
* Execute the job.
*
$this->batch->file->publish();
}
- $templateVariables = $this->processVariablesForMailgun();
+ $this->html = $this->batch->render();
+
+ $this->templateVariables = $this->processVariablesForMailgun();
foreach($chunks as $chunk) {
- $variables = json_encode($chunk->mapWithKeys(function (User $user) use ($templateVariables){
+ $variables = json_encode($chunk->mapWithKeys(function (User $user) {
$fileVar = $this->batch->file === null ?
[] :
['file_url' => $this->batch->file->getMailableUrl($user)];
- $filledVariables = array_map(fn($template) => $user->templateAttribute($template), $templateVariables);
+ $filledVariables = array_map(fn($template) => $user->templateAttribute($template), $this->templateVariables);
return [
- $view = $this->batch->render();
-
$params = [
'from' => config('mail.mailgun.from_name').' <'.config('mail.from.address').'>',
'to' => $chunk->pluck('email')->toArray(),
'subject' => $this->batch->subject,
'recipient-variables' => $variables,
- 'html' => $view,
+ 'html' => $this->html,
'o:tag' => [$this->batch->getTag(), config('app.env').'_batch_'],
'o:testmode' => config('mail.mailgun.test_mode', 'yes'),
'h:Reply-To' => 'olivier.robichon@prescription-quotidien.com',
$this->html = preg_replace_callback('/\{\{[^(% )]*\}\}/', function($matches) use(&$variables){
$match = Str::before(Str::after($matches[0], '{{'), '}}');
- $variables[] = $match ;
+ $variables[$match] = $match ;
return "%recipient.$match%";
}, $this->html);
- return array_unique($variables);
+ return $variables;
}
}