Laravel-5 如何从存储在数据库中的模板发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31338436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel-5 how to send email from templates stored in db
提问by V4n1ll4
How can I send email templates which are stored in a database using Laravel-5?
如何使用 Laravel-5 发送存储在数据库中的电子邮件模板?
I am using the following code currently:
我目前正在使用以下代码:
/* Queue thank you email for sending */
Mail::queue('emails.orderthankyou', $order->get()->first()->toArray(), function ($message) {
$message->to('[email protected]')->subject('Thank you for your order');
});
However, currently the above template is pulled from the Resources/views
directory. I need to change this as my email templates are now stored in a database, like so:
但是,目前上述模板是从Resources/views
目录中提取的。我需要更改此设置,因为我的电子邮件模板现在存储在数据库中,如下所示:
Dear {{ $first_name }},<br><br>
Thank you for your order.
How can I pull these templates from the database and render them to be sent using Mail::Queue?
如何从数据库中提取这些模板并呈现它们以使用 Mail::Queue 发送?
Thanks in advance.
提前致谢。
回答by rozklad
First you need to compile your template from string, if you need to use blade syntax on the string template see for example: https://github.com/TerrePorter/StringBladeCompiler
首先,您需要从字符串编译模板,如果您需要在字符串模板上使用刀片语法,请参见例如:https: //github.com/TerrePorter/StringBladeCompiler
If you don't need any difficult operations, just replacing first name etc., i would go with:
如果您不需要任何困难的操作,只需替换名字等,我会选择:
// 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.";
Now in the message
现在在消息中
Mail::queue([], [], function ($message) use ($generated)
{
$message->queue('[email protected]')
->subject('Thank you for your order')
->setBody($generated, 'text/html');
});
Notice, that Mail::queue/send first paramater is empty array, there is also setBody() method, where you first pass the generated html and then just tell it the format as text/html.
请注意,Mail::queue/send 第一个参数是空数组,还有 setBody() 方法,您首先将生成的 html 传递给它,然后告诉它格式为 text/html。