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
/**
21
 * Uses PEAR Mail package to send the build log to one or
22
 * more recipients.
23
 *
24
 * @author  Michiel Rook <mrook@php.net>
25
 * @package phing.listener
26
 */
27
class MailLogger extends DefaultLogger
28
{
29
    private $mailMessage = '';
30

31
    private $from = 'phing@phing.info';
32

33
    private $tolist;
34

35
    /**
36
     * Construct new MailLogger
37
     */
38 0
    public function __construct()
39
    {
40 0
        parent::__construct();
41

42 0
        if (!class_exists('Mail')) {
43 0
            throw new BuildException('Need the PEAR Mail package to send logs');
44
        }
45

46 0
        $tolist = Phing::getDefinedProperty('phing.log.mail.recipients');
47

48 0
        if (!empty($tolist)) {
49 0
            $this->tolist = $tolist;
50
        }
51
    }
52

53
    /**
54
     * @see DefaultLogger::printMessage
55
     * @param string $message
56
     * @param OutputStream $stream
57
     * @param int $priority
58
     */
59 0
    final protected function printMessage($message, OutputStream $stream, $priority)
60
    {
61 0
        if ($message !== null) {
62 0
            $this->mailMessage .= $message . "\n";
63
        }
64
    }
65

66
    /**
67
     * Sends the mail
68
     *
69
     * @see   DefaultLogger#buildFinished
70
     * @param BuildEvent $event
71
     */
72 0
    public function buildFinished(BuildEvent $event)
73
    {
74 0
        parent::buildFinished($event);
75

76 0
        $project = $event->getProject();
77 0
        $properties = $project->getProperties();
78

79 0
        $filename = $properties['phing.log.mail.properties.file'];
80

81
        // overlay specified properties file (if any), which overrides project
82
        // settings
83 0
        $fileProperties = new Properties();
84 0
        $file = new PhingFile($filename);
85

86 0
        try {
87 0
            $fileProperties->load($file);
88 0
        } catch (IOException $ioe) {
89
            // ignore because properties file is not required
90
        }
91

92 0
        foreach ($fileProperties as $key => $value) {
93 0
            $properties['key'] = $project->replaceProperties($value);
94
        }
95

96 0
        $success = $event->getException() === null;
97 0
        $prefix = $success ? 'success' : 'failure';
98

99 0
        try {
100 0
            $notify = StringHelper::booleanValue($this->getValue($properties, $prefix . '.notify', 'on'));
101 0
            if (!$notify) {
102 0
                return;
103
            }
104

105 0
            if (is_string(Phing::getDefinedProperty('phing.log.mail.subject'))) {
106 0
                $defaultSubject = Phing::getDefinedProperty('phing.log.mail.subject');
107 0
            } else {
108 0
                $defaultSubject = ($success) ? 'Build Success' : 'Build Failure';
109
            }
110 0
            $hdrs = [];
111 0
            $hdrs['From'] = $this->getValue($properties, 'from', $this->from);
112 0
            $hdrs['Reply-To'] = $this->getValue($properties, 'replyto', '');
113 0
            $hdrs['Cc'] = $this->getValue($properties, $prefix . '.cc', '');
114 0
            $hdrs['Bcc'] = $this->getValue($properties, $prefix . '.bcc', '');
115 0
            $hdrs['Body'] = $this->getValue($properties, $prefix . '.body', '');
116 0
            $hdrs['Subject'] = $this->getValue($properties, $prefix . '.subject', $defaultSubject);
117 0
            $tolist = $this->getValue($properties, $prefix . '.to', $this->tolist);
118 0
        } catch (BadMethodCallException $e) {
119 0
            $project->log($e->getMessage(), Project::MSG_WARN);
120
        }
121

122 0
        if (empty($tolist)) {
123 0
            return;
124
        }
125

126 0
        $mail = Mail::factory('mail');
127 0
        $mail->send($tolist, $hdrs, $this->mailMessage);
128
    }
129

130
    /**
131
     * @param array $properties
132
     * @param string $name
133
     * @param mixed $defaultValue
134
     *
135
     * @return mixed
136
     *
137
     * @throws BadMethodCallException
138
     */
139 0
    private function getValue(array $properties, $name, $defaultValue)
140
    {
141 0
        $propertyName = 'phing.log.mail.' . $name;
142 0
        $value = $properties[$propertyName];
143 0
        if ($value === null) {
144 0
            $value = $defaultValue;
145
        }
146 0
        if ($value === null) {
147 0
            throw new BadMethodCallException('Missing required parameter: ' . $propertyName);
148
        }
149 0
        return $value;
150
    }
151
}

Read our documentation on viewing source code .

Loading