|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package org.openpsa.mail |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\Mailer\Mailer; |
|
10
|
|
|
use Symfony\Component\Mailer\Transport\TransportInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Backend for mailer operations |
|
14
|
|
|
* |
|
15
|
|
|
* @package org.openpsa.mail |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class org_openpsa_mail_backend |
|
18
|
|
|
{ |
|
19
|
|
|
public $error; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Mailer |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $mailer; |
|
25
|
|
|
|
|
26
|
|
|
abstract public function __construct(array $params); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Send the actual email |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract public function mail(org_openpsa_mail_message $message); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Prepare the mail backend |
|
35
|
|
|
*/ |
|
36
|
9 |
|
public static function get(string $implementation, array $params) : self |
|
37
|
|
|
{ |
|
38
|
9 |
|
if (defined('OPENPSA2_UNITTEST_RUN')) { |
|
39
|
9 |
|
return self::_load_backend('unittest', $params); |
|
40
|
|
|
} |
|
41
|
|
|
if ($implementation == 'try_default') { |
|
42
|
|
|
$try_backends = midcom_baseclasses_components_configuration::get('org.openpsa.mail', 'config')->get('default_try_backends'); |
|
43
|
|
|
//Use first available backend in list |
|
44
|
|
|
foreach ($try_backends as $backend) { |
|
45
|
|
|
try { |
|
46
|
|
|
$object = self::_load_backend($backend, $params); |
|
47
|
|
|
debug_add('Using backend ' . $backend); |
|
48
|
|
|
return $object; |
|
49
|
|
|
} catch (midcom_error $e) { |
|
50
|
|
|
debug_add('Failed to load backend ' . $backend . ', message:' . $e->getMessage()); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
throw new midcom_error('All configured backends failed to load'); |
|
54
|
|
|
} |
|
55
|
|
|
return self::_load_backend($implementation, $params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function prepare_mailer(TransportInterface $transport) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->mailer = new Mailer($transport); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
9 |
|
private static function _load_backend(string $backend, array $params) : self |
|
64
|
|
|
{ |
|
65
|
9 |
|
$default_params = midcom_baseclasses_components_configuration::get('org.openpsa.mail', 'config')->get($backend . '_params'); |
|
66
|
9 |
|
if (is_array($default_params)) { |
|
67
|
|
|
$params = array_merge($default_params, $params); |
|
68
|
|
|
} |
|
69
|
9 |
|
$classname = 'org_openpsa_mail_backend_' . $backend; |
|
70
|
9 |
|
return new $classname($params); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
final public function send(org_openpsa_mail_message $message) |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
8 |
|
$ret = $this->mail($message); |
|
77
|
8 |
|
$this->error = null; |
|
78
|
8 |
|
return $ret; |
|
79
|
|
|
} catch (Exception $e) { |
|
80
|
|
|
$this->error = $e->getMessage(); |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function get_error_message() : string |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->error === null) { |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->error ?: 'Unknown error'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|