craigpaul/laravel-postmark

Too much data being rendered in `template.blade.php` view.

Closed this issue · 2 comments

When I attempted to use this library to send an email using a template, I noticed the email body was always empty. After tracing around the code, I found that the output from the postmark::template view (https://github.com/craigpaul/laravel-postmark/blob/5481b9ad178fddc919fa717c9ae25eb7cc6b72ce/resources/views/template.blade.php) was always empty and I never received any feedback in output (logs, console etc). Further digging and I discovered that the data was getting into the render function, however, the output was always empty.

After some more poking around I ended up in Illuminate\View\View::gatherData(). There is a line that adds "shared" data of __env and app. Removing these allows the template to render. It seems there is just too much data in these array fields.

To solve the problem, I paired down the data by changing the template from:

@json($__data)

to:

{
    "id": @json($id),
    "alias": @json($alias),
    "model": @json($model)
}

I am unclear why there is so much data, but, at least this solves the problem. For reference, I am extending PostmarkTemplateMailable and overloading the __construct() and build() methods which is being called via

Mail::to("email@address.com")
    ->send(new OrderReceived($order))
;

Here is my Mailable class for reference

<?php

namespace App\Mail;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Coconuts\Mail\PostmarkTemplateMailable;

class OrderReceived extends PostmarkTemplateMailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * The order instance.
     *
     * @var \App\Models\Order
     */
    protected $order;

    /**
     * Create a new message instance.
     *
     * @param  \App\Models\Order  $order
     * @return void
     */
    public function __construct(Order $order)
    {
        parent::__construct();
        $this->order = $order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            ->afterCommit()
            ->id(12345)
            ->include([
                "product_url" => "product_url_Value",
                "product_name" => "product_name_Value",
                "name" => "name_Value",
                "action_url" => "action_url_Value",
                "support_email" => "support_email_Value",
                "company_name" => "company_name_Value",
                "company_address" => "company_address_Value",
            ])
        ;
    }
}

@pointybeard thanks for bringing this to my attention. I’ll get some fixes out for the latest 2.x and 3.x branches today 👍

This has now been patched and is available in v2.11.2 and v3.0.0-RC2.

2feeeb9
ab8e8d0