Mailble using twig templates (solved)
thijndehaas opened this issue · 1 comments
Laravel version: 5.8
In the last 2 hours I have tried to use twig as a template in a Mailable class. Unfortunately without success.
In the public build function I have tried the following return values:
return View::make('mails.example_view');
return View::make('mails.example_view')->render();
return ['text' => View::make('mails.example_view')];
It seems like the mailable really wants a Blade view as parameter. Does someone have experience in using Twig views for the Mailable Build function?
I just looked through the Mailable source code and used the following code to use the Mailable classs with Twig. Instead of returning the view I just set the Mailable html without a return value.
The solution to use Twig in a Mailable:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use View;
class ExampleMailable extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
//Do not return the View here!
$this->subject('Subject');
$this->html(View::make('mails.example')->render());
}
}