Passed
Push — main ( e5a85d...619edc )
by Michiel
07:04
created

MailTask::setTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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