0

I want to print the mail content from db table in laravel view blade.I have the table mail_template. I want to fetch the field body from that table.

body field is a long text and value is in PHP content

body field value 
<?php

if(($logged==Auth::User()->user_name && $finance_review!='')|| $status=='Rejected'){
?>
<span>Hello {{$requestor_name}},</span>
<p>{{$approver_name}} has {{$status}} your Asset
request. Please login to <a href='asd.com'>http://systems.test.com</a>
&rarr; “Asset” and see your status</p>
<?php }
else{ ?>
<span>Hello {{$approver_name}},</span>
<p>{{$requestor_name}} has raised request. 
Please login to <a href=''>test</a>
&rarr; “Asset” and see your status</p>
<?php } ?>

I want to send the mail with filled content from fetched body field. I have tried as

$mail_content=mail_template::where('config_name','=','approve')->first()->body;
echo $mail_content;

It just prints the content as text. I want to fill the value in variable

Zoe
  • 27,060
  • 21
  • 118
  • 148
user3386779
  • 6,883
  • 20
  • 66
  • 134

2 Answers2

0

You can try saving the email templates as

<p>{{name_replace}}</p>
<p>{{email_replace}}</p>

After fetching the template from the database try using the php str_replace function on the template with values. For example

$template = str_replace("{{name_replace}}", "Zoe", $template);

I think this will solve your problem. Let me know if that helped.

0

Mmm, this approach you have on loading the email template, so to say, from your database is awesome, the problem is that it is written in PHP.

Instead of storing the template in your database use views. Create, in your \resources\views a folder that will hold all your mail templates, in this case folder. And in your controller do this:

// Use Mail class, right on top:
use Illuminate\Support\Facades\Mail;

// Load the view to send
Mail::send('folder.view', $data, function ($mail) {
   $mail->from('from@email.com', 'Description');
   $mail->to(['to@email.com']);
   $mail->subject('SUBJECT');
});

Where:

  • folder.view is view.blade.php view located in folder folder.
  • $data is whatever you want to pass to the view.
  • The rest depends on your preference, read docs: laravel Mail

Hope this info will be useful and keep coding.

Nelson Eco
  • 31
  • 6