| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Monolog package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Monolog\Handler;
|
|
|
13 |
|
|
|
14 |
use Monolog\Logger;
|
|
|
15 |
use Monolog\Utils;
|
|
|
16 |
use Monolog\Formatter\FormatterInterface;
|
|
|
17 |
use Monolog\Formatter\LineFormatter;
|
|
|
18 |
use Symfony\Component\Mailer\MailerInterface;
|
|
|
19 |
use Symfony\Component\Mailer\Transport\TransportInterface;
|
|
|
20 |
use Symfony\Component\Mime\Email;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* SymfonyMailerHandler uses Symfony's Mailer component to send the emails
|
|
|
24 |
*
|
|
|
25 |
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
26 |
*
|
|
|
27 |
* @phpstan-import-type Record from \Monolog\Logger
|
|
|
28 |
*/
|
|
|
29 |
class SymfonyMailerHandler extends MailHandler
|
|
|
30 |
{
|
|
|
31 |
/** @var MailerInterface|TransportInterface */
|
|
|
32 |
protected $mailer;
|
|
|
33 |
/** @var Email|callable(string, Record[]): Email */
|
|
|
34 |
private $emailTemplate;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* @psalm-param Email|callable(string, Record[]): Email $email
|
|
|
38 |
*
|
|
|
39 |
* @param MailerInterface|TransportInterface $mailer The mailer to use
|
|
|
40 |
* @param callable|Email $email An email template, the subject/body will be replaced
|
|
|
41 |
*/
|
|
|
42 |
public function __construct($mailer, $email, $level = Logger::ERROR, bool $bubble = true)
|
|
|
43 |
{
|
|
|
44 |
parent::__construct($level, $bubble);
|
|
|
45 |
|
|
|
46 |
$this->mailer = $mailer;
|
|
|
47 |
$this->emailTemplate = $email;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* {@inheritDoc}
|
|
|
52 |
*/
|
|
|
53 |
protected function send(string $content, array $records): void
|
|
|
54 |
{
|
|
|
55 |
$this->mailer->send($this->buildMessage($content, $records));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Gets the formatter for the Swift_Message subject.
|
|
|
60 |
*
|
|
|
61 |
* @param string|null $format The format of the subject
|
|
|
62 |
*/
|
|
|
63 |
protected function getSubjectFormatter(?string $format): FormatterInterface
|
|
|
64 |
{
|
|
|
65 |
return new LineFormatter($format);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Creates instance of Email to be sent
|
|
|
70 |
*
|
|
|
71 |
* @param string $content formatted email body to be sent
|
|
|
72 |
* @param array $records Log records that formed the content
|
|
|
73 |
*
|
|
|
74 |
* @phpstan-param Record[] $records
|
|
|
75 |
*/
|
|
|
76 |
protected function buildMessage(string $content, array $records): Email
|
|
|
77 |
{
|
|
|
78 |
$message = null;
|
|
|
79 |
if ($this->emailTemplate instanceof Email) {
|
|
|
80 |
$message = clone $this->emailTemplate;
|
|
|
81 |
} elseif (is_callable($this->emailTemplate)) {
|
|
|
82 |
$message = ($this->emailTemplate)($content, $records);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
if (!$message instanceof Email) {
|
|
|
86 |
$record = reset($records);
|
|
|
87 |
throw new \InvalidArgumentException('Could not resolve message as instance of Email or a callable returning it' . ($record ? Utils::getRecordMessageForException($record) : ''));
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if ($records) {
|
|
|
91 |
$subjectFormatter = $this->getSubjectFormatter($message->getSubject());
|
|
|
92 |
$message->subject($subjectFormatter->format($this->getHighestRecord($records)));
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if ($this->isHtmlBody($content)) {
|
|
|
96 |
if (null !== ($charset = $message->getHtmlCharset())) {
|
|
|
97 |
$message->html($content, $charset);
|
|
|
98 |
} else {
|
|
|
99 |
$message->html($content);
|
|
|
100 |
}
|
|
|
101 |
} else {
|
|
|
102 |
if (null !== ($charset = $message->getTextCharset())) {
|
|
|
103 |
$message->text($content, $charset);
|
|
|
104 |
} else {
|
|
|
105 |
$message->text($content);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
return $message->date(new \DateTimeImmutable());
|
|
|
110 |
}
|
|
|
111 |
}
|