Passed
Push — main ( b7649a...398f47 )
by Michiel
06:32
created

MailTask   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
eloc 54
dl 0
loc 174
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setMsg() 0 3 1
A setFrom() 0 3 1
A setSubject() 0 3 1
A setTo() 0 3 1
A sendFilesets() 0 31 4
A main() 0 15 3
A setBackend() 0 3 1
A setBackendParams() 0 17 4
A setRecipient() 0 3 1
A addText() 0 3 1
A setMessage() 0 3 1
A setToList() 0 3 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\Optional;
21
22
use Phing\Exception\BuildException;
23
use Phing\Task;
24
use Phing\Type\Element\FileSetAware;
25
26
/**
27
 * Send an e-mail message
28
 *
29
 * <mail tolist="[email protected]" subject="build complete">The build process is a success...</mail>
30
 *
31
 * @author  Michiel Rook <[email protected]>
32
 * @author  Francois Harvey at SecuriWeb (http://www.securiweb.net)
33
 * @package phing.tasks.ext
34
 */
35
class MailTask extends Task
36
{
37
    use FileSetAware;
38
39
    protected $tolist = null;
40
    protected $subject = null;
41
    protected $msg = null;
42
    protected $from = null;
43
44
    protected $backend = 'mail';
45
    protected $backendParams = [];
46
47
    public function main()
48
    {
49
        if (empty($this->from)) {
50
            throw new BuildException('Missing "from" attribute');
51
        }
52
53
        $this->log('Sending mail to ' . $this->tolist);
54
55
        if (!empty($this->filesets)) {
56
            $this->sendFilesets();
57
58
            return;
59
        }
60
61
        mail($this->tolist, $this->subject, $this->msg, "From: {$this->from}\n");
62
    }
63
64
    protected function sendFilesets()
65
    {
66
        @include_once 'Mail.php';
67
        @include_once 'Mail/mime.php';
68
69
        if (!class_exists('Mail_mime')) {
70
            throw new BuildException('Need the pear/mail and pear/mail_mime packages installed');
71
        }
72
73
        $mime = new \Mail_mime(['text_charset' => 'UTF-8']);
74
        $hdrs = [
75
            'From' => $this->from,
76
            'Subject' => $this->subject
77
        ];
78
        $mime->setTXTBody($this->msg);
79
80
        foreach ($this->filesets as $fs) {
81
            $ds = $fs->getDirectoryScanner($this->project);
82
            $fromDir = $fs->getDir($this->project);
83
            $srcFiles = $ds->getIncludedFiles();
84
85
            foreach ($srcFiles as $file) {
86
                $mime->addAttachment($fromDir . DIRECTORY_SEPARATOR . $file, 'application/octet-stream');
87
            }
88
        }
89
90
        $body = $mime->get();
91
        $hdrs = $mime->headers($hdrs);
92
93
        $mail = \Mail::factory($this->backend, $this->backendParams);
94
        $mail->send($this->tolist, $hdrs, $body);
95
    }
96
97
    /**
98
     * Setter for message
99
     *
100
     * @param $msg
101
     */
102
    public function setMsg($msg)
103
    {
104
        $this->setMessage($msg);
105
    }
106
107
    /**
108
     * Alias setter
109
     *
110
     * @param $msg
111
     */
112
    public function setMessage($msg)
113
    {
114
        $this->msg = (string) $msg;
115
    }
116
117
    /**
118
     * Setter for subject
119
     *
120
     * @param $subject
121
     */
122
    public function setSubject($subject)
123
    {
124
        $this->subject = (string) $subject;
125
    }
126
127
    /**
128
     * Setter for tolist
129
     *
130
     * @param $tolist
131
     */
132
    public function setToList($tolist)
133
    {
134
        $this->tolist = $tolist;
135
    }
136
137
    /**
138
     * Alias for (deprecated) recipient
139
     *
140
     * @param $recipient
141
     */
142
    public function setRecipient($recipient)
143
    {
144
        $this->tolist = (string) $recipient;
145
    }
146
147
    /**
148
     * Alias for to
149
     *
150
     * @param $to
151
     */
152
    public function setTo($to)
153
    {
154
        $this->tolist = (string) $to;
155
    }
156
157
    /**
158
     * Supports the <mail>Message</mail> syntax.
159
     *
160
     * @param $msg
161
     */
162
    public function addText($msg)
163
    {
164
        $this->msg = (string) $msg;
165
    }
166
167
    /**
168
     * Sets email address of sender
169
     *
170
     * @param $from
171
     */
172
    public function setFrom($from)
173
    {
174
        $this->from = $from;
175
    }
176
177
    /**
178
     * Sets PEAR Mail backend to use
179
     *
180
     * @param $backend
181
     */
182
    public function setBackend($backend)
183
    {
184
        $this->backend = $backend;
185
    }
186
187
    /**
188
     * Sets PEAR Mail backend params to use
189
     *
190
     * @param $backendParams
191
     */
192
    public function setBackendParams($backendParams)
193
    {
194
        $params = explode(',', $backendParams);
195
196
        foreach ($params as $param) {
197
            $values = explode('=', $param);
198
199
            if (count($values) < 1) {
200
                continue;
201
            }
202
203
            if (count($values) == 1) {
204
                $this->backendParams[] = $values[0];
205
            } else {
206
                $key = $values[0];
207
                $value = $values[1];
208
                $this->backendParams[$key] = $value;
209
            }
210
        }
211
    }
212
}
213