swiftmail symfony在发送前重复检查错误日志/电子邮件
我想在发送电子邮件之前运行重复内容检查为什么CIH在我的symph2应用程序中使用swiftmailer向我发送开发错误日志条目 这个功能就在“错误日志到数据库”功能的旁边,它也有一个重复检查,虽然这个检查要简单得多,但它使用sql 对于这封邮件,我想在接下来的10封邮件中保留上一封邮件的发送主体,这样,如果我的错误日志失控,它就不会继续向我发送相同错误的重复邮件 我是否应该将此正文收集到包含最后10个电子邮件正文的对象上,并将其附加到swift mailer类?或者有没有一种更简单的方法,比如使用swift mailer中已经嵌入的东西来进行此类邮件发送?或者一个会议 编辑,我从后端助手类调用swift mailer,因此我认为只要它至少是半优雅的,我几乎可以在那里做任何事情 EDIT这是该方法的改进版本,该方法同时调用持久化和激发电子邮件swiftmail symfony在发送前重复检查错误日志/电子邮件,symfony,duplicate-removal,swiftmailer,Symfony,Duplicate Removal,Swiftmailer,我想在发送电子邮件之前运行重复内容检查为什么CIH在我的symph2应用程序中使用swiftmailer向我发送开发错误日志条目 这个功能就在“错误日志到数据库”功能的旁边,它也有一个重复检查,虽然这个检查要简单得多,但它使用sql 对于这封邮件,我想在接下来的10封邮件中保留上一封邮件的发送主体,这样,如果我的错误日志失控,它就不会继续向我发送相同错误的重复邮件 我是否应该将此正文收集到包含最后10个电子邮件正文的对象上,并将其附加到swift mailer类?或者有没有一种更简单的方法,比如
<?php
class someWierdClass
{
public function addLogAction(Request $request, $persist = TRUE, $addEmail = TRUE)
{
$responseAdd = array();
if ($this->getRequest()->request->all() !== null) {
$data = $this->getRequest()->request->get('data') ? $this->getRequest()->request->get('data') : 'no_data';
$duplicate = $this->getRequest()->request->get('duplicate', null);
}
if ($addEmail) {
$responseAdd[] = 'firedIt';
$this->fireEmailString('You have an error log here. <br>' . $data);
}
if ($persist)
{
$responseAdd[] = 'persistedIt';
$this->persistLog($data, $duplicate);
}
if ($responseAdd)
{
$body = implode(', ', $responseAdd);
return new Response($body);
}
}
}
将电子邮件记录在表格中,并在每次发送电子邮件时检查该表格是否重复
要做到这一点,您应该创建一个helper函数来查询emails表中的条目,这些条目的正文与您想要发送的正文相匹配。如果查询没有返回任何内容,那么您知道这不是重复的。然后,您将发送电子邮件并将其记录到数据库中。否则,如果它返回一条记录,您将发送一条开发错误日志条目
如果您只想查看最近10封电子邮件,您可以通过查询$body==$new\u body
和$id>=($total\u rows-10)
然后将其注入容器中,并使用类似的方式调用它
$this->container->get('helper')->sendmail($body,$subject,$recipients)代码>好的,谢谢Dan关于使用数据库进行dup检查的想法。如果你注意到,根据你的建议,我已经在做dup检查了,但它让我思考。它帮助我把这些点连接起来
我所做的是在更新数据库时,如果响应重复,则返回答案,然后使用该响应作为标志来确定电子邮件是否触发。(就我而言,我会进一步检查更新的印章是否至少有+1小时的时间,而不是“最后10封电子邮件内容”的想法)
这是密码。。享受
<?php
namespace Acme\AcmeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Acme\AcmeBundle\Entity\Log,
Symfony\Component\HttpFoundation\JsonResponse,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\Config\Definition\Exception\Exception,
Symfony\Component\HttpFoundation\Request,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class someWierdClass
{
/**
* @var array
*/
protected $senderArray = array('no-reply@yorudomain.com' => 'Your Website Name');
/**
* @param Request $request
* @param bool $persist
* @param bool $addEmail
* @return Response
*/
public function addLogAction(Request $request, $persist = TRUE, $addEmail = TRUE)
{
$responseAdd = array();
if ($this->getRequest()->request->all() !== null) {
$data = $this->getRequest()->request->get('data') ? $this->getRequest()->request->get('data') : 'no_data';
$type = $this->getRequest()->request->get('type') ? $this->getRequest()->request->get('type') : 'no_type';
$duplicate = $this->getRequest()->request->get('duplicate', null);
}
if ($addEmail) {
$responseAdd[] = 'firedIt';
$this->fireEmailString('You have an error log here. <br>' . $data);
}
if ($persist) {
$responseAdd[] = 'persistedIt';
$persistResponse = $this->persistLog( $type = $data, $duplicate);
if ($persistResponse) {
// a dup check is done here and results of this is on the response. (e.g. $content->passesCutoff)
$content = json_decode($persistResponse->getContent());
}
}
if ( $addEmail && ( isset($content->passesCutoff) && $content->passesCutoff ))
{
//fire off an email also, because its kind of hard to look in teh logs all the time, sometimes we just want an email.
$successEmail = $this->fireEmailString($data);
if( ! $successEmail )
{
$responseAdd[] = 'firedIt';
}
}
if ($responseAdd) {
$body = implode(', ', $responseAdd);
return new Response($body);
}
}
/**
* @param $emailStringData
* @param null $emailSubject
* @param null $emailTo
* @return mixed
*/
protected function fireEmailString($emailStringData, $emailSubject = null, $emailTo=null){
$templateName = 'AcmeBundle:Default:fireEmailString.html.twig';
if( ! $emailSubject )
{
$emailSubject = 'An email is being fired to you!' ;
}
if( ! $emailTo )
{
$emailTo = 'youremail@gmail.com';
}
$renderedView = $this->renderView(
$templateName, array(
'body' => $emailStringData,
));
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject( $emailSubject)
->setBody($emailStringData, 'text/plain')
->addPart($renderedView, 'text/html')
->setFrom($this->senderArray)
->setSender($this->senderArray)
->setTo($emailTo);
$results = $mailer->send($message);
return $results;
}
/**
* @param $type
* @param $data
* @param $duplicate
* @return JsonResponse
*/
protected function persistLog($type, $data, $duplicate) {
$em = $this->getDoctrine()->getManager();
$count = null;
$passesCutoff = null;
$mysqlNow = new \DateTime(date('Y-m-d G:i:s'));
//only two conditions can satisy here, strings '1' and 'true'.
if($duplicate !== '1' && $duplicate !== 'true' /*&& $duplicate != TRUE*/)
{
//in order to check if its unique we need to get the repo
//returns an object (findByData() would return an array)
$existingLog = $em->getRepository('AcmeBundle:Log')->findOneByData(
array('type' => $type, 'data' => $data)
);
if($existingLog)
{
$timeUpdatedString = strtotime($existingLog->getTimeupdated()->format('Y-m-d H:i:s'));
$cutoffStamp = strtotime('+1 hour', $timeUpdatedString); //advance 1 hour (customize this to the amount of time you want to go by before you consider this a duplicate. i think 1 hour is good)
$passesCutoff = time() >= $cutoffStamp ? TRUE : FALSE; //1 hour later
$count = $existingLog->getUpdatedcount();
$existingLog->setUpdatedcount($count + 1); // '2014-10-11 03:52:20' // date('Y-m-d G:i:s')
$em->persist($existingLog);
}
else
{
//this record isnt found, must be unique
$newLog = new Log(); //load our entity
//set in new values
$newLog->setType($type);
$newLog->setData($data);
$newLog->setUpdatedcount(0);
$newLog->setTimeupdated($mysqlNow);
$em->persist($newLog);
}
}
else
{
//we dont care if unique or not, we just want a new row
$newLog = new Log(); //load our entity
$newLog->setType($type);
$newLog->setData($data);
//time updated has been set to auto update to current timestamp in the schema, test first, then remove this
$newLog->setUpdatedcount(0);
$newLog->setTimeupdated($mysqlNow);
$em->persist($newLog);
}
$em->flush();
$response = new JsonResponse();
$response->setData(
array(
'data' => 'persistedIt',
'existingLog' => $count,
'passesCutoff' => $passesCutoff,
));
return $response;
}
}
斯威夫特没有那样的“记忆”。这是你必须补充的。所以你提到记忆,是的,我应该朝什么方向去创造这种记忆。我认为,因为它是一个助手类,在后端,可能是会话数据。因此,我考虑只创建一个新的空obj,具有最大10个元素的功能,每次创建一个新日志时,将/pop olg log content/new log content替换/pop到obj上,将该数据加载到会话中。下一页运行时,当触发错误时,查找会话,读取错误日志,如果它与会话数据中的10个重复,则不要发送电子邮件。这就是我的php大脑告诉我的。所以在symph或swift中没有这样的内容?我的解决方案解决了你的问题吗?嗯,这很有趣。有趣的是,你说把他们记录到数据库里,因为我已经在这么做了,而且在这个功能里面,我添加了电子邮件功能。已经有一个重复的检查,如果它看到一个dup,那么它只是增加更新的计数,并且只增加更新的时间戳。我会在作品中更新我是如何做到这一点的。所以我的代码你现在看到了,记住了这一点,你给了我一个获胜的想法,我想。那么,你是说如果我从mhy db呼叫那里返回重复的支票,我已经有那个标志了吗?DOH。对非常感谢。是吗?是的,但我必须发一篇帖子来展示我所做的。我将添加这个代码时,我可以得到它刮起来。