spatie / laravel-database-mail-templates Goto Github PK
View Code? Open in Web Editor NEWRender Laravel mailables based on a mail template stored in the database
Home Page: https://spatie.be/open-source
License: MIT License
Render Laravel mailables based on a mail template stored in the database
Home Page: https://spatie.be/open-source
License: MIT License
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?
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
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.
Is it possible to add Laravel 6 Support to this package ?
Thank
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?
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
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;
Hi
Is there a way to dynamically set the template variables from the model? for example using Magic methods __set() and __get() allow me to retrieve properties from a properties array?
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?
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.php
Hi
@{{{body}}}
Copyright 2022
`Send Email
Mail::to($project->eodConfiguration->client->email)->send(new EodMail($project, $request->email));
if I try to use {!! eodHtmlTemplate !!}
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?
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.
public function __construct(Campaign $campaign, Subscriber $subscriber)
{
$this->name = $subscriber->name; \\ Some subcribers has "null" or "empty" this value
$this->campaign = $campaign->campaign;
}
has a
..... Hello {{ name }}, ...
{{#name}}
Hello {{ name }},
{{/name}}
{{^name}}
Hello,
{{/name}}
But this renders both things even though the var is null, empty or contains data
Any ideas?
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.
When are you planning to add php 8 support?
Regards,
Gregor
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
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) ?
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??
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.
Hi.
Can you confirm where and how I set the template variables when extending the model?
Mustache.php v2.14.1 was released today. It contains a security improvement, and should be a straightforward update.
https://github.com/bobthecow/mustache.php/releases/tag/v2.14.1
(You're receiving this issue because Packagist says you depend on Mustache.php)
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?
Can I integrate this package with the Postfix server?
protected static $templateModel = MailTemplate::class;
// changed to
protected static $templateModelClass = MailTemplate::class;
Is this possible? It seems to be missing.
Sometimes one want to send with [email protected]
or [email protected]
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.