Php Swift_Mailer Symfony 3添加事件侦听器以发送事件
如何将eventListener添加到swiftmailer发送事件? 每次我发送电子邮件时,我都会创建一个文件并将其附加到电子邮件中,发送后我想取消该文件的链接。怎么做? 谢谢 我试着这样注册听众Php Swift_Mailer Symfony 3添加事件侦听器以发送事件,php,symfony,swiftmailer,symfony3.x,Php,Symfony,Swiftmailer,Symfony3.x,如何将eventListener添加到swiftmailer发送事件? 每次我发送电子邮件时,我都会创建一个文件并将其附加到电子邮件中,发送后我想取消该文件的链接。怎么做? 谢谢 我试着这样注册听众 app.service.send_email_listener: class: AppBundle\Listener\SendEmailListener public: true tags: - { name: swiftmailer.plugin } 这
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无法流式传输文件,因为文件在发送结束前已解除链接。。。
这来自开发日志:
[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)"} []
作为使用Swiftmailer插件的替代方法,我建议在您的服务/控制器中使用利用文件的magic方法<代码>\uuu 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);
}
}
}
注意:如果使用,此方法可能不起作用,因为
只有在满足的阈值之前,才会发送电子邮件
线轴
当您使用
Swiftmailer.plugin
标记服务时,Symfony 2.3+将自动注册Swiftmailer插件。因此不需要调用$mailer->registerPlugin($listener)代码>,其中swiftmailer将忽略重复的插件注册。Symfony 2.3+将在您使用swiftmailer.plugin
标记服务时自动注册swiftmailer插件。所以你不需要调用$mailer->registerPlugin($listener)代码>,但设置路径在服务上仍然有效。我建议在您的服务/控制器中使用magic方法来取消链接
文件。这样,一旦您的服务/控制器发布,所有相关文件都将被删除。非常感谢Fyrye。成功了。你能补充你的评论作为回答吗?我会接受的
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);
}
}
}