From c0197c259f5e411a0179b92bce101c8031b4a967 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 4 Sep 2019 15:16:12 +0000 Subject: [PATCH] - WIP on a new EmailMiddleware to send errors into a friendly formatted message. --- src/EmailMiddleware.php | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/EmailMiddleware.php diff --git a/src/EmailMiddleware.php b/src/EmailMiddleware.php new file mode 100644 index 0000000..4f1aabd --- /dev/null +++ b/src/EmailMiddleware.php @@ -0,0 +1,80 @@ +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; + } +}