2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

swiftmailerで特定ドメイン以外送らないようにする

2
Posted at

SwiftMailerでのメール送信を利用していると、環境によっては特定ドメインだけ送るとか送らないとかそういったことをやりたいことがありました。
例えば、ステージング環境でメール送信がされるときは自社ドメイン以外はキャンセルするとかですね。

対処法

プラグインを自作しましょう。

<?php

class FilterDomainPlugin implements \Swift_Events_SendListener
{
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
        foreach($evt->getMessage()->getTo() as $email => $name) {
            if () { // 判定を入れる
                $evt->cancelBubble();
            }
        }
    }

    public function sendPerformed(Swift_Events_SendEvent $evt)
    {
    }
}

このようなプラグインを作ることでキャンセルができるようになります。

登録は以下のようにすればおっけー。

$transport->registerPlugin(new FilterDomainPlugin());

Laravelの場合は

Mail::getSwiftMailer()->registerPlugin(new FilterDomainPlugin());

から可能です。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?