public function sendMail(\Exception $exception, Request $request, array $context, $needToFlush)
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
}
if (!$this->_hasInitialized) {
$this->_initialize();
}
$params = array('exception' => $exception, 'request' => $request, 'context' => $context, 'status_text' => Response::$statusTexts[$exception->getStatusCode()]);
$preMailEvent = new GenericEvent($params, array('shouldSend' => true));
$this->_eventDispatcher->dispatch('ehough.bundle.emailErrors.preMail', $preMailEvent);
if (!$preMailEvent->getArgument('shouldSend')) {
//mail was cancelled
return;
}
$body = $this->_templatingEngine->render('EhoughEmailErrorsBundle::mail.html.twig', $params);
$subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage();
if (function_exists('mb_substr')) {
$subject = mb_substr($subject, 0, 255);
} else {
$subject = substr($subject, 0, 255);
}
$mail = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->_fromAddress)->setTo($this->_toAddress)->setContentType('text/html')->setBody($body);
$this->_mailer->send($mail);
if ($needToFlush) {
$this->_flushEmailer();
}
}