0
votes

I'm using the Mail library in Laravel to send html email with custom data passed to a blade view.

The problem born when the mail has to render the html fetched from a row in the database which include a variable that i pass through the view.

This is my build function in my mailable class

public function build()
{
   return $this->from('[email protected]')
     ->view('view')
     ->with([
       'url'     => 'https://google.com',
       'text' => $this->parameters->text,
      ]);
}

Then in the blade view:

<div>
  {!! $text !!}
</div>

This is what the $text variable looks like:

<p>
  <span>This is my text for the mail</span>
  <a href="{{ $url }}">Click here to compile</a>
</p>

The link href shoul contain the url variable value instead of not passing the variable name itself

2

2 Answers

1
votes

A simple solution would be formating with php:

public function build()
{
    return $this->from('[email protected]')
     ->view('view')
     ->with([
       'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
      ]);
 }
0
votes

I did not try by myself but you could make an attempt with Blade::compileString(), i.e.:

public function build()
{
    return $this->from('[email protected]')
      ->view('view')
      ->with([
        'url'     => 'https://google.com',
        'text' => \Blade::compileString($this->parameters->text),
    ]);
}