loots-it/laravel-mail-template-channel

这是一个替代标准邮件通道的方案,它使用外部模板来显示内容。例如,提供此类模板的Mailjet服务,为其提供了实现。

v1.0.0-beta 2020-10-28 15:31 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:59 UTC


README

Total Downloads Latest Stable Version License

安装

首先,使用composer包含此包

composer require loots-it/laravel-mail-template-channel

最重要的Provider是MailTemplateDriverServiceProvider,它通过composer.json文件自动发现。

另一个Provider是TestMailTemplateChannelServiceProvider,它为测试MailTemplateDriver配置添加了一个Artisan命令。如果您想使用它,您需要手动将其添加到config/app.php文件中。

  • Provider数组
'providers' => [
    ...
    LootsIt\LaravelMailTemplateChannel\Providers\TestMailTemplateChannelServiceProvider::class,
    LootsIt\LaravelMailTemplateChannel\Providers\MailTemplateDriverServiceProvider::class, // This one is auto discovered
    ...
],

配置

在您的.env文件中添加Mailjet API密钥/密钥

MAILJET_APIKEY=YOUR_APIKEY
MAILJET_APISECRET=YOUR_APISECRET

您可以在这里找到您的API密钥/密钥。

将以下部分添加到config/services.php文件中

'mailjet' => [
    'key' => env('MAILJET_APIKEY'),
    'secret' => env('MAILJET_APISECRET'),
],

如果您想通过通知发送电子邮件而不在通知中指定发送者电子邮件/姓名,您必须添加一个默认发送者。为此,您首先需要发布配置文件

php artisan vendor:publish --provider="LootsIt\LaravelMailTemplateChannel\Providers\MailTemplateDriverServiceProvider"

然后您需要添加默认发送者电子邮件地址和默认发送者姓名

return [
    'from' => [
        'email' => 'info@yourdomain.com',
        'name' => 'yourdomain.com',
    ],
];

测试配置

您可以使用TestMailTemplateChannelServiceProvider提供的命令测试邮件模板Driver/Channel的配置。您需要一个不需要任何变量的模板,并将id(在本例中为1)传递给命令

php artisan mailTemplateDriver:test 1

这实际上不会发送电子邮件,它只会测试配置。

用法

您可以使用此通道发送任何通知。以下是一个创建最小通知的例子

php artisan make:notification VerifyEmailNotification

如果您想删除标准的toMail($notifiable)方法,您可以这样做。您应该更改via($notifiable)方法并实现如下的toExternalMailTemplate($notifiable)

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use LootsIt\LaravelMailTemplateChannel\ExternalMailTemplateChannel;
use LootsIt\LaravelMailTemplateChannel\MailTemplateMessage;

class VerifyEmailNotification extends Notification
{
    use Queueable;

    private int $templateID;
    private string $verificationLink;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($templateID, $verificationLink)
    {
        $this->templateID = $templateID;
        $this->verificationLink = $verificationLink;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [ExternalMailTemplateChannel::class];
    }

    public function toExternalMailTemplate($notifiable) {

        $variables = [
            "verification_link" => $this->verificationLink,
        ];

        $message = new MailTemplateMessage($this->templateID, $variables);
        $message->subject = "Verify email address";

        return $message;
    }
}

您可以使用此通知如下

$user->notify(New VerifyEmailNotification($templateID, $verificationLink));