I have a Mailable which accepts two parameters which are used in the generated view when sent to the user. I want to emit an event once the Mailable has sent, however, a parameter should be passed to that event which is the string version of the view that is generated by the Mailable with the variables replaced:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Events\CommunicationEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
class DummyMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($title, $name)
{
$this->title = $title;
$this->name = $name;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$this->from('dummy@test.com')->subject('Howdy!')->view('mail.dummy-tpl')->with([
'title' => $this->title,
'name' => $this->name
]);
}
}
I have tried:
- Adding
event(new CommunicationEvent($this->render()));
before and after the$this->from([...])[...]
call in the build function - I have tried calling
(new DummyMail($this->title, $this->name))->render();
as suggested here. The event is not fired nor is the response of my AJAX request successful, bothstorage/logs/laravel.log
and apache2 logs are not helpful. - I have tried calling
$this->buildMarkdownView()
that should have a key calledhtml
which should give me the template, however that throws an error saying 'View [] not found.'.
So, how can I simply return the view that is generated for the email as a string so that it can be passed to the event that I plan on emitting?