|
0
|
1 |
<?php |
|
|
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 |
|
|
|
16 |
/** |
|
|
17 |
* SwiftMailerHandler uses Swift_Mailer to send the emails |
|
|
18 |
* |
|
|
19 |
* @author Gyula Sallai |
|
|
20 |
*/ |
|
|
21 |
class SwiftMailerHandler extends MailHandler |
|
|
22 |
{ |
|
|
23 |
protected $mailer; |
|
|
24 |
protected $message; |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* @param \Swift_Mailer $mailer The mailer to use |
|
|
28 |
* @param callback|\Swift_Message $message An example message for real messages, only the body will be replaced |
|
|
29 |
* @param integer $level The minimum logging level at which this handler will be triggered |
|
|
30 |
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
|
|
31 |
*/ |
|
|
32 |
public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true) |
|
|
33 |
{ |
|
|
34 |
parent::__construct($level, $bubble); |
|
|
35 |
$this->mailer = $mailer; |
|
|
36 |
if (!$message instanceof \Swift_Message && is_callable($message)) { |
|
|
37 |
$message = call_user_func($message); |
|
|
38 |
} |
|
|
39 |
if (!$message instanceof \Swift_Message) { |
|
|
40 |
throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callback returning it'); |
|
|
41 |
} |
|
|
42 |
$this->message = $message; |
|
|
43 |
} |
|
|
44 |
|
|
|
45 |
/** |
|
|
46 |
* {@inheritdoc} |
|
|
47 |
*/ |
|
|
48 |
protected function send($content) |
|
|
49 |
{ |
|
|
50 |
$message = clone $this->message; |
|
|
51 |
$message->setBody($content); |
|
|
52 |
|
|
|
53 |
$this->mailer->send($message); |
|
|
54 |
} |
|
|
55 |
} |