ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SwiftMailerHandler.php
Go to the documentation of this file.
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;
17 use Swift;
18 
25 {
26  protected $mailer;
28 
35  public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
36  {
37  parent::__construct($level, $bubble);
38 
39  $this->mailer = $mailer;
40  $this->messageTemplate = $message;
41  }
42 
46  protected function send($content, array $records)
47  {
48  $this->mailer->send($this->buildMessage($content, $records));
49  }
50 
57  protected function getSubjectFormatter($format)
58  {
59  return new LineFormatter($format);
60  }
61 
69  protected function buildMessage($content, array $records)
70  {
71  $message = null;
72  if ($this->messageTemplate instanceof \Swift_Message) {
74  $message->generateId();
75  } elseif (is_callable($this->messageTemplate)) {
76  $message = call_user_func($this->messageTemplate, $content, $records);
77  }
78 
79  if (!$message instanceof \Swift_Message) {
80  throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
81  }
82 
83  if ($records) {
84  $subjectFormatter = $this->getSubjectFormatter($message->getSubject());
85  $message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
86  }
87 
88  $message->setBody($content);
89  if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
90  $message->setDate(new \DateTimeImmutable());
91  } else {
92  $message->setDate(time());
93  }
94 
95  return $message;
96  }
97 
101  public function __get($name)
102  {
103  if ($name === 'message') {
104  trigger_error('SwiftMailerHandler->message is deprecated, use ->buildMessage() instead to retrieve the message', E_USER_DEPRECATED);
105 
106  return $this->buildMessage(null, array());
107  }
108 
109  throw new \InvalidArgumentException('Invalid property '.$name);
110  }
111 }
const ERROR
Runtime errors.
Definition: Logger.php:58
$format
Definition: metadata.php:141
__get($name)
BC getter, to be removed in 2.0.
$records
Definition: simple_test.php:22
catch(Exception $e) $message
__construct(\Swift_Mailer $mailer, $message, $level=Logger::ERROR, $bubble=true)
send($content, array $records)
{}
buildMessage($content, array $records)
Creates instance of Swift_Message to be sent.
Base class for all mail handlers.
Definition: MailHandler.php:19
getSubjectFormatter($format)
Gets the formatter for the Swift_Message subject.
Formats incoming records into a one-line string.
SwiftMailerHandler uses Swift_Mailer to send the emails.