Swift_Mailer Symfony 3 添加事件监听器发送事件
Swift_Mailer Symfony 3 add event listener to send event
如何给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 }
这是监听器 class :
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\vatfiles2.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 插件的替代方法,我建议在您的 service/controller 中使用 __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);
}
}
}
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 将忽略重复的插件注册。
如何给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 }
这是监听器 class :
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\vatfiles2.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 插件的替代方法,我建议在您的 service/controller 中使用 __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);
}
}
}
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 将忽略重复的插件注册。