1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MonologPHPMailer; |
4
|
|
|
|
5
|
|
|
use Monolog\Formatter\FormatterInterface; |
6
|
|
|
use Monolog\Formatter\LineFormatter; |
7
|
|
|
use Monolog\Handler\MailHandler; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
use PHPMailer\PHPMailer\PHPMailer; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PHPMailer handler for Monolog 2.x. |
13
|
|
|
* |
14
|
|
|
* It uses [PHPMailer](https://github.com/PHPMailer/PHPMailer/) to send emails. |
15
|
|
|
* |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
* |
18
|
|
|
* @author Filip Š <[email protected]> |
19
|
|
|
* |
20
|
|
|
* @license MIT |
21
|
|
|
* |
22
|
|
|
* @package MonologPHPMailer |
23
|
|
|
*/ |
24
|
|
|
class PHPMailerHandler2 extends MailHandler |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* A PHPMailer instance. |
28
|
|
|
* |
29
|
|
|
* @var PHPMailer $mailer |
30
|
|
|
*/ |
31
|
|
|
protected $mailer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructs the PHPMailer handler. |
35
|
|
|
* |
36
|
|
|
* @param PHPMailer $mailer A PHPMailer instance to use. |
37
|
|
|
* @param int|string $level The minimum logging level at which this handler will be triggered. |
38
|
|
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not. |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(PHPMailer $mailer, $level = Logger::ERROR, bool $bubble = true) |
41
|
|
|
{ |
42
|
3 |
|
parent::__construct($level, $bubble); |
|
|
|
|
43
|
3 |
|
$this->mailer = $mailer; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sends a mail with the given content. |
48
|
|
|
* |
49
|
|
|
* @param string $content Formatted email body to be sent. |
50
|
|
|
* @param array $records The array of log records that formed this content. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
1 |
|
protected function send(string $content, array $records): void |
55
|
|
|
{ |
56
|
1 |
|
$mailer = $this->buildMessage($content, $records); |
57
|
1 |
|
$mailer->send(); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Builds a message to be sent. |
62
|
|
|
* |
63
|
|
|
* @param string $content Formatted email body to be sent. |
64
|
|
|
* @param array $records The array of log records that formed this content. |
65
|
|
|
* |
66
|
|
|
* @return PHPMailer Builded message. |
67
|
|
|
*/ |
68
|
2 |
|
public function buildMessage(string $content, array $records): PHPMailer |
69
|
|
|
{ |
70
|
2 |
|
$mailer = clone $this->mailer; |
71
|
|
|
|
72
|
2 |
|
if (substr($content, 0, 1) == '<') { |
73
|
2 |
|
$mailer->isHTML(true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// @codingStandardsIgnoreStart |
77
|
2 |
|
if ($records) { |
|
|
|
|
78
|
2 |
|
$subjectFormatter = new LineFormatter($mailer->Subject); |
79
|
2 |
|
$mailer->Subject = $subjectFormatter->format($this->getHighestRecord($records)); |
80
|
|
|
} |
81
|
|
|
// @codingStandardsIgnoreEnd |
82
|
|
|
|
83
|
|
|
// @codingStandardsIgnoreStart |
84
|
2 |
|
$mailer->Body = $content; |
85
|
|
|
// @codingStandardsIgnoreEnd |
86
|
|
|
|
87
|
2 |
|
return $mailer; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|