-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathEmailNotify.php
More file actions
222 lines (188 loc) · 6.24 KB
/
Copy pathEmailNotify.php
File metadata and controls
222 lines (188 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Exception\HttpException;
use PHPCensor\Helper\Email as EmailHelper;
use PHPCensor\Plugin;
use PHPCensor\View;
use Psr\Log\LogLevel;
use PHPCensor\Common\Exception\RuntimeException;
/**
* Email Plugin - Provides simple email capability.
*
* @package PHP Censor
* @subpackage Application
*
* @author Steve Brazier <meadsteve@gmail.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class EmailNotify extends Plugin
{
/**
* @return string
*/
public static function pluginName()
{
return 'email_notify';
}
/**
* Send a notification mail.
*
* @return bool
*
* @throws HttpException
*/
public function execute()
{
$addresses = $this->getEmailAddresses();
// Without some email addresses in the yml file then we
// can't do anything.
if (\count($addresses) === 0) {
return false;
}
$buildStatus = $this->build->isSuccessful() ? "Passing Build" : "Failing Build";
$projectName = $this->build->getProject()->getTitle();
try {
$view = $this->getMailTemplate();
} catch (RuntimeException $e) {
$this->builder->log(
\sprintf('Unknown mail template "%s", falling back to default.', $this->options['template']),
LogLevel::WARNING
);
$view = $this->getDefaultMailTemplate();
}
$view->build = $this->build;
$view->project = $this->build->getProject();
$layout = new View('Email/layout');
$layout->build = $this->build;
$layout->project = $this->build->getProject();
$layout->content = $view->render();
$body = $layout->render();
$sendFailures = $this->sendSeparateEmails(
$addresses,
\sprintf("PHP Censor - %s - %s", $projectName, $buildStatus),
$body
);
// This is a success if we've not failed to send anything.
$this->builder->log(\sprintf('%d emails sent.', (\count($addresses) - $sendFailures)));
$this->builder->log(\sprintf('%d emails failed to send.', $sendFailures));
return ($sendFailures === 0);
}
/**
* @param string $toAddress Single address to send to
* @param string[] $ccList
* @param string $subject Email subject
* @param string $body Email body
*
* @return int
*/
protected function sendEmail($toAddress, $ccList, $subject, $body)
{
$email = new EmailHelper($this->builder->getConfiguration());
$email->setEmailTo($this->builder->interpolate($toAddress), $this->builder->interpolate($toAddress));
$email->setSubject($this->builder->interpolate($subject));
$email->setBody($this->builder->interpolate($body));
$email->setHtml(true);
if (\is_array($ccList) && \count($ccList)) {
foreach ($ccList as $address) {
$email->addCc($this->builder->interpolate($address), $this->builder->interpolate($address));
}
}
return $email->send($this->builder);
}
/**
* Send an email to a list of specified subjects.
*
* @param array $toAddresses
* List of destination addresses for message.
* @param string $subject
* Mail subject
* @param string $body
* Mail body
*
* @return int number of failed messages
*/
public function sendSeparateEmails(array $toAddresses, $subject, $body)
{
$failures = 0;
$ccList = $this->getCcAddresses();
foreach ($toAddresses as $address) {
if (!$this->sendEmail($address, $ccList, $subject, $body)) {
$failures++;
}
}
return $failures;
}
/**
* Get the list of email addresses to send to.
* @return array
*/
protected function getEmailAddresses()
{
$addresses = [];
$committer = $this->build->getCommitterEmail();
$this->builder->logDebug(\sprintf("Committer email: '%s'", $committer));
$this->builder->logDebug(\sprintf(
"Committer option: '%s'",
(!empty($this->options['committer']) && $this->options['committer']) ? 'true' : 'false'
));
if (!empty($this->options['committer']) && $this->options['committer']) {
if ($committer) {
$addresses[] = $committer;
}
}
$this->builder->logDebug(\sprintf(
"Addresses option: '%s'",
(!empty($this->options['addresses']) && \is_array($this->options['addresses'])) ? \implode(', ', $this->options['addresses']) : 'false'
));
if (!empty($this->options['addresses']) && \is_array($this->options['addresses'])) {
foreach ($this->options['addresses'] as $address) {
$addresses[] = $address;
}
}
$this->builder->logDebug(\sprintf(
"Default mailTo option: '%s'",
!empty($this->options['default_mailto_address']) ? $this->options['default_mailto_address'] : 'false'
));
if (empty($addresses) && !empty($this->options['default_mailto_address'])) {
$addresses[] = $this->options['default_mailto_address'];
}
return \array_unique($addresses);
}
/**
* Get the list of email addresses to CC.
*
* @return array
*/
protected function getCcAddresses()
{
$ccAddresses = [];
if (isset($this->options['cc'])) {
foreach ($this->options['cc'] as $address) {
$ccAddresses[] = $address;
}
}
return $ccAddresses;
}
/**
* Get the mail template used to sent the mail.
*
* @return View
*/
protected function getMailTemplate()
{
if (isset($this->options['template'])) {
return new View('Email/' . $this->options['template']);
}
return $this->getDefaultMailTemplate();
}
/**
* Get the default mail template.
*
* @return View
*/
protected function getDefaultMailTemplate()
{
$template = $this->build->isSuccessful() ? 'short' : 'long';
return new View('Email/' . $template);
}
}