|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Email; |
|
4
|
|
|
|
|
5
|
|
|
class SwiftPlugin implements \Swift_Events_SendListener |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Before sending a message make sure all our overrides are taken into account |
|
9
|
|
|
* |
|
10
|
|
|
* @param \Swift_Events_SendEvent $evt |
|
11
|
|
|
*/ |
|
12
|
|
|
public function beforeSendPerformed(\Swift_Events_SendEvent $evt) |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Swift_Message $message */ |
|
16
|
|
|
$message = $evt->getMessage(); |
|
17
|
|
|
$sendAllTo = Email::config()->send_all_emails_to; |
|
|
|
|
|
|
18
|
|
|
$ccAllTo = Email::config()->cc_all_emails_to; |
|
|
|
|
|
|
19
|
|
|
$bccAllTo = Email::config()->bcc_all_emails_to; |
|
|
|
|
|
|
20
|
|
|
$sendAllFrom = Email::config()->send_all_emails_from; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
if (!empty($sendAllTo)) { |
|
23
|
|
|
$this->setTo($message, $sendAllTo); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (!empty($ccAllTo)) { |
|
27
|
|
|
if (!is_array($ccAllTo)) { |
|
28
|
|
|
$ccAllTo = array($ccAllTo => null); |
|
29
|
|
|
} |
|
30
|
|
|
foreach ($ccAllTo as $address => $name) { |
|
31
|
|
|
$message->addCc($address, $name); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!empty($bccAllTo)) { |
|
36
|
|
|
if (!is_array($bccAllTo)) { |
|
37
|
|
|
$bccAllTo = array($bccAllTo => null); |
|
38
|
|
|
} |
|
39
|
|
|
foreach ($bccAllTo as $address => $name) { |
|
40
|
|
|
$message->addBcc($address, $name); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (!empty($sendAllFrom)) { |
|
45
|
|
|
$this->setFrom($message, $sendAllFrom); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \Swift_Mime_Message $message |
|
51
|
|
|
* @param string $to |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function setTo($message, $to) |
|
54
|
|
|
{ |
|
55
|
|
|
$headers = $message->getHeaders(); |
|
56
|
|
|
$origTo = $message->getTo(); |
|
57
|
|
|
$cc = $message->getCc(); |
|
58
|
|
|
$bcc = $message->getBcc(); |
|
59
|
|
|
|
|
60
|
|
|
// set default recipient and remove all other recipients |
|
61
|
|
|
$message->setTo($to); |
|
62
|
|
|
$headers->removeAll('Cc'); |
|
63
|
|
|
$headers->removeAll('Bcc'); |
|
64
|
|
|
|
|
65
|
|
|
// store the old data as X-Original-* Headers for debugging |
|
66
|
|
|
$headers->addMailboxHeader('X-Original-To', $origTo); |
|
67
|
|
|
$headers->addMailboxHeader('X-Original-Cc', $cc); |
|
68
|
|
|
$headers->addMailboxHeader('X-Original-Bcc', $bcc); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param \Swift_Mime_Message $message |
|
73
|
|
|
* @param string $from |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function setFrom($message, $from) |
|
76
|
|
|
{ |
|
77
|
|
|
$headers = $message->getHeaders(); |
|
78
|
|
|
$origFrom = $message->getFrom(); |
|
79
|
|
|
$headers->addMailboxHeader('X-Original-From', $origFrom); |
|
80
|
|
|
$message->setFrom($from); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function sendPerformed(\Swift_Events_SendEvent $evt) |
|
84
|
|
|
{ |
|
85
|
|
|
// noop |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.