matchory/laravel-mailgun-templated-messages

此包已废弃,不再维护。作者建议使用matchory/laravel-mailgun-templates-channel包。

这是一个用于Mailgun模板消息的Laravel通知通道。

1.1.0 2024-01-23 10:09 UTC

This package is auto-updated.

Last update: 2024-01-23 10:23:03 UTC


README

为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。您也可以简单地开启一个问题。别忘了给项目点个star!再次感谢!

(返回顶部)

许可协议

在MIT许可协议下分发。更多信息请见LICENSE

(返回顶部)