elfsundae/laravel-multi-mail

为Laravel 5.3缺失的多邮件实现

1.0.0 2016-12-03 01:56 UTC

This package is auto-updated.

Last update: 2024-08-29 04:26:57 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

此包提供了一种灵活的方式来帮助您扩展Laravel的邮件服务,这是Laravel 5.3缺失的多邮件实现。

Laravel邮件服务提供了一系列优雅的方式来发送电子邮件,例如MailerMail外观),MailableMailableMailer,以及新的Mail Notification。在开始使用此包之前,请确保您已阅读了官方邮件文档。此包不会改变您已熟悉的发送电子邮件的方式,但可以帮助您自定义Laravel邮件服务,例如在运行时管理多邮件驱动程序,处理最终发送的消息。

安装

  1. 使用Composer管理器安装此包

    $ composer require elfsundae/laravel-multi-mail
  2. config/app.php文件中将Illuminate\Mail\MailServiceProvider::class替换为ElfSundae\Multimail\MailServiceProvider::class

架构

  • ElfSundae\Multimail\Mailer (扩展Illuminate\Mail\Mailer

    Mailer类是Laravel邮件系统中的外观和主要中心,所有发送任务将由此类处理。您可以使用Mail外观或app('mailer')辅助函数访问它,也可以通过Mailer类型提示或依赖注入。

  • ElfSundae\Multimail\SwiftMailerManager

    SwiftMailerManager单例管理所有Swift Mailer实例及其对应的Swift Transport实例,为Mailer创建、缓存、重置或销毁它们。每个Swift Mailer实例由其传输程序的驱动程序名称标识,例如smtpmailgun等。您可以通过Mail::getSwiftMailerManager()app('swift.manager')SwiftMailerManager类型提示或依赖注入来访问管理器。

  • ElfSundae\Multimail\MessageHelper

    它提供了一些操作邮件消息的辅助方法,例如获取邮件接收者的电子邮件地址的域名。

使用示例

以下是几个使用示例。请记住,您可以按照自己的意愿进行任何定制。

自定义邮件驱动程序

Laravel附带了一些邮件驱动程序,但您可能想编写自己的驱动程序,通过其他邮件服务发送电子邮件。Laravel使这变得很简单。通过使用TransportManager单例的extend方法,您可以注册自定义驱动程序创建器。

<?php

namespace App\Providers;

use Illuminate\Mail\TransportManager;
use Illuminate\Support\ServiceProvider;
use App\Support\Mail\FooTransport;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->resolving(function (TransportManager $manager) {
            $manager->extend('foo', function ($app) {
                $config = $app['config']['services.foo'];

                return new FooTransport($config['key'], $config['secret']);
            });
        });
    }
}

更改默认驱动程序

您可以通过mailDriver方法在运行时更改默认驱动程序,而不是使用在config/mail.php文件中指定的邮件驱动程序。

Mail::mailDriver('mailgun')->to($user)->send(new OrderShipped($order));

💡 注意:在运行时更改邮件驱动程序不会影响队列发送作业的驱动程序,它仅在此应用生命周期内有效。

处理最终消息

此包使您能够在发送邮件之前处理每个最终邮件消息。为此,请通过registerSendingMessageHandler方法注册全局消息处理程序。

<?php

namespace App\Providers;

use ElfSundae\Multimail\Mailer;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->resolving(function (Mailer $mailer) {
            $mailer->registerSendingMessageHandler(function ($message) {
                $message->addBcc('syslog@example.com');
            });
        });
    }
}

传递给处理器的第一个参数是邮件消息类型,为 Swift_Message,你可以自由地添加额外的类型提示依赖。

$mailer->registerSendingMessageHandler(
    function (CacheRepository $cache, SwiftMailerManager $swift, $message, $mailer) {
        //
    }
);

除了 Closure 外,处理器还可以使用类名进行注册。在发送邮件之前,该类的 sendingMail 方法将被调用。

$mailer->registerSendingMessageHandler('App\Mail\Handler\SendingMessage');

当然,你可以指定方法名称。

$mailer->registerSendingMessageHandler('App\Mail\Handler\SendingMessage@sendingMailHandler');

更改邮件消息的驱动程序

发送消息处理器 的返回值可以是邮件驱动程序名称,通过这种方式,邮件将使用指定的驱动程序发送。

$mailer->registerSendingMessageHandler(function ($message) {
    if (preg_match_all(
        '#@(.+\.)?(qq.com|126.com|163.com|sina.com|sina.cn)$#im',
        implode(PHP_EOL, MessageHelper::getRecipients($message))
    )) {
        return 'directmail';
    }
});

重置Swift Mailers

使用 SwiftMailerManagerresetMailerresetMailers 方法,你可以重置创建的 Swift Mailer 实例。

$this->updateMailConfig();

Mail::getSwiftMailerManager()->resetMailers();

许可协议

MIT 许可证