use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
class ProcessEmailBatch implements ShouldQueue
$this->batch->file->publish();
}
+ $templateVariables = $this->processVariablesForMailgun();
+
foreach($chunks as $chunk) {
- $variables = json_encode($chunk->mapWithKeys(function (User $user) {
+ $variables = json_encode($chunk->mapWithKeys(function (User $user) use ($templateVariables){
$fileVar = $this->batch->file === null ?
[] :
['file_url' => $this->batch->file->getMailableUrl($user)];
+ $filledVariables = array_map(fn($template) => $user->templateAttribute($template), $templateVariables);
+
return [
$user->email => array_merge([
'id' => $user->id,
'name' => $user->name ?? '',
- ], $fileVar)
+ ], $fileVar, $filledVariables)
];
}), JSON_THROW_ON_ERROR);
$this->batch->update(['sent_to' => $this->users->pluck('email')]);
}
+
+ public function processVariablesForMailgun()
+ {
+ if($this->html === null) {
+ return [];
+ }
+ $variables = [];
+
+ $this->html = preg_replace_callback('/\{\{[^(% )]*\}\}/', function($matches) use(&$variables){
+
+ $match = Str::before(Str::after($matches[0], '{{'), '}}');
+ $variables[] = $match ;
+ return "%recipient.$match%";
+ }, $this->html);
+
+ return array_unique($variables);
+ }
}