We're using Laravel 9, and we store email template in our database, not in blade files.
When sending an email, we would like to use the embedData()
function on the Illuminate/Mail/Message
class, but how do we get this message object?
We have a class that extends Illuminate\Mail\Mailable
, called App\Mail\Message
.
We send mail like this:
$message = new App\Mail\Message($subject, $fullBody, $fromAddress, $replyTo, $attachments);
$message->to($address)->from($fromAddress, $fromName);
Mail::send($message);
But we want the Illuminate/Mail/Message message object, because we want the embed function. How can we get this object?
CodePudding user response:
Use mail raw and if the template is in blade syntax, you can parse it using the Blade
class.
$template = Template::find(11); // get the template from db
$templateBody = $template->body; // or whatever the template body attribute is
$templateRender = Blade::render($templateBody); // render body via blade engine
Mail::raw($templateRender, function (Message $message) {
$message->subject('Hello, world!');
$message->to('[email protected]');
// add other stuff to the message here...
});
CodePudding user response:
In our app/Mail/Message::build() function, we ran this to get the $message object:
return $this->withSymfonyMessage(function (Email $message) {
// here is the $message object
});