appletonlearning/laravel-postmark-channel

此包已被废弃,不再维护。未建议替代包。

为 Laravel 通知提供 Postmark 通道。

v1.0.4 2018-02-19 13:32 UTC

This package is not auto-updated.

Last update: 2021-01-09 09:44:25 UTC


README

Latest Version on Packagist Software License Build Status

为 Laravel 提供的 Postmark 通知通道

这为 Laravel 添加了一个 Postmark 通知通道,允许您的应用程序利用 Postmark API 跟踪与您的电子邮件相关的事件,如打开、退回、点击等。

内容

安装

需要 PHP 7.0+

$ composer require appletonlearning/laravel-postmark-channel

如果使用 Laravel 5.5+,则包将自动发现。

设置 Postmark 服务

  1. 在 Postmark 上获取您服务器的 API 密钥
  2. 将密钥添加到您的环境配置中作为 POSTMARK_KEY

用法

发送电子邮件

在每个您希望通过 Postmark 通知的 Notifiable 模型中,您必须添加一个电子邮件地址到该模型,并通过 routeNotificationForPostmark 方法访问

class User extends Eloquent
{
    use Notifiable;

    public function routeNotificationForPostmark()
    {
        return $this->email;
    }
}

您现在可以告诉 Laravel 使用 Postmark 在 via 方法中发送通知

use Spur\Postmark\PostmarkChannel;
use Spur\Postmark\PostmarkMessage;

class InvoiceNotification extends Notification
{
    public function via($notifiable)
    {
        return [PostmarkChannel::class];
    }

    public function toPostmark($notifiable)
    {
    	$url = url('/invoice/'.$this->invoice->id);
    
        return (new PostmarkMessage)
            ->greeting('Hello!')
            ->line('One of your invoices has been paid!')
            ->action('View Invoice', $url)
            ->line('Thank you for using our application!');
    }
}

发送 Markdown 电子邮件

就像常规的 mail 类型通知一样,您也可以发送 Markdown 电子邮件

public function toPostmark($notifiable)
{
    $url = url('/invoice/'.$this->invoice->id);

    return (new PostmarkMessage)
        ->subject('Invoice Paid')
        ->markdown('mail.invoice.paid', ['url' => $url]);
}

实际上,Postmark 通道与 Laravel 内置的 mail 通道具有相同的 API。请参阅 Laravel 的 邮件通知Markdown 邮件通知 文档以获取完整选项。

跟踪通知

使用 Postmark 通道而不是默认的 mail 通道的好处之一是可以进行事件跟踪,例如已发送电子邮件的投递、点击、打开和退回。要跟踪电子邮件事件,您必须首先将每个通知存储在您的数据库中,该数据库至少有一个列用于跟踪电子邮件的 Postmark 特定的 MessageID

为了在发送时捕获 ID,我们监听 Laravel 的 Illuminate\Notifications\Events\NotificationSent 事件。在您的 EventServiceProvider 中注册此事件的监听器

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'Illuminate\Notifications\Events\NotificationSent' => [
        'App\Listeners\LogNotification',
    ],
];

正如 Laravel 文档 所述

在事件监听器中,您可以访问事件上的可通知的、通知和通道属性,以了解通知接收者或通知本身...

/**
 * Handle the event.
 *
 * @param  NotificationSent  $event
 * @return void
 */
public function handle(NotificationSent $event)
{
    // $event->channel
    // $event->notifiable
    // $event->notification
    // $event->response
}

使用 Postmark,$event->response 对象包含 ID、状态和其他当电子邮件通过他们的系统发送时从 Postmark 返回的 数据

{
    "To": "receiver@example.com",
    "SubmittedAt": "2014-02-17T07:25:01.4178645-05:00",
    "MessageID": "0a129aee-e1cd-480d-b08d-4f48548ff48d",
    "ErrorCode": 0,
    "Message": "OK"
}

一旦您的邮件通过Postmark的MessageID成功存储,使用Postmark Webhooks API跟踪每封邮件发生的所有事件变得非常简单。

测试

$ composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件adam@spurjobs.com联系我们,而不是使用问题追踪器。

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。