3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * The filtered-mailer is a utility to wrap an existing PEAR Mail class
14 * and apply extra filters. It is primarily intended for resolving
15 * quirks in the standard implementations.
17 * This wrapper acts a bit like a chameleon, passing-through properties
18 * from the underlying object. Consequently, internal properties are
19 * prefixed with `_` to avoid conflict.
22 * @copyright CiviCRM LLC https://civicrm.org/licensing
24 class CRM_Utils_Mail_FilteredPearMailer
extends Mail
{
28 * Ex: 'smtp' or 'sendmail'
45 protected $_filters = [];
48 * CRM_Utils_Mail_FilteredPearMailer constructor.
49 * @param string $driver
50 * @param array $params
53 public function __construct($driver, $params, $mailer) {
54 $this->_driver
= $driver;
55 $this->_params
= $params;
56 $this->_delegate
= $mailer;
59 public function send($recipients, $headers, $body) {
60 $filterArgs = [$this, &$recipients, &$headers, &$body];
61 foreach ($this->_filters
as $filter) {
62 $result = call_user_func_array($filter, $filterArgs);
63 if ($result !== NULL) {
68 return $this->_delegate
->send($recipients, $headers, $body);
73 * Unique ID for this filter. Filters are sorted by ID.
74 * Suggestion: '{nnnn}_{name}', where '{nnnn}' is a number.
75 * Filters are sorted and executed in order.
76 * @param callable $func
77 * function(FilteredPearMailer $mailer, mixed $recipients, array $headers, string $body).
78 * The return value should generally be null/void. However, if you wish to
79 * short-circuit execution of the filters, then return a concrete value.
82 public function addFilter($id, $func) {
83 $this->_filters
[$id] = $func;
84 ksort($this->_filters
);
90 * Ex: 'smtp', 'sendmail', 'mail'.
92 public function getDriver() {
93 return $this->_driver
;
96 public function &__get($name) {
97 return $this->_delegate
->{$name};
100 public function __set($name, $value) {
101 return $this->_delegate
->{$name} = $value;
104 public function __isset($name) {
105 return isset($this->_delegate
->{$name});
108 public function __unset($name) {
109 unset($this->_delegate
->{$name});
112 public function disconnect() {
113 if (is_callable([$this->_delegate
, 'disconnect'])) {
114 return $this->_delegate
->disconnect();