<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / laravel-database-mail-templates example snippets
namespace App\Mail;
use Spatie\MailTemplates\TemplateMailable;
class WelcomeMail extends TemplateMailable
{
/** @var string */
public $name;
public function __construct(User $user)
{
$this->name = $user->name;
}
public function getHtmlLayout(): string
{
$pathToLayout = storage_path('mail-layouts/main.html');
return file_get_contents($pathToLayout);
}
}
MailTemplate::create([
'mailable' => \App\Mail\WelcomeMail::class,
'subject' => 'Welcome, {{ name }}',
'html_template' => '<p>Hello, {{ name }}.</p>',
'text_template' => 'Hello, {{ name }}.'
]);
Mail::to($user->email)->send(new WelcomeMail($user));
use Illuminate\Database\Seeder;
class MailTemplatesSeeder extends Seeder
{
public function run()
{
MailTemplate::create([
'mailable' => \App\Mail\WelcomeMail::class,
'subject' => 'Welcome, {{ name }}',
'html_template' => '<h1>Hello, {{ name }}!</h1>',
'text_template' => 'Hello, {{ name }}!',
]);
}
}
namespace App\Mail;
use TemplateMailable;
class WelcomeMail extends TemplateMailable
{
/** @var string */
public $name;
/** @var string */
public $email;
public function __construct(User $user)
{
$this->name = $user->name;
$this->email = $user->email;
}
}
namespace App\Mail;
use TemplateMailable;
class WelcomeMail extends TemplateMailable
{
public function __construct(User $user)
{
$this->setAdditionalData([
'name' => 'Joe Bloggs'
]);
}
}
use Spatie\MailTemplates\Models\MailTemplate;
class MeetupMailTemplate extends MailTemplate implements MailTemplateInterface
{
public function meetupGroup(): BelongsTo
{
return $this->belongsTo(MeetupGroup::class);
}
public function scopeForMailable(Builder $query, Mailable $mailable): Builder
{
return $query
->where('mailable', get_class($mailable))
->where('meetup_group_id', $mailable->getMeetupGroupId());
}
public function getHtmlLayout(): string
{
return $this->meetupGroup->mail_layout;
}
}
use Spatie\MailTemplates\TemplateMailable;
class NewMeetupPlannedMail extends TemplateMailable
{
// use our custom mail template model
protected static $templateModelClass = MeetupMailTemplate::class;
/** @var string */
public $location;
/** @var \App\Models\Meetup */
protected $meetup; // protected property, we don't want this in the template data
public function __construct(Meetup $meetup)
{
$this->meetup = $meetup;
$this->location = $meetup->location;
}
// provide a method to get the meetup group id so we can use it in MeetupMailTemplate
public function getMeetupGroupId(): int
{
return $this->meetup->meetup_group_id;
}
}
use Spatie\MailTemplates\TemplateMailable;
class WelcomeMail extends TemplateMailable
{
// ...
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 '<header>Site name!</header>{{{ body }}}<footer>Copyright 2018</footer>';
}
}
MailTemplate::create([
'mailable' => WelcomeMail::class,
'html_template' => '<p>Welcome, {{ name }}!</p>',
]);
Mail::to($user->email)->send(new WelcomeMail($user));
use Spatie\MailTemplates\Models\MailTemplate;
class EventMailTemplate extends MailTemplate
{
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
public function getHtmlLayout(): string
{
return $this->event->mail_layout_html;
}
}
use \Spatie\MailTemplates\MailTemplate;
class MailTemplate extends MailTemplate
{
use HasTranslations;
public $translatable = ['subject', 'html_template'];
}