Merge branch 'master' of https://git.mcnd.ca/mcndave/the-bugs into dev
This commit is contained in:
commit
689c3203e7
|
@ -18,6 +18,12 @@ class EmailConfiguration
|
||||||
|
|
||||||
public $smtpUseTLS = false;
|
public $smtpUseTLS = false;
|
||||||
|
|
||||||
|
public $toAddress = "";
|
||||||
|
|
||||||
|
public $fromAddress = "";
|
||||||
|
|
||||||
|
public $fromName = "";
|
||||||
|
|
||||||
public function __construct($type = self::AUTH_TYPE_SMTP)
|
public function __construct($type = self::AUTH_TYPE_SMTP)
|
||||||
{
|
{
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
|
|
|
@ -2,19 +2,11 @@
|
||||||
|
|
||||||
namespace TheBugs;
|
namespace TheBugs;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseFactoryInterface,
|
use Psr\Http\Message\ResponseInterface,
|
||||||
Psr\Container\ContainerInterface,
|
|
||||||
Psr\Http\Message\ResponseInterface,
|
|
||||||
Psr\Http\Message\ServerRequestInterface,
|
Psr\Http\Message\ServerRequestInterface,
|
||||||
Psr\Http\Server\MiddlewareInterface,
|
Psr\Http\Server\MiddlewareInterface,
|
||||||
Psr\Http\Server\RequestHandlerInterface;
|
Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
use Zend\Diactoros\Response,
|
|
||||||
Zend\Diactoros\ServerRequest,
|
|
||||||
Zend\Diactoros\Stream,
|
|
||||||
Zend\Diactoros\Uri;
|
|
||||||
|
|
||||||
|
|
||||||
class EmailErrorMiddleware implements MiddlewareInterface
|
class EmailErrorMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
protected /* EmailConfiguration */ $emailConfiguration;
|
protected /* EmailConfiguration */ $emailConfiguration;
|
||||||
|
@ -35,9 +27,8 @@ class EmailErrorMiddleware implements MiddlewareInterface
|
||||||
$response = $handler->handle($request);
|
$response = $handler->handle($request);
|
||||||
|
|
||||||
if ( $response->getStatusCode() === 500 ) {
|
if ( $response->getStatusCode() === 500 ) {
|
||||||
|
$this->mailer->setTo($this->emailConfiguration->toAddress);
|
||||||
$this->mailer->setTo('dave.mcnicoll@cslsj.qc.ca');
|
$this->mailer->setFrom([$this->emailConfiguration->fromAddress => $this->emailConfiguration->fromName]);
|
||||||
$this->mailer->setFrom(['test@johndoe.com' => 'John Doe']);
|
|
||||||
|
|
||||||
$bugReport = $response->getBody()->__toString();
|
$bugReport = $response->getBody()->__toString();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TheBugs;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseFactoryInterface,
|
||||||
|
Psr\Container\ContainerInterface,
|
||||||
|
Psr\Http\Message\ResponseInterface,
|
||||||
|
Psr\Http\Message\ServerRequestInterface,
|
||||||
|
Psr\Http\Server\MiddlewareInterface,
|
||||||
|
Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
|
use Monolog\Processor\WebProcessor,
|
||||||
|
Monolog\Processor\MemoryUsageProcessor,
|
||||||
|
Monolog\Processor\IntrospectionProcessor;
|
||||||
|
|
||||||
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
|
||||||
|
class EmailMiddleware implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
const DEFAULT_FROM_NAME = "Bug Email Reporter";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PHPMailer object
|
||||||
|
* @var PHPMailer
|
||||||
|
*/
|
||||||
|
protected $phpmailer = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Monolog object
|
||||||
|
* @var Logger object
|
||||||
|
*/
|
||||||
|
protected $logger = null;
|
||||||
|
|
||||||
|
public function __construct(array $mail, array $options = [], PHPMailer $mailer = null, Logger $logger = null)
|
||||||
|
{
|
||||||
|
$this->mailer = $mailer ?: new PHPMailer(true);
|
||||||
|
$this->logger = $logger ?: new Logger('default');
|
||||||
|
$this->handler = new PHPMailerHandler($this->mailer);
|
||||||
|
|
||||||
|
$this->mailer->isSMTP();
|
||||||
|
$this->mailer->Host = $mail['host'];
|
||||||
|
$this->mailer->SMTPAuth = $mail['smtp_auth'] ?? true;
|
||||||
|
$this->mailer->Username = $mail['username'];
|
||||||
|
$this->mailer->Password = $mail['password'];
|
||||||
|
|
||||||
|
$this->mailer->setFrom($mail['from_address'], $mail['from_name'] ?? static::DEFAULT_FROM_NAME);
|
||||||
|
|
||||||
|
foreach($this->adjustAddress($mail['to'] ?? []) as $item) {
|
||||||
|
$this->mailer->addAddress($item['address'], $item['name'] ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($this->adjustAddress($mail['cc'] ?? []) as $item) {
|
||||||
|
$this->mailer->addCC($item['address'], $item['name'] ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($this->adjustAddress($mail['bcc'] ?? []) as $item) {
|
||||||
|
$this->mailer->addBCC($item['address'], $item['name'] ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->handler->setFormatter(new HtmlFormatter);
|
||||||
|
$this->logger->pushHandler($this->handler);
|
||||||
|
|
||||||
|
$this->logger->pushProcessor(new IntrospectionProcessor);
|
||||||
|
$this->logger->pushProcessor(new MemoryUsageProcessor);
|
||||||
|
$this->logger->pushProcessor(new WebProcessor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||||
|
{
|
||||||
|
return $handler->handle($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function adjustAddress($address) {
|
||||||
|
return is_string($address) ? [ [ 'address' => $address ] ] : $address;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue