-
Notifications
You must be signed in to change notification settings - Fork 14
/
MimeMailTask.php
277 lines (242 loc) · 6.67 KB
/
MimeMailTask.php
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
require_once 'phing/Task.php';
/**
* Send MIME multipart e-mail message. Loosely based on the MailTask provided by phing
*
* <mimemail from="sender@example.org"
* to="user1@example.org"
* cc="user2@example.org,user3@example.org"
* bcc="restricteduser@example.org"
* subject="build complete"
* html="true"
* template="somefile.html" />
*/
class MimeMailTask extends Task
{
/**
* Indicate if message is html. Defaults to false.
* @var boolean
*/
protected $html = false;
/**
* Message subject. Defaults to '(No subject)'
* @var string
*/
protected $subject = null;
/**
* Message body.
* @var string
*/
protected $message = null;
/**
* Message as separate template filename. Overrides $message.
* @var string
*/
protected $template = null;
/**
* Sender email address. Required.
* @var string
*/
protected $from = null;
/**
* Direct recipient email address(es) as comma-delimited list.
* @var string
*/
protected $to = array();
/**
* CC'd recipient email address(es) as comma-delimited list.
* @var string
*/
protected $cc = array();
/**
* BCC'd recipient email address(es) as comma-delimited list.
* @var string
*/
protected $bcc = array();
public function init()
{
require_once 'Mail.php';
if (false == class_exists('Mail')) {
throw new BuildException("This task depends on PEAR::Mail");
}
require_once 'Mail/mime.php';
if (false == class_exists('Mail_mime')) {
throw new BuildException("This task depends on PEAR::Mail_Mime");
}
}
public function main()
{
$mime = new Mail_mime();
// Add message as html or plain text
if ($this->isHtml()) {
$mime->setHTMLBody($this->getMessage());
}
else {
$mime->setTXTBody($this->getMessage());
}
// Add recipients
foreach ($this->getTo() as $recipient) {
$mime->addTo($recipient);
}
foreach ($this->getCc() as $recipient) {
$mime->addCc($recipient);
}
foreach ($this->getBcc() as $recipient) {
$mime->addBcc($recipient);
}
// Add sender and subject
$mime->setFrom($this->getFrom());
$mime->setSubject($this->getSubject());
// Send the damnedable thing!
$body = $mime->get();
$headers = $mime->headers();
$this->log("Assembled headers: " . print_r($headers, true));
$mail =& Mail::factory('mail');
$status = $mail->send($this->getTo(), $headers, $body);
// Handle the outcome of our send attempt
if ($status === true) {
$recipCount = count($this->getTo()) + count($this->getCc()) + count($this->getBcc());
$this->log("Sent MIME mail to $recipCount recipients");
}
else {
throw new BuildException('Failed to send MIME mail: ' . $status->getMessage());
}
}
/**
* Is message plain text or html
*/
public function setHtml($flag)
{
$this->html = (bool) $flag;
}
public function getHtml()
{
return $this->html;
}
public function isHtml()
{
return $this->getHtml();
}
/**
* Subject
*/
public function setSubject($subject)
{
$this->subject = (string) $subject;
}
public function getSubject()
{
if (empty($this->subject)) {
return '(No subject)';
}
return $this->subject;
}
/**
* Message template (overrides message text)
*/
public function setTemplate($filename)
{
if (!is_readable($filename)) {
throw new BuildException("Template '$filename' does not exist or is not readable");
}
$this->template = $filename;
}
public function getTemplate()
{
return $this->template;
}
/**
* Message text
*/
public function setMessage($message)
{
$this->message = (string) $message;
}
public function getMessage()
{
if(null !== $this->getTemplate()) {
$content = file_get_contents( $this->getTemplate() );
$this->setMessage( $this->project->replaceProperties($content) );
}
return $this->message;
}
/**
* Supports the <mimemail>Message</mimemail> syntax.
*/
public function addText($message)
{
$this->setMessage($message);
}
/**
* Sender
*/
public function setFrom($sender)
{
# note: filter_var requires PHP >= 5.2.0
$this->from = filter_var($sender, FILTER_VALIDATE_EMAIL);
}
public function getFrom()
{
if (empty($this->from)) {
throw new BuildException('Missing "from" attribute');
}
return $this->from;
}
/**
* Direct recipient(s)
*/
public function setTo($recipients)
{
foreach (explode(',', $recipients) as $recipient) {
$filteredRecip = filter_var($recipient, FILTER_VALIDATE_EMAIL);
if ($filteredRecip) {
$this->to[] = $filteredRecip;
}
else {
throw new BuildException('Bad recipient for "to" attribute: ' . $recipient);
}
}
}
public function getTo()
{
return $this->to;
}
/**
* CC'd recipient(s)
*/
public function setCc($recipients)
{
foreach (explode(',', $recipients) as $recipient) {
$filteredRecip = filter_var($recipient, FILTER_VALIDATE_EMAIL);
if ($filteredRecip) {
$this->cc[] = $filteredRecip;
}
else {
throw new BuildException('Bad recipient for "cc" attribute: ' . $recipient);
}
}
}
public function getCc()
{
return $this->cc;
}
/**
* BCC'd recipient(s)
*/
public function setBcc($recipients)
{
foreach (explode(',', $recipients) as $recipient) {
$filteredRecip = filter_var($recipient, FILTER_VALIDATE_EMAIL);
if ($filteredRecip) {
$this->bcc[] = $filteredRecip;
}
else {
throw new BuildException('Bad recipient for "bcc" attribute: ' . $recipient);
}
}
}
public function getBcc()
{
return $this->bcc;
}
}