spatie / mailcoach-support

Questions and support for Mailcoach
https://mailcoach.app
31 stars 2 forks source link

Custom Mailable Errror - DOMDocument::loadHTML is being passed an empty string #204

Closed nkeena closed 4 years ago

nkeena commented 4 years ago

I'm running version v2.13.0. When using a custom mailable it's passing an empty string into line 34 of the PrepareEmailHtmlAction.php. Am I missing something that needs to be added to my mailable to inject the HTML?

This is the error I keep getting in my failed_jobs table only when using a custom mailable:

ErrorException: DOMDocument::loadHTML(): Empty string supplied as input in /.../vendor/spatie/laravel-mailcoach/src/Actions/Campaigns/PrepareEmailHtmlAction.php:34

This is my mailable. It's only extending CampaignMail as per the mailcoach documentation.

<?php

namespace App\Mail;

use App\EmailAttachment;
use Spatie\Mailcoach\Mails\CampaignMail;

class ClassMessage extends CampaignMail
{
    public function build()
    {
        $this
            ->from(
                $this->campaign->from_email ?? $this->campaign->emailList->default_from_email,
                $this->campaign->from_name ?? $this->campaign->emailList->default_from_name ?? null
            )
            ->subject($this->subject)
            ->view('mailcoach::mails.campaignHtml')
            ->text('mailcoach::mails.campaignText')
            ->addUnsubscribeHeaders()
            ->storeTransportMessageId();

        foreach (EmailAttachment::query()->where('campaign_id', $this->campaign->id)->get() as $attachment) {
            $this->attachFromStorage($attachment->source);
        }

        return $this;
    }
}
nkeena commented 4 years ago

Finally got it to work by updating my mailable and view to what's below.

From what I can tell, the content from the Mailable's view is extracted by Mailcoach and used instead of the $campaign->html. It also would not work when using a variable named $html or $campaignHtml. That's why I chose $emailContent.

The documentation on using a custom mailable is kinda vague, so hopefully this helps anyone with a similar issue.

<?php

namespace App\Mail;

use App\EmailAttachment;
use Spatie\Mailcoach\Mails\CampaignMail;

class ClassMessage extends CampaignMail
{
    public $emailContent;

    public function build()
    {
        $this->emailContent = $this->campaign->html;

        $this->view('emails.classes.message');

        foreach (EmailAttachment::query()->where('campaign_id', $this->campaign->id)->get() as $attachment) {
            $this->attachFromStorage($attachment->source);
        }

        return $this;
    }
}
// emails/classes/message.blade.php

{!! $emailContent !!}