mistery_girl mistery_girl - 6 months ago 21
PHP Question

How to attach a Laravel blade view in mail

I'm trying to attach a blade view when send mail. I'm using Laravel 4. I tried with this:

Mail::send(
"emails.clientEmail",
$data,
function($message) use ($data,$template,$subject) {
$message->to($data['email'], $data['name'])
->from($template['from'],$template['from_name'])
->subject($subject)
->attach(app_path().'/views/email.blade.php');
}
);


But my view
email.blade.php
is in blade template and it doesn't display the html but code.

How should I send it and display as html?

Answer

As you metioned you can use attachData and add rendered data like:

$renderedData = view('email')->render();

Mail::send(
        "emails.clientEail",
        $data,
        function($message) use ($data,$template,$subject) {
            $message->to($data['email'], $data['name'])
                ->from($template['from'],$template['from_name'])
                ->subject($subject)
                ->attachData($renderedData, 'name_of_attachment');
        }
    );