matchory/laravel-mailgun-templates-channel

为Mailgun的模板消息提供Laravel通知渠道。

1.1.0 2024-01-23 10:09 UTC

This package is auto-updated.

Last update: 2024-09-23 11:41:45 UTC


README

Mailgun模板消息通知渠道 最新稳定版本 总下载量 最新不稳定版本 许可 Laravel Octane 兼容

为Laravel应用程序提供Mailgun消息模板的通知渠道。

此库向您的应用程序添加了一个新的通知渠道,将电子邮件模板从Laravel移动到Mailgun。例如,如果您需要从多个应用程序发送电子邮件,或者需要一种简单的方法让非开发者管理电子邮件模板,这将很有用。

安装

使用composer安装库

composer require matchory/laravel-mailgun-templates-channel

配置

配置遵循Laravel文档中的说明:您应该在config/services.php文件中放置您的Mailgun凭据。

    // ...

    'mailgun' => [
    
        // Add your mailing domain as registered on Mailgun
        'domain' => env('MAILGUN_DOMAIN', 'mailing.example.com'),

        // Add your Mailgun secret
        'secret' => env('MAILGUN_SECRET'),
        
        // Optional: Specify the endpoint of Mailgun's EU API if you're a EU
        // customer and need to comply to the GDPR
        'endpoint' => env('MAILGUN_ENDPOINT', 'https://api.eu.mailgun.net'),
    ],

    // ...

(返回顶部)

用法

要发送消息模板,您首先需要在Mailgun上创建一个模板(在Mailgun Web应用程序中导航到“发送”>“模板”以管理模板)。然后,创建一个使用toMailgun方法的新通知。

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;

class TestNotification extends Notification
{
    use Queueable;
    
    public function __construct(private readonly int $randomNumber) {}

    public function toMailgun(mixed $notifiable): MailgunTemplatedMessage
    {
        return (new MailgunTemplatedMessage('your_template_name'))
            ->from('noreply@example.com')
            ->subject('Test Subject')
            ->param('foo', 'bar')
            ->params([
                'some' => 'more data',
                'available' => 'in your template',
                'name' => $notifiable->name,
                'number' => $this->randomNumber
            ]);
    }
}

发送该通知,您将收到一个包含渲染模板的电子邮件。

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;

// number chosen by fair dice roll
Notification::sendNow(Auth::user(), new TestNotification(4));

就这样 - 您现在可以使用消息模板了!

(返回顶部)

高级用法

MailgunTemplatedMessage实例公开了几个消息构建方法,用于向您的消息添加更多元数据。这包括像主题、抄送、密送、收件人和发件人这样的常规内容,以及选项参数
通过设置额外的选项,您可以控制Mailgun的功能,如延迟交付和跟踪;通过设置参数,您可以在Mailgun渲染模板时添加模板变量。

有关详细信息,请参阅Mailgun文档

消息选项

以下方法可以用于处理消息选项

use Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;

$message = new MailgunTemplatedMessage();

// Add an option.
// Note that the value can be anything that can be converted to JSON!
$message->addOption(name: 'skip-verification', value: false);

// Use the fluent methods for chaining several operations together. They all
// have an equivalent getter and setter.
$message->option(name: 'skip-verification', value: false)
        ->option('require-tls', true)

// Set multiple options at once
$message->options([
    'skip-verification' => false,
    'require-tls' => true,
]);

// Check whether options are set
$message->hasOption('require-tls'); // true

// Retrieve all options
$options = $message->getOptions(); 

// Remove a previously set option. If the option isn't set, this does nothing
$message->removeOption('require-tls');

// Equivalent to the above removeOption() call
$message = $message->withoutOption('require-tls');

(返回顶部)

模板参数

以下方法可以用于处理模板渲染参数

use Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;

$message = new MailgunTemplatedMessage();

// Add an param.
// Note that the value can be anything that can be converted to JSON!
$message->addParam(name: 'foo', value: 'bar');

// Use the fluent methods for chaining several operations together. They all
// have an equivalent getter and setter.
$message->param(name: 'foo', value: 'bar')
        ->param('baz', true)

// Set multiple params at once
$message->params([
    'foo' => false,
    'bar' => true,
]);

// Check whether params are set
$message->hasParam('foo'); // true

// Retrieve all params
$params = $message->getParams(); 

// Remove a previously set param. If the param isn't set, this does nothing
$message->removeParam('foo');

// Equivalent to the above removeParam() call
$message = $message->withoutParam('foo');

(返回顶部)

管理模板

遗憾的是,Mailgun SDK目前没有提供管理消息模板的功能,尽管Mailgun服务器上存在API端点(有关参考,请参见此问题)。

一旦模板API实现,我们将为此库添加管理功能 - 包括自动从本地blade文件更新您的消息模板。

(返回顶部)

贡献

贡献使开源社区成为一个如此神奇的学习、灵感和创造的地方。您所做的一切贡献都备受珍视。

如果您有让这个项目变得更好的建议,请Fork仓库并创建一个Pull Request。您也可以简单地开启一个问题。别忘了为该项目点个赞!再次感谢!

(返回顶部)

许可证

在MIT许可证下分发。更多信息请参见LICENSE

(返回顶部)