gpt4 book ai didi

php - Swift_Mailer Symfony 3 添加事件监听发送事件

转载 作者:行者123 更新时间:2023-12-04 16:00:50 26 4
gpt4 key购买 nike

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

 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)"} []

最佳答案

作为使用 Swiftmailer 插件的替代方案,我建议使用 __destruct您的服务/ Controller 中使用文件的魔术方法。 __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);
}
}

}

NOTE: This approach may not work if you use a spool to send emails, as the email will not be sent until the thresholds have been met for the spool.


当您使用 swiftmailer.plugin 标记服务时,Symfony 2.3+ 将自动注册 Swiftmailer 插件。因此无需调用 $mailer->registerPlugin($listener);,swiftmailer 将忽略重复的插件注册。

关于php - Swift_Mailer Symfony 3 添加事件监听发送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50512596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com