| 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 Swift_Message;
|
|
|
19 |
use Swift;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* SwiftMailerHandler uses Swift_Mailer to send the emails
|
|
|
23 |
*
|
|
|
24 |
* @author Gyula Sallai
|
|
|
25 |
*
|
|
|
26 |
* @phpstan-import-type Record from \Monolog\Logger
|
|
|
27 |
* @deprecated Since Monolog 2.6. Use SymfonyMailerHandler instead.
|
|
|
28 |
*/
|
|
|
29 |
class SwiftMailerHandler extends MailHandler
|
|
|
30 |
{
|
|
|
31 |
/** @var \Swift_Mailer */
|
|
|
32 |
protected $mailer;
|
|
|
33 |
/** @var Swift_Message|callable(string, Record[]): Swift_Message */
|
|
|
34 |
private $messageTemplate;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* @psalm-param Swift_Message|callable(string, Record[]): Swift_Message $message
|
|
|
38 |
*
|
|
|
39 |
* @param \Swift_Mailer $mailer The mailer to use
|
|
|
40 |
* @param callable|Swift_Message $message An example message for real messages, only the body will be replaced
|
|
|
41 |
*/
|
|
|
42 |
public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, bool $bubble = true)
|
|
|
43 |
{
|
|
|
44 |
parent::__construct($level, $bubble);
|
|
|
45 |
|
|
|
46 |
@trigger_error('The SwiftMailerHandler is deprecated since Monolog 2.6. Use SymfonyMailerHandler instead.', E_USER_DEPRECATED);
|
|
|
47 |
|
|
|
48 |
$this->mailer = $mailer;
|
|
|
49 |
$this->messageTemplate = $message;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* {@inheritDoc}
|
|
|
54 |
*/
|
|
|
55 |
protected function send(string $content, array $records): void
|
|
|
56 |
{
|
|
|
57 |
$this->mailer->send($this->buildMessage($content, $records));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Gets the formatter for the Swift_Message subject.
|
|
|
62 |
*
|
|
|
63 |
* @param string|null $format The format of the subject
|
|
|
64 |
*/
|
|
|
65 |
protected function getSubjectFormatter(?string $format): FormatterInterface
|
|
|
66 |
{
|
|
|
67 |
return new LineFormatter($format);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Creates instance of Swift_Message to be sent
|
|
|
72 |
*
|
|
|
73 |
* @param string $content formatted email body to be sent
|
|
|
74 |
* @param array $records Log records that formed the content
|
|
|
75 |
* @return Swift_Message
|
|
|
76 |
*
|
|
|
77 |
* @phpstan-param Record[] $records
|
|
|
78 |
*/
|
|
|
79 |
protected function buildMessage(string $content, array $records): Swift_Message
|
|
|
80 |
{
|
|
|
81 |
$message = null;
|
|
|
82 |
if ($this->messageTemplate instanceof Swift_Message) {
|
|
|
83 |
$message = clone $this->messageTemplate;
|
|
|
84 |
$message->generateId();
|
|
|
85 |
} elseif (is_callable($this->messageTemplate)) {
|
|
|
86 |
$message = ($this->messageTemplate)($content, $records);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if (!$message instanceof Swift_Message) {
|
|
|
90 |
$record = reset($records);
|
|
|
91 |
throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it' . ($record ? Utils::getRecordMessageForException($record) : ''));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if ($records) {
|
|
|
95 |
$subjectFormatter = $this->getSubjectFormatter($message->getSubject());
|
|
|
96 |
$message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$mime = 'text/plain';
|
|
|
100 |
if ($this->isHtmlBody($content)) {
|
|
|
101 |
$mime = 'text/html';
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$message->setBody($content, $mime);
|
|
|
105 |
/** @phpstan-ignore-next-line */
|
|
|
106 |
if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
|
|
|
107 |
$message->setDate(new \DateTimeImmutable());
|
|
|
108 |
} else {
|
|
|
109 |
/** @phpstan-ignore-next-line */
|
|
|
110 |
$message->setDate(time());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return $message;
|
|
|
114 |
}
|
|
|
115 |
}
|