I'm having trouble rendering my Mailable on a view. I can clearly see (in telescope) that the my post request has a session with the array key "preview" but nothing shows up.
When debugging the value of my response, I find that my view-data has it's preview key set to the correct stringified render of the view between 2 more quotes ("")
For Example:
[
"preview": """<html></html>"""
]
Here is my code:
View: Where I check whether the $preview has been defined in the $res variable from the controller.
</div>
@isset($preview)
<hr>
<div class="form-group row">
<div class="col">
<h4>Preview:</h4>
<div class="form__preview">
</div>
</div>
</div>
@endisset
</form>
Controller: In my Action I execute my sendMail Action
public function mail(PotentialClientRequest $request)
{
$sendMail = new PotentialClientAction();
$res = $sendMail->execute($request);
return back()->with($res);
}
Action: Here we return the View or send a Mail (and return a success message).
<?php
namespace App\Http\Actions;
use App\Mail\PotentialClientMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Http\FormRequest;
class PotentialClientAction
{
public function execute(FormRequest $request): array
{
$contact = $this->getData($request);
$mail = new PotentialClientMail($contact);
$response = [];
switch ($request->input('action')) {
case 'mail':
$response = $this->mail($mail);
break;
case 'preview':
$response = $this->preview($mail);
break;
default:
break;
}
return $response;
}
private function mail(PotentialClientMail $mail): array
{
Mail::to(env('MAIL_FROM_ADDRESS'))->queue($mail);
return [
'success' => 'Mail successfully sent!'
];
}
private function preview(PotentialClientMail $mail): array
{
return [
'preview' => $mail->render()
];
}
private function getData(FormRequest $request): array
{
$data = $request->validated();
$contact = [
'email' => $data['email'] ?? '-',
'email_cc' => $data['email_cc'] ?? '-',
'subject' => $data['subject'] ?? '-',
'body' => $data['body'] ?? '-'
];
return $contact;
}
}
Mailable:
public function build()
{
return $this
->subject($this->data['subject'])
->markdown('backend.mails.client')
->with([
'email' => $this->data['email'],
'email_cc' => $this->data['email_cc'],
'subject' => $this->data['subject'],
'body' => $this->data['body']
]);
}
Mail view:
@component('mail::message')
<p>Hi Foo,</p>
@component('mail::panel')
<p>
<strong>My email is:</strong> <a href="mailto:"></a><br>
<strong>My email_cc is:</strong> <a href="mailto:"></a><br>
<strong>My subject is:</strong> <br>
<strong>It's about:</strong>
</p>
<p></p>
@endcomponent
@component('mail::button', ['url' => 'mailto:' . $email])
Reply
@endcomponent
Company name,<br>
@endcomponent
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire