|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
declare(strict_types=1); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
23
|
|
|
* |
|
24
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
|
25
|
|
|
* |
|
26
|
|
|
* This program is free software; you can redistribute it and/or |
|
27
|
|
|
* modify it under the terms of the GNU General Public License |
|
28
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
29
|
|
|
* of the License, or (at your option) any later version. |
|
30
|
|
|
* |
|
31
|
|
|
* This program is distributed in the hope that it will be useful, |
|
32
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
33
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
34
|
|
|
* GNU General Public License for more details. |
|
35
|
|
|
* |
|
36
|
|
|
* You should have received a copy of the GNU General Public License |
|
37
|
|
|
* along with this program; if not, write to the Free Software |
|
38
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
namespace App\EventSubscriber; |
|
42
|
|
|
|
|
43
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
44
|
|
|
use Symfony\Component\Mailer\Event\MessageEvent; |
|
45
|
|
|
use Symfony\Component\Mime\Address; |
|
46
|
|
|
use Symfony\Component\Mime\Email; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* This subscriber set the "From" field for all sent email, based on the global configured sender name and email. |
|
50
|
|
|
*/ |
|
51
|
|
|
final class SetEmailFromSubscriber implements EventSubscriberInterface |
|
52
|
|
|
{ |
|
53
|
|
|
private $email; |
|
54
|
|
|
private $name; |
|
55
|
|
|
private $envelope_sender; |
|
56
|
|
|
|
|
57
|
|
|
public function __construct(string $email, string $name, string $envelope_sender) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->email = $email; |
|
60
|
|
|
$this->name = $name; |
|
61
|
|
|
|
|
62
|
|
|
$this->envelope_sender = $envelope_sender; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function onMessage(MessageEvent $event): void |
|
66
|
|
|
{ |
|
67
|
|
|
$address = new Address($this->email, $this->name); |
|
68
|
|
|
$event->getEnvelope() |
|
69
|
|
|
->setSender($address); |
|
70
|
|
|
$email = $event->getMessage(); |
|
71
|
|
|
|
|
72
|
|
|
//Set envelope sender if one was specified |
|
73
|
|
|
if (!empty($this->envelope_sender)) { |
|
74
|
|
|
$sender_address = new Address($this->envelope_sender); |
|
75
|
|
|
$event->getEnvelope() |
|
76
|
|
|
->setSender($sender_address); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($email instanceof Email) { |
|
80
|
|
|
$email->from($address); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public static function getSubscribedEvents(): array |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
|
|
// should be the last one to allow header changes by other listeners first |
|
88
|
|
|
MessageEvent::class => ['onMessage'], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|