1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ilateral\SilverStripe\Notifier\Types; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Email\Email; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Simple wrapper for SilverStripe email notoifications |
9
|
|
|
*/ |
10
|
|
|
class EmailNotification extends NotificationType |
11
|
|
|
{ |
12
|
|
|
private static $table_name = "Notifications_EmailNotification"; |
|
|
|
|
13
|
|
|
|
14
|
|
|
private static $singular_name = 'Email Notification'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
private static $plural_name = 'Email Notifications'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
private static $template = self::class; |
19
|
|
|
|
20
|
|
|
private static $db = [ |
|
|
|
|
21
|
|
|
'Subject' => 'Varchar' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
private static $casting = [ |
|
|
|
|
25
|
|
|
'RenderedSubject' => 'Varchar' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Return a rendered version of this notification's subject using the |
30
|
|
|
* current object as a base |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getRenderedSubject(): string |
35
|
|
|
{ |
36
|
|
|
return $this->renderString((string) $this->Subject); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function send( |
40
|
|
|
array $custom_recipients = [], |
41
|
|
|
array $custom_data = [] |
42
|
|
|
) { |
43
|
|
|
$recipients = array_merge( |
44
|
|
|
$this->getRecipients(), |
45
|
|
|
$custom_recipients |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$from = $this->getSender(); |
49
|
|
|
$template = $this->config()->template; |
50
|
|
|
$object = $this->getObject(); |
51
|
|
|
$subject = $this->getRenderedSubject(); |
52
|
|
|
$content = $this->getRenderedContent(); |
53
|
|
|
$data = [ |
54
|
|
|
'Object' => $object, |
55
|
|
|
'Content' => $content |
56
|
|
|
]; |
57
|
|
|
$data = array_merge($data, $custom_data); |
58
|
|
|
|
59
|
|
|
if (empty($from)) { |
60
|
|
|
$from = Email::config()->admin_email; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($recipients as $recipient) { |
64
|
|
|
// If recipient is blank for some reason |
65
|
|
|
// then skip sending |
66
|
|
|
$recipient = trim($recipient); |
67
|
|
|
|
68
|
|
|
if (empty($recipient)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
Email::create() |
73
|
|
|
->setTo($recipient) |
74
|
|
|
->setFrom($from) |
75
|
|
|
->setSubject($subject) |
76
|
|
|
->setData($data) |
77
|
|
|
->setHTMLTemplate($template) |
78
|
|
|
->send(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|