Home > Mobile >  How to render a mail with HTML variable?
How to render a mail with HTML variable?

Time:09-06

I am trying to send email by html code.

I would like to include image and data on the blade template, but can't render the view.

current code:

MailTemplate.php

<?php

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class MailTemplate extends Mailable
{
    use Queueable, SerializesModels;

    public $subject = null;
    public $template;
    public $data;
    /**
     * Create a new message instance.
     *
    * @return void
     */
    public function __construct($subject, $template, $data)
    {
        //
        $this->subject = $subject;
        $this->template = $template;
        $this->data    = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->template = '<div><img src="{{ $message->embed(public_path('/assets/images/logo.png')) }}" ></div>';
        return $this->subject( $this->subject)->view('emails.EmailTemplate');
    }
}

EmailTemplate.blade.php

<html>
<body>
{!! $template !!}
</body>
</html>

result

current:

<html>
<body>
<div><img src="{{ $message->embed(public_path('/assets/images/logo.png')) }}" ></div>
</body>
</html>

//==============================================

**I want:**

<html>
<body>
<div><img src="embed swiftmailfile~~~~~~" ></div>
</body>
</html>

So, how can I return the view using HTML so that it can be passed to the variable?

CodePudding user response:

You can try this:
Just pass src to view

public function build()
{
    $src = $message->embed(public_path('/assets/images/logo.png'));
    return $this->subject( $this->subject)->view('emails.EmailTemplate', ['src' => $src]);
}

And then , render to view

<html>
<body>
<div><img src="{{ $src }}" ></div>
</body>
</html>

CodePudding user response:

You are trying to render blade inside the mailable.

Instead, create the string using concatenation

$this->template = '<div><img src="' . $message->embed(public_path('/assets/images/logo.png')) . '" ></div>';

  • Related