17

私のsymfony2.2プロジェクトからメールを送信するためにswiftmailerを使用しています。すべての電子メール情報をグローバルに記録し、結果を送信する方法はありますか?

メーラーの send() メソッドにいくつかのイベントをトリガーする機能があれば素晴らしいのですが、そうであることがわかりません。

4

5 に答える 5

9

サービス:

class MessageFileLogger implements Swift_Events_SendListener
{
    private $filename;

    public function __construct($filename)
    {
        $this->filename = $filename;
    }

    public function getMessages()
    {
        return $this->read();
    }

    public function clear()
    {
        $this->write(array());
    }

    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
        $messages = $this->read();
        $messages[] = clone $evt->getMessage();

        $this->write($messages);
    }

    public function sendPerformed(Swift_Events_SendEvent $evt)
    {
    }

    private function read()
    {
        if (!file_exists($this->filename)) {
            return array();
        }

        return (array) unserialize(file_get_contents($this->filename));
    }

    private function write(array $messages)
    {
        file_put_contents($this->filename, serialize($messages));
    }
}

構成:

services:
    umpirsky.mailer.message_file_logger:
        class: MessageFileLogger
        arguments:
            - %kernel.logs_dir%/mailer.log
        tags:
            - { name: swiftmailer.plugin }
于 2014-04-16T10:16:38.190 に答える
5

私はこのようにしました:

1.私のサービス構成

# /src/Tiriana/MyBundle/Resources/config/services.yml
parameters:
     swiftmailer.class:  Tiriana\MyBundle\Util\MailerWrapper

2.メーラーラッパーサービス

Swift_Mailerメーラーが のインスタンスであることを期待して、さまざまなクラスに渡されるため、拡張されますSwift_Mailer。そして...が(リンク)にSwift_Mailerあるため、インスタンスをフィールドとして作成します。もしそうなら、コードはとても良いでしょう...$transportprivate\Swith_Mailer$transportprotected

// /src/Tiriana/MyBundle/Util/MailerWrapper.php
namespace Tiriana\MyBundle\Util;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class MailerWrapper extends \Swift_Mailer
{
    private $_logger;

    /** @var \Swift_Mailer */
    private $_mailer;

    public function send(\Swift_Mime_Message $message, &$failedRecipients = null)
    {
        $this->_log('BEFORE SEND');  // <-- add your logic here
        $ret = $this->_mailer->send($message, $failedRecipients);
        $this->_log('AFTER SEND');  // <-- add your logic here

        return $ret;
    }

    /** @return Logger */
    public function getLogger()
    {
        return $this->_logger;
    }

    protected function _log($msg)
    {
        $this->getLogger()->debug(__CLASS__ . ": " . $msg);
    }

    public function __construct(\Swift_Transport $transport, Logger $logger)
    {
        /* we need _mailer because _transport is private
           (not protected) in Swift_Mailer, unfortunately... */
        $this->_mailer = parent::newInstance($transport);
        $this->_logger = $logger;
    }

    public static function newInstance(\Swift_Transport $transport)
    {
        return new self($transport);
    }

    public function getTransport()
    {
        return $this->_mailer->getTransport();
    }

    public function registerPlugin(Swift_Events_EventListener $plugin)
    {
        $this->getTransport()->registerPlugin($plugin);
    }
}

3.バンドルビルダー

// /src/Tiriana/MyBundle/TirianaMyBundle.php
namespace Tiriana\MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use Tiriana\MyBundle\DependencyInjection\Compiler\OverrideServiceSwiftMailer;

class TirianaMyBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new OverrideServiceSwiftMailer());  // <-- ADD THIS LINE
    }
}

4.そしてOverrideServiceSwiftMailerクラス

// /src/Tiriana/MyBundle/DependencyInjection/Compiler/OverrideServiceSwiftMailer.php
namespace Tiriana\MyBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class OverrideServiceSwiftMailer implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        /* @var $definition \Symfony\Component\DependencyInjection\DefinitionDecorator */
        $definition = $container->findDefinition('mailer');
        $definition->addArgument(new Reference('logger'));
        /* add more dependencies if you need - i.e. event_dispatcher */
    }
}
于 2013-08-08T15:33:04.307 に答える
3

構成の「services」セクションに以下を追加すると、トランスポートとのやり取りが stdout に出力されます (これは、「swiftmailer:email:send」または「swiftmailer:spool:」などのコンソール コマンドを使用してデバッグしている場合に役立ちます。送信'):

services:

    # (...)

    swiftmailer.plugins.loggerplugin:
        class: 'Swift_Plugins_LoggerPlugin'
        arguments: ['@swiftmailer.plugins.loggerplugin.logger']
        tags: [{ name: 'swiftmailer.default.plugin' }]

    swiftmailer.plugins.loggerplugin.logger:
        class: 'Swift_Plugins_Loggers_EchoLogger'
        arguments: [false]

localhost への SMTP トランスポートを使用した出力例:

$ app/console swiftmailer:email:send --subject="Test" --body="Yo! :)" --from="user@example.com" --to="user@example.com"

++ Starting Swift_Transport_EsmtpTransport
<< 220 example.com ESMTP Exim 4.86 Thu, 07 Jan 2016 13:57:43 +0000

>> EHLO [127.0.0.1]

<< 250-example.com Hello localhost [127.0.0.1]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP

++ Swift_Transport_EsmtpTransport started
>> MAIL FROM: <user@example.com>

<< 250 OK

>> RCPT TO: <user@example.com>

<< 451 Temporary local problem - please try later

!! Expected response code 250/251/252 but got code "451", with message "451 Temporary local problem - please try later"
>> RSET

<< 250 Reset OK

Sent 0 emails

++ Stopping Swift_Transport_EsmtpTransport
>> QUIT

<< 221 example.com closing connection

++ Swift_Transport_EsmtpTransport stopped
于 2016-01-07T14:02:54.063 に答える