Giter Site home page Giter Site logo

spatie / laravel-database-mail-templates Goto Github PK

View Code? Open in Web Editor NEW
421.0 421.0 43.0 103 KB

Render Laravel mailables based on a mail template stored in the database

Home Page: https://spatie.be/open-source

License: MIT License

PHP 100.00%
customize laravel mailable php template

laravel-database-mail-templates's People

Contributors

adrianmrn avatar alexbowers avatar alexdpunkt avatar alexvanderbist avatar apfelwurm avatar corwindev avatar dragonfly4 avatar freekmurze avatar fulopattila122 avatar ilkermutlu avatar laravel-shift avatar lewblu avatar lloy0076 avatar michaelnabil230 avatar oguzcandemircan avatar patinthehat avatar simonedelmann avatar tof06 avatar yaquawa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-database-mail-templates's Issues

[Question] How can I get the rendered subject of a mail

First of all, thanks for your work and your great packages! I'm using your package alongside with Laravel Notifications and I store the result of the render method of the mailable in the database, so that I've got the mail also as database notification. However I also want to get the rendered subject of the mail, but I don't know how. Can you please give me a hint?

2.0 is incompatible with eloquent due to interface

Probably the same as #9. But Eloquent is throwing an exception because it expects any property defined as a method to be a Relation.

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:411
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:397
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:327
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1536
vendor/laravel/framework/src/Illuminate/Support/Arr.php:148

Even if I force subjects() to return a string, Eloquent throws a LogicException() because it requires a Relation object. App\MailTemplate::subject must return a relationship instance.

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:397
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:327
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1536
vendor/laravel/framework/src/Illuminate/Support/Arr.php:148
vendor/laravel/framework/src/Illuminate/Support/helpers.php:475

Using blade as a layout

Hi,

I'm trying to use a blade file as a template, but I'm not sure how to integrate {{{ body }}} inside the blade layout. Whatever I do I keep getting an error message Use of undefined constant body - assumed 'body' and if I try to replace it with a $body I get an unknown variable error.

This is my code:
public function getLayout(): string { return view('emails.base')->render(); }
Edit: I was able to find a work-around by putting some other magic text and then replacing it after the render, but when trying to use components in blade (specificly @component('mail::message')) I'm getting the error - No hint path defined for [mail]

Thank you.

How to use laravel-translatable package

I have configured laravel-translatable package but it returns the mail in json.

image

I tried doing like follows.

    app()->setLocale('nl');
    Mail::to($user->email)->send(new \App\Mail\WelcomeMail($user));

Having issues installing the package.

Hey,

When running the composer it gives me this error :

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for spatie/laravel-database-mail-templates ^3.1 -> satisfiable by spatie/laravel-database-mail-templates[3.1.0, 3.1.1].
    - Can only install one of: mustache/mustache[v2.12.0, v2.7.0].
    - Can only install one of: mustache/mustache[v2.7.0, v2.12.0].
    - Can only install one of: mustache/mustache[v2.12.0, v2.7.0].
    - Conclusion: install mustache/mustache v2.12.0
    - Installation request for mustache/mustache (locked at v2.7.0) -> satisfiable by mustache/mustache[v2.7.0].


Installation failed, reverting ./composer.json to its original content.

I have looked at the mustache package, and unless I'm missing something (which is possible as I'm a noob) there is no version v2.12

Not really sure how to proceed, any ideas?

Template wont use HTML tags

So when I use the {{ body }} tag in my main.html file which is my template it just shows plain text and does not support HTML tags. Example: <b>Sample</b> instead of Sample

readme needs updates

I think body is obsolete and needs to be replaced by html_template and text_template.

MailTemplate::create([
    'mailable' => WelcomeMail::class,
    'subject' => 'Welcome',
    'html_template' => '<p>Hello, {{ name }}.</p>',
    'text_template' => 'Hello, {{ name }}.',
]);

Also the $templateModel property in the TemplateMailable class needs to be replaced by $templateModelClass.

class NewMeetupPlannedMail extends TemplateMailable
{
    // use our custom mail template model
    protected static $templateModelClass = MeetupMailTemplate::class;

MailTemplate doesn't work in a nova resource properly

When using MailTemplate as the basis model of a Nova Resource, the create flow breaks due the the subject and template functions.

Nova doesn't work properly when an attribute and a model have the same name.

Could you change the subject and template method names to getSubject and getTemplate?

html print instead of render

eodHtmlTemplate print html instead of rendering
EodMail.php
`<?php

namespace App\Mail;

use App\Models\Project;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Spatie\MailTemplates\TemplateMailable;

class EodMail extends TemplateMailable
{
use Queueable, SerializesModels;

/** @var string */
public $name;

/** @var string */
public $eodHtmlTemplate;

public function __construct(Project $project, $eodHtmlTemplate)
{
    $this->name = $project->eodConfiguration->client->name;
    $this->eodHtmlTemplate = $eodHtmlTemplate;
}

public function getHtmlLayout(): string
{
    /**
     * In your application you might want to fetch the layout from an external file or Blade view.
     *
     * External file: `return file_get_contents(storage_path('mail-layouts/main.html'));`
     *
     * Blade view: `return view('mailLayouts.main', $data)->render();`
     */

    return view('mail-layouts.eod')->render();

// return '

Site name!{{{ body }}}Copyright 2018';
}

}
eod.blade.phpHi


@{{{body}}}

Copyright 2022

`

Send Email
Mail::to($project->eodConfiguration->client->email)->send(new EodMail($project, $request->email));

image

if I try to use {!! eodHtmlTemplate !!}

image

Override body content

I'm working on an application that allows you to override the content of an e-mail before it is send out to a recipient. We use the following base class for all of our template classes:

class BaseMail extends TemplateMailable
{
    public $signature;
    public $body;
    protected static $templateModel = BaseMailTemplate::class;
...

The idea being that we can override $body in __construct() and override whatever gets generated through Mustache further on. We don't need to do any HTML parsing, we will be passing valid HTML through Quill (a WYSIWYG-editor).

I am a little lost though. I think that I need to extend TemplateMailableRenderer somehow and make sure we're changing renderHtmlLayout(). I should then pass this renderer somehow to my custom TemplateMailable?

Not sure how to move on with this and I hope this is something others who are using this have been struggling with too. Could someone please point me in the right direction?

package works it with 'if' abnd 'if not' notattion?

Hi.

I'm working with package and I tried to use mustache logic with if and if not but it doesn't work for me.

Example

Maillable WelcomeEmail

public function __construct(Campaign $campaign, Subscriber $subscriber)
{
  $this->name =  $subscriber->name; \\ Some subcribers has "null" or "empty" this value
  $this->campaign = $campaign->campaign; 
}

Template

has a

..... Hello {{ name }}, ...

Try template with mustache logic if and if not

{{#name}}
    Hello {{ name }},
{{/name}}
{{^name}}
    Hello,
{{/name}}

But this renders both things even though the var is null, empty or contains data

Any ideas?

buildView() overrides

I've been using standard Laravel mailables in my project and had a couple of tests asserting some string in the mailable HTML like

        $mailable = new UserInvitation($userInvitation);
        $mailable->assertSeeInHtml($url);

After migrating my mailables to this package, this assertion started erroring with:

 Illuminate\Support\HtmlString::__toString(): Return value must be of type string, null returned

  at vendor/laravel/framework/src/Illuminate/Support/HtmlString.php:64
     60▕      * @return string
     61▕      */
     62▕     public function __toString()
     63▕     {
  ➜  64▕         return $this->toHtml();
     65▕     }
     66▕ }
     67▕ 

After some digging, I see that the original buildView() method is as follows:

        if (isset($this->html)) {
            return array_filter([
                'html' => new HtmlString($this->html),
                'text' => $this->textView ?? null,
            ]);
        }

And the corresponding part of the override in the package:

        $html = $renderer->renderHtmlLayout($viewData);
        $text = $renderer->renderTextLayout($viewData);

        return array_filter([
            'html' => new HtmlString($html),
            'text' => new HtmlString($text) ?? null,
        ]);

I saw that the renderTextLayout() method is already returning consistently with the original method signature:

        if (! $this->mailTemplate->getTextTemplate()) {
            return $this->textView ?? null;
        }

So, I tried replacing 'text' => new HtmlString($text) ?? null with 'text' => $text and got the tests passing.

Considering renderTextLayout() will always return null when there is no text template (which is optional), any assertion on the html template will generate this same error.

I am not yet sure about further side effects of the change I made, therefore not submitting a PR.

I'd appreciate any insight into this.

PHP8

When are you planning to add php 8 support?

Regards,
Gregor

Minimum Laravel version

Hello,

I see the package has listed Laravel 5.7 as one of the requirements? Does it use any 5.7 specific feature? If not, can this be installed on previous Laravel versions like 5.5?

Thanks
Mohin

Print objects

Thank You for a great package.
We print strings like {{ your_string_variable }} But How do we print object properties inside your mailTemplate ?
Also can we print loops (similar to blade @foreach) ?

Htmt layoutviews

Hi there,
First of all nice package!
I am getting some errors when i wanted to use a hmtl layout.
Use of undefined constant body - assumed 'body
i have defined the body as the docs explained and just use a file_get_contents instead of returning a view.
How can i get it working??

defining mail settings from database

Hello,

Thanks for the package. I want to know If there is a good way to change mail settings in the runtime.

I want to get the right settings from database and set it just before sending emails. I am using queue and the settings seems mixing.

I'm stuck and looking forward to your reply.

How to use the package?

I don't understand if I'm that stupid or the usage guide is incomplete but everyone else manages to figure out how to use it. In the example seeder you're using

MailTemplate::create([
            'mailable' => \App\Mails\WelcomeMail::class,
            'subject' => 'Welcome, {{ name }}',
            'html_template' => '<h1>Hello, {{ name }}!</h1>',
            'text_template' => 'Hello, {{ name }}!',
        ]);

How would my app know about MailTemplate if I don't import it? I've tried it and I do get an error Class 'App\\MailTemplate' not found I've tried adding use Spatie\MailTemplates\MailTemplate; at the top but that also didn't work - got Class 'Spatie\MailTemplates\MailTemplate' not found

How do you use the MailTemplate class?

Postfix

Can I integrate this package with the Postfix server?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.