Logging swiftmailer send() activity in symfony2

前端 未结 5 1162
轻奢々
轻奢々 2020-12-16 14:53

Im using swiftmailer for sending mails from my symfony2.2 project. Is there a way to log globally all email info and send results?

It would be great if mailer send

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 15:13

    Service:

    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));
        }
    }
    

    Config:

    services:
        umpirsky.mailer.message_file_logger:
            class: MessageFileLogger
            arguments:
                - %kernel.logs_dir%/mailer.log
            tags:
                - { name: swiftmailer.plugin }
    

提交回复
热议问题