Mails
Definition
The Mail component allows you to describe an email and send it whenever needed.
Principles
-
Containers MAY or MAY NOT have one or more Mail.
-
Ship may contain general Mails.
Rules
- All Notifications MUST extend from
App\Ship\Parents\Mails\Mail
. - Email Templates must be placed inside the Mail directory in a Templates directory
app/Containers/{section}/{container}/Mails/Templates
.
Folder Structure
- app
- Containers
- {section-name}
- {container-name}
- Mails
- UserRegisteredMail.php
- ...
- Templates
- user-registered.blade.php
- ...
- Ship
- Mails
- SomeMail.php
- ...
- Templates
- some-template.blade.php
- ...
Code Samples
A simple Mail
class UserRegisteredMail extends Mail implements ShouldQueue
{
use Queueable;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function build()
{
return $this->view('appSection@user::user-registered')
->to($this->user->email, $this->user->name)
->with([
'name' => $this->user->name,
]);
}
}
Usage from an Action
Notifications can be sent from Actions or Tasks using the Mail
Facade.
Mail::send(new UserRegisteredMail($user));
Email Templates
Templates should be placed inside a folder Templates
inside the Mail
folder.
To access a Mail template (same like accessing a web view) you must call the camelCase of its Section name + @
+ camelCase of its Container name.
In the example below we're using the user-registered.blade.php
template in the AppSection
Section > User
Container.
$this->view('appSection@user::user-registered');
Configure Emails
Open the .env
file and set the from
mail and address. This will be used globally whenever the from
function is not called in the Mail.
[email protected]
MAIL_FROM_NAME="apiato"
To use different email address in some classes add ->to($this->email, $this->name)
to the build
function in your Mail class.
By default Apiato is configured to use Log Driver MAIL_DRIVER=log
, you can change that from the .env
file.
Queueing A Notification
To queue a notification you should use Illuminate\Bus\Queueable
and implement Illuminate\Contracts\Queue\ShouldQueue
.
More info at Laravel Docs.