Select email template and copy the content in a form field
I am currently working on implementing a mailing function for my application, and so far, everything is functioning correctly. I have created a form that includes a dropdown menu where I can select the email template I want to use. After choosing the template, I proceed to send the email.
Additionally, there is a text field called mailMessage where, in the future, I plan to input some text before sending.
My objective is to automatically populate the mailMessage field with the content of the selected template when I choose a template from the dropdown. This way, I can either send the template as is or customize it by adding some additional text. The templates themselves utilize placeholders, for example:
Do you have any examples or recommendations for achieving this functionality?
4 Replies
the code in the resource
Similar to creating a slug from a Post name, you could have the Select call afterStateUpdated to call a (helper/class) function which uses the Select's $state (you pass it to your helper function) to determine which option was chosen, and then generate and return the necessary Textarea content, which Set would then push to the Textarea field.
https://filamentphp.com/docs/3.x/forms/advanced#generating-a-slug-from-a-title
i've found a solution but discord limits the number of char
Forms\Components\Select::make('emailTemplate')
->helperText('Select the email to send or write your own message')
->options([
'SendQuote' => 'Send quote',
'ReadyForDelivery' => 'Ready for delivery',
])
->live()
->afterStateUpdated(function (Set $set, $state) {
$bladeFileName = Str::lower($state);;
$bladeFilePath = resource_path("views/emails/repairs/{$bladeFileName}.blade.php");
if (file_exists($bladeFilePath)) {
// Leggi il contenuto del file Blade
$bladeContent = File::get($bladeFilePath);
// Esegui l'operazione desiderata sul contenuto del file
$set('emailMessage', $bladeContent);
} else {
// Il file Blade non esiste, gestisci di conseguenza
$set('emailMessage', 'Il file Blade non esiste: ' . $bladeFilePath);
}
})
->native(false)
->required()
->columnSpanFull(),
Great! Thanks for posting back with your solution.