Mails 本文未发布 发布文章

未匹配的标注

Definition

The Mail component allows you to describe an email and send it whenever needed.
For more details refer to this link.

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/{container}/Mails/Templates.

Folder Structure

 - app
    - Containers
        - {container-name}
            - Mails
                - UserRegisteredMail.php
                - ...
                - Templates
                    - user-registered.blade.php
                    - ...
    - Ship
        - Mails
            - SomeMail.php
            - ...
            - Templates
                - some-template.blade.php
                - ...

Code Samples

Example: a simple Mail

<?php

namespace App\Containers\User\Mails;

use App\Containers\User\Models\User;
use Illuminate\Bus\Queueable;
use App\Ship\Parents\Mails\Mail;
use Illuminate\Contracts\Queue\ShouldQueue;

class UserRegisteredMail extends Mail implements ShouldQueue
{
    use Queueable;

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->view('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 container name then the view name.
In the example below we’re using the user-registered.blade.php template in the User Container.

$this->view('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.

MAIL_FROM_ADDRESS=test@test.test
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 .envTo queue a notification you should use Illuminate\Bus\Queueable and implement Illuminate\Contracts\Queue\ShouldQueue. file.

Queueing A Notification

Events can implement Illuminate\Contracts\Queue\ShouldQueue to be queued.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~