Laravel-5 how to send an email from templates stored in db

How can I send email templates that are stored in a database using Laravel-5?

I am currently using the following code:

/* Queue thank you email for sending */

Mail::queue('emails.orderthankyou', $order->get()->first()->toArray(), function ($message) {
    $message->to('me@myemail.com')->subject('Thank you for your order');
});

However, this template is currently being pulled from the catalog Resources/views. I need to change this since my email templates are now stored in a database, for example:

Dear {{ $first_name }},<br><br>

Thank you for your order.

How can I pull these templates out of the database and make them sent using Mail :: Queue?

Thanks in advance.

+4
source share
1 answer

First you need to compile a template from a string if you need to use the blade syntax in a string template, for example: https://github.com/TerrePorter/StringBladeCompiler

- , .., :

// This will extract your array to variables like $first_name = 'John'
extract($order->get()->first()->toArray());
$generated = "Dear $first_name,<br><br>
            Thank you for your order.";

Mail::queue([], [], function ($message) use ($generated)
{
    $message->queue('me@myemail.com')
        ->subject('Thank you for your order')
        ->setBody($generated, 'text/html');
});

, Mail:: queue/send first paramater - , setBody(), html, text/html.

+8

All Articles