Swift_Mailer Symfony 3添加事件监听器来发送事件

时间:2018-05-24 15:04:03

标签: php symfony swiftmailer symfony3.x

如何将eventListener添加到swiftmailer发送事件? 每次我发送电子邮件我创建一个文件并将其附加到电子邮件,并在发送后我想取消链接该文件。怎么做? 感谢。

 file_put_contents($path, implode(";\r\n", $result));
$message = (new \Swift_Message('VAT checking result !'))
            ->setFrom('vah@gmail.com')
            ->setTo($vat->getEmail())
             ->setBody(
                    'Hello, ...' ,'text/')
             ->attach(\Swift_Attachment::fromPath($path));
// START send result email
    $mailer = $this->container->get('mailer');

    $listener = $this->container->get('app.service.send_email_listener');
    $listener->setPathToFile($path);
    $mailer->registerPlugin($listener);

    $mailer->send( $message );
    // END send email to admin

//unlink($path);  email will not be sent

我试图像那样注册听众

app.service.send_email_listener:
    class: AppBundle\Listener\SendEmailListener
    public: true
    tags:
         - { name: swiftmailer.plugin }

这是听众类:

namespace AppBundle\Listener;

use \Swift_Events_SendListener as base;

class SendEmailListener implements base
{
    private $pathToFile;

    public function setPathToFile($path)
    {
        $this->pathToFile = $path;
    }
    public function getPathToFile($path)
    {
        return $this->pathToFile;
    }


    /**
     * Invoked immediately before the Message is sent.
     *
     * @param \Swift_Events_SendEvent $evt
     */
    public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
    {

    }

    /**
     * Invoked immediately after the Message is sent.
     *
     * @param \Swift_Events_SendEvent $evt
     */
    public function sendPerformed(\Swift_Events_SendEvent $evt)
    {
        if($this->pathToFile){
            unlink($this->pathToFile);
        }
    }
}

修改 它执行方法,但是swift无法传输文件,因为在发送结束之前文件被取消链接... 这是来自dev_logs:

[2018-05-24 20:40:18] php.CRITICAL: Uncaught Exception: Unable to open file for reading [C:\Users\\projects\vat\web\vatfiles\122.txt] {"exception":"[object] (Swift_IoException(code: 0): Unable to open file for reading [C:\\Users\\\projects\\vat\\web\\vatfiles\\122.txt] at C:\\Users\\projects\\vat\\vendor\\swiftmailer\\swiftmailer\\lib\\classes\\Swift\\ByteStream\\FileByteStream.php:144)"} []

1 个答案:

答案 0 :(得分:2)

作为使用Swiftmailer插件的替代方法,我建议在使用该文件的服务/控制器中使用__destruct魔术方法。释放对象时将调用__destruct,并取消链接任何已声明的路径。

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class YourController extends Controller
{
    private $paths = [];

    public function someAction()
    {
         $this->paths[] = $path;
         file_put_contents($path, implode(";\r\n", $result));
         $message = (new \Swift_Message('VAT checking result !'))
            ->setFrom('vah@gmail.com')
            ->setTo($vat->getEmail())
            ->setBody('Hello, ...' ,'text/')
            ->attach(\Swift_Attachment::fromPath($path));
         $mailer = $this->container->get('mailer');
         $mailer->send( $message );

         return $this->redirectToRoute('some_route');
    }

    public function __destruct()
    {
        if ($this->paths) {
            array_map('unlink', $this->paths);
        }
    }

}
  

注意:如果您使用spool to send emails,则此方法可能无效   电子邮件将不会发送,直到达到阈值   卷轴上。

当您使用swiftmailer.plugin标记服务时,Symfony 2.3+会自动注册Swiftmailer插件。所以没有必要调用$mailer->registerPlugin($listener);,swiftmailer只会忽略重复的插件注册。