summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Mail.php
blob: 7581f4549b4bd05deeb2ff4802d1bf516ca5c76d (plain)
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
<?php
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting;

use Zend_Mail;
use Zend_Mail_Transport_Sendmail;
use Zend_Mime;
use Zend_Mime_Part;

class Mail
{
    /** @var string */
    const DEFAULT_SUBJECT = 'Icinga Reporting';

    /** @var string */
    protected $from;

    /** @var string */
    protected $subject = self::DEFAULT_SUBJECT;

    /** @var Zend_Mail_Transport_Sendmail */
    protected $transport;

    /** @var array */
    protected $attachments = [];

    /**
     * Get the from part
     *
     * @return  string
     */
    public function getFrom()
    {
        if (isset($this->from)) {
            return $this->from;
        }

        if (isset($_SERVER['SERVER_ADMIN'])) {
            $this->from = $_SERVER['SERVER_ADMIN'];

            return $this->from;
        }

        foreach (['HTTP_HOST', 'SERVER_NAME', 'HOSTNAME'] as $key) {
            if (isset($_SEVER[$key])) {
                $this->from = 'icinga-reporting@' . $_SERVER[$key];

                return $this->from;
            }
        }

        $this->from = 'icinga-reporting@localhost';

        return $this->from;
    }

    /**
     * Set the from part
     *
     * @param   string $from
     *
     * @return  $this
     */
    public function setFrom($from)
    {
        $this->from = $from;

        return $this;
    }

    /**
     * Get the subject
     *
     * @return  string
     */
    public function getSubject()
    {
        return $this->subject;
    }

    /**
     * Set the subject
     *
     * @param   string $subject
     *
     * @return  $this
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;

        return $this;
    }

    /**
     * Get the mail transport
     *
     * @return  Zend_Mail_Transport_Sendmail
     */
    public function getTransport()
    {
        if (! isset($this->transport)) {
            $this->transport = new Zend_Mail_Transport_Sendmail('-f ' . escapeshellarg($this->getFrom()));
        }

        return $this->transport;
    }

    public function attachCsv($csv, $filename)
    {
        if (is_array($csv)) {
            $csv = Str::putcsv($csv);
        }

        $attachment = new Zend_Mime_Part($csv);

        $attachment->type = 'text/csv';
        $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
        $attachment->encoding = Zend_Mime::ENCODING_BASE64;
        $attachment->filename = basename($filename, '.csv') . '.csv';

        $this->attachments[] = $attachment;

        return $this;
    }

    public function attachJson($json, $filename)
    {
        if (is_array($json)) {
            $json = json_encode($json);
        }

        $attachment = new Zend_Mime_Part($json);

        $attachment->type = 'application/json';
        $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
        $attachment->encoding = Zend_Mime::ENCODING_BASE64;
        $attachment->filename = basename($filename, '.json') . '.json';

        $this->attachments[] = $attachment;

        return $this;
    }

    public function attachPdf($pdf, $filename)
    {
        $attachment = new Zend_Mime_Part($pdf);

        $attachment->type = 'application/pdf';
        $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
        $attachment->encoding = Zend_Mime::ENCODING_BASE64;
        $attachment->filename = basename($filename, '.pdf') . '.pdf';

        $this->attachments[] = $attachment;

        return $this;
    }

    public function send($body, $recipient)
    {
        $mail = new Zend_Mail('UTF-8');

        $mail->setFrom($this->getFrom());
        $mail->addTo($recipient);
        $mail->setSubject($this->getSubject());

        if (strlen($body) !== strlen(strip_tags($body))) {
            $mail->setBodyHtml($body);
        } else {
            $mail->setBodyText($body);
        }

        foreach ($this->attachments as $attachment) {
            $mail->addAttachment($attachment);
        }

        $mail->send($this->getTransport());
    }
}